checking for config file
This commit is contained in:
55
chub/config.go
Normal file
55
chub/config.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/edindazdarevic/confighub/chub/globals"
|
||||
homedir "github.com/mitchellh/go-homedir"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type config struct {
|
||||
MachineGuid string
|
||||
}
|
||||
|
||||
func configurationDirectoryName() string {
|
||||
directoryName, err := homedir.Dir()
|
||||
globals.PanicWithStyle("I cannot find your home directory. Are you homeless ? ", err)
|
||||
|
||||
expandedDirectoryName, err := homedir.Expand(directoryName)
|
||||
globals.PanicWithStyle("Cannot know your full path to home directory. Is it a secret ? ", err)
|
||||
|
||||
return expandedDirectoryName + globals.RuneToAscii(filepath.Separator) + globals.ConfigurationDirectoryName
|
||||
}
|
||||
|
||||
func configurationFilePath() string {
|
||||
|
||||
return configurationDirectoryName() + globals.RuneToAscii(filepath.Separator) + globals.ConfigurationFileName
|
||||
|
||||
}
|
||||
|
||||
func saveGuidToHomedir(configuration config) {
|
||||
directory := configurationDirectoryName()
|
||||
if !globals.FileExists(directory) {
|
||||
err := os.Mkdir(directory, globals.ConfigurationFilePermissions)
|
||||
globals.PanicWithStyle("I simply cannot make the directory. Am I alowed to ?", err)
|
||||
}
|
||||
|
||||
configByteContent, err := json.Marshal(configuration)
|
||||
globals.PanicWithStyle("Something is terribly wrong. I am sorry. I am so so sorry. :~(", err)
|
||||
|
||||
err = ioutil.WriteFile(configurationFilePath(), configByteContent, globals.ConfigurationFilePermissions)
|
||||
globals.PanicWithStyle("I cannot write to config file in user directory. How can this be ?", err)
|
||||
}
|
||||
|
||||
func loadGuidFromHomedir() config {
|
||||
result := config{}
|
||||
|
||||
file, err := ioutil.ReadFile(configurationFilePath())
|
||||
globals.PanicWithStyle("I cannot read config file in user directory. How can this be ?", err)
|
||||
|
||||
json.Unmarshal(file, &result)
|
||||
return result
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user