Little bit of code refactoring

This commit is contained in:
Dane Everitt 2020-04-10 18:07:57 -07:00
parent af241f3de4
commit 17d204a631
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
3 changed files with 19 additions and 19 deletions

1
.gitignore vendored
View File

@ -45,3 +45,4 @@ test_*/
# Keep all gitkeep files (This needs to stay at the bottom) # Keep all gitkeep files (This needs to stay at the bottom)
!.gitkeep !.gitkeep
debug debug
data/.states.json

View File

@ -161,7 +161,7 @@ func LoadDirectory() error {
return errors.New(rerr.String()) return errors.New(rerr.String())
} }
states, err := FetchServerStates() states, err := getServerStates()
if err != nil { if err != nil {
return errors.WithStack(err) return errors.WithStack(err)
} }

View File

@ -8,17 +8,16 @@ import (
"sync" "sync"
) )
var ( const stateFileLocation = "data/.states.json"
statesLock sync.Mutex
statesFile = "data/states.json"
)
// DoesStatesFileExist . var stateMutex sync.Mutex
func DoesStatesFileExist() (bool, error) {
statesLock.Lock()
defer statesLock.Unlock()
if _, err := os.Stat(statesFile); err != nil { // Checks if the state tracking file exists, if not it is generated.
func ensureStateFileExists() (bool, error) {
stateMutex.Lock()
defer stateMutex.Unlock()
if _, err := os.Stat(stateFileLocation); err != nil {
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
return false, errors.WithStack(err) return false, errors.WithStack(err)
} }
@ -29,17 +28,17 @@ func DoesStatesFileExist() (bool, error) {
return true, nil return true, nil
} }
// FetchServerStates . // Returns the state of the servers.
func FetchServerStates() (map[string]string, error) { func getServerStates() (map[string]string, error) {
// Check if the states file exists. // Check if the states file exists.
exists, err := DoesStatesFileExist() exists, err := ensureStateFileExists()
if err != nil { if err != nil {
return nil, errors.WithStack(err) return nil, errors.WithStack(err)
} }
// Request a lock after we check if the file exists. // Request a lock after we check if the file exists.
statesLock.Lock() stateMutex.Lock()
defer statesLock.Unlock() defer stateMutex.Unlock()
// Return an empty map if the file does not exist. // Return an empty map if the file does not exist.
if !exists { if !exists {
@ -47,7 +46,7 @@ func FetchServerStates() (map[string]string, error) {
} }
// Open the states file. // Open the states file.
f, err := os.Open(statesFile) f, err := os.Open(stateFileLocation)
if err != nil { if err != nil {
return nil, errors.WithStack(err) return nil, errors.WithStack(err)
} }
@ -76,11 +75,11 @@ func SaveServerStates() error {
return errors.WithStack(err) return errors.WithStack(err)
} }
statesLock.Lock() stateMutex.Lock()
defer statesLock.Unlock() defer stateMutex.Unlock()
// Write the data to the file // Write the data to the file
if err := ioutil.WriteFile(statesFile, data, 0644); err != nil { if err := ioutil.WriteFile(stateFileLocation, data, 0644); err != nil {
return errors.WithStack(err) return errors.WithStack(err)
} }