Refactoring #34
7
util/persistentStateDir.go
Normal file
7
util/persistentStateDir.go
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
//go:build !linux
|
||||||
|
|
||||||
|
package util
|
||||||
|
|
||||||
|
const (
|
||||||
|
persistentStateDir = "./"
|
||||||
|
)
|
5
util/persistentStateDir_linux.go
Normal file
5
util/persistentStateDir_linux.go
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
package util
|
||||||
|
|
||||||
|
const (
|
||||||
|
persistentStateDir = "/var/lib/spaceapid/"
|
||||||
|
)
|
28
util/util.go
28
util/util.go
|
@ -5,13 +5,13 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
|
|
||||||
"git.hamburg.ccc.de/ccchh/spaceapid/types"
|
"git.hamburg.ccc.de/ccchh/spaceapid/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
savedStateJSONPath = "/var/lib/spaceapid/spaceapid-state.json"
|
persistentStateFile = "spaceapid-state.json"
|
||||||
|
persistentStatePath = persistentStateDir + persistentStateFile
|
||||||
)
|
)
|
||||||
|
|
||||||
// MergeOldState merges a given SpaceAPIResponse with the state saved at the time of last program exit and then deletes
|
// MergeOldState merges a given SpaceAPIResponse with the state saved at the time of last program exit and then deletes
|
||||||
|
@ -23,12 +23,12 @@ func MergeOldState(response *types.SpaceAPIResponseV14) {
|
||||||
persistedState types.PersistentStateV14
|
persistedState types.PersistentStateV14
|
||||||
)
|
)
|
||||||
|
|
||||||
log.Println("Merging old state from", savedStateJSONPath, "...")
|
log.Println("Merging old state from", persistentStatePath, "...")
|
||||||
|
|
||||||
// Check if state.json is present
|
// Check if state.json is present
|
||||||
_, err = os.Stat(savedStateJSONPath)
|
_, err = os.Stat(persistentStatePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Old state json not accessible at", savedStateJSONPath, ", skipping merge... error:", err)
|
log.Println("Old state json not accessible at", persistentStatePath, ", skipping merge... error:", err)
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -36,14 +36,14 @@ func MergeOldState(response *types.SpaceAPIResponseV14) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read file and load persisted state
|
// Read file and load persisted state
|
||||||
oldState, err = os.ReadFile(savedStateJSONPath)
|
oldState, err = os.ReadFile(persistentStatePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error reading old state from", savedStateJSONPath, ", skipping merge... error:", err)
|
log.Println("Error reading old state from", persistentStatePath, ", skipping merge... error:", err)
|
||||||
goto removeOld
|
goto removeOld
|
||||||
}
|
}
|
||||||
err = json.Unmarshal(oldState, &persistedState)
|
err = json.Unmarshal(oldState, &persistedState)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(savedStateJSONPath, "doesn't seem to contain valid data... error:", err)
|
log.Println(persistentStatePath, "doesn't seem to contain valid data... error:", err)
|
||||||
goto removeOld
|
goto removeOld
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,23 +66,23 @@ func MergeOldState(response *types.SpaceAPIResponseV14) {
|
||||||
|
|
||||||
// Delete old state json
|
// Delete old state json
|
||||||
removeOld:
|
removeOld:
|
||||||
err = os.RemoveAll(savedStateJSONPath)
|
err = os.RemoveAll(persistentStatePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to remove", savedStateJSONPath, ", continuing... error:", err)
|
log.Println("Failed to remove", persistentStatePath, ", continuing... error:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SaveCurrentState(response types.SpaceAPIResponseV14) {
|
func SaveCurrentState(response types.SpaceAPIResponseV14) {
|
||||||
// Create state directory if not present
|
// Create state directory if not present
|
||||||
err := os.MkdirAll(path.Dir(savedStateJSONPath), 0750)
|
err := os.MkdirAll(persistentStateDir, 0750)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Failed creating", savedStateJSONPath, ", aborting... error:", err)
|
log.Fatalln("Failed creating", persistentStateDir, ", aborting... error:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open persistent state file
|
// Open persistent state file
|
||||||
file, err := os.OpenFile(savedStateJSONPath, os.O_WRONLY|os.O_CREATE, 0644)
|
file, err := os.OpenFile(persistentStatePath, os.O_WRONLY|os.O_CREATE, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Failed opening", savedStateJSONPath, "while trying to save current state... error:", err)
|
log.Fatalln("Failed opening", persistentStatePath, "while trying to save current state... error:", err)
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue