From c7c6086d11fb469748b479b82a3941960e5a8b83 Mon Sep 17 00:00:00 2001 From: Bennett Wetters Date: Wed, 31 Jan 2024 17:08:39 +0100 Subject: [PATCH] Rename constant for persistent state file and fix it at compile-time --- util/persistentStateDir.go | 7 +++++++ util/persistentStateDir_linux.go | 5 +++++ util/util.go | 28 ++++++++++++++-------------- 3 files changed, 26 insertions(+), 14 deletions(-) create mode 100644 util/persistentStateDir.go create mode 100644 util/persistentStateDir_linux.go diff --git a/util/persistentStateDir.go b/util/persistentStateDir.go new file mode 100644 index 0000000..96a9749 --- /dev/null +++ b/util/persistentStateDir.go @@ -0,0 +1,7 @@ +//go:build !linux + +package util + +const ( + persistentStateDir = "./" +) diff --git a/util/persistentStateDir_linux.go b/util/persistentStateDir_linux.go new file mode 100644 index 0000000..9f36a47 --- /dev/null +++ b/util/persistentStateDir_linux.go @@ -0,0 +1,5 @@ +package util + +const ( + persistentStateDir = "/var/lib/spaceapid/" +) diff --git a/util/util.go b/util/util.go index 5f109d8..5d1e8bd 100644 --- a/util/util.go +++ b/util/util.go @@ -5,13 +5,13 @@ import ( "errors" "log" "os" - "path" "git.hamburg.ccc.de/ccchh/spaceapid/types" ) 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 @@ -23,12 +23,12 @@ func MergeOldState(response *types.SpaceAPIResponseV14) { persistedState types.PersistentStateV14 ) - log.Println("Merging old state from", savedStateJSONPath, "...") + log.Println("Merging old state from", persistentStatePath, "...") // Check if state.json is present - _, err = os.Stat(savedStateJSONPath) + _, err = os.Stat(persistentStatePath) 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) { return } @@ -36,14 +36,14 @@ func MergeOldState(response *types.SpaceAPIResponseV14) { } // Read file and load persisted state - oldState, err = os.ReadFile(savedStateJSONPath) + oldState, err = os.ReadFile(persistentStatePath) 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 } err = json.Unmarshal(oldState, &persistedState) 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 } @@ -66,23 +66,23 @@ func MergeOldState(response *types.SpaceAPIResponseV14) { // Delete old state json removeOld: - err = os.RemoveAll(savedStateJSONPath) + err = os.RemoveAll(persistentStatePath) 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) { // Create state directory if not present - err := os.MkdirAll(path.Dir(savedStateJSONPath), 0750) + err := os.MkdirAll(persistentStateDir, 0750) if err != nil { - log.Fatalln("Failed creating", savedStateJSONPath, ", aborting... error:", err) + log.Fatalln("Failed creating", persistentStateDir, ", aborting... error:", err) } // 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 { - 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()