Read/Write config, create default config if not exists

This commit is contained in:
Justin Hawkins 2021-09-30 12:27:59 +09:30
parent b0804b743e
commit 910cb443bd
2 changed files with 81 additions and 7 deletions

View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"log" "log"
"os"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
@ -26,10 +27,10 @@ type UI struct {
} }
type Config struct { type Config struct {
ConfigVersion int `yaml:"config_version" json:"config_version"`
Server Server `yaml:"server" json:"server"` Server Server `yaml:"server" json:"server"`
UI UI `yaml:"ui" json:"ui"` UI UI `yaml:"ui" json:"ui"`
DownloadProfiles []DownloadProfile `yaml:"profiles" json:"profiles"` DownloadProfiles []DownloadProfile `yaml:"profiles" json:"profiles"`
ConfigVersion int `yaml:"config_version" json:"config_version"`
} }
func DefaultConfig() *Config { func DefaultConfig() *Config {
@ -74,11 +75,74 @@ func (c *Config) UpdateFromJSON(j []byte) error {
return nil return nil
} }
func WriteDefaultConfig(path string) { func configPath() string {
defaultConfig := DefaultConfig() dir, err := os.UserConfigDir()
s, err := yaml.Marshal(&defaultConfig) if err != nil {
log.Fatalf("cannot find a directory to store config: %v", err)
}
appDir := "gropple"
fullPath := dir + string(os.PathSeparator) + appDir
_, err = os.Stat(fullPath)
if os.IsNotExist(err) {
err := os.Mkdir(fullPath, 0777)
if err != nil {
log.Fatalf("Could not create config dir '%s': %v", fullPath, err)
}
}
fullFilename := fullPath + string(os.PathSeparator) + "config.yml"
return fullFilename
}
func ConfigFileExists() bool {
info, err := os.Stat(configPath())
if os.IsNotExist(err) {
return false
}
if err != nil {
log.Fatal(err)
}
if info.Size() == 0 {
log.Print("config file is 0 bytes?")
return false
}
return true
}
func LoadConfig() (*Config, error) {
path := configPath()
b, err := os.ReadFile(path)
if err != nil {
log.Printf("Could not read config '%s': %v", path, err)
return nil, err
}
c := Config{}
err = yaml.Unmarshal(b, &c)
if err != nil {
log.Printf("Could not parse YAML config '%s': %v", path, err)
return nil, err
}
return &c, nil
}
func (c *Config) WriteConfig() {
s, err := yaml.Marshal(c)
if err != nil { if err != nil {
panic(err) panic(err)
} }
log.Print(string(s))
path := configPath()
file, err := os.Create(
path,
)
if err != nil {
log.Fatalf("Could not open config file")
}
file.Write(s)
file.Close()
log.Printf("Stored in %s", path)
} }

14
main.go
View File

@ -45,7 +45,17 @@ var webFS embed.FS
var conf *config.Config var conf *config.Config
func main() { func main() {
conf = config.DefaultConfig() if !config.ConfigFileExists() {
log.Print("No config file - creating default config")
conf = config.DefaultConfig()
conf.WriteConfig()
} else {
loadedConfig, err := config.LoadConfig()
if err != nil {
log.Fatal(err)
}
conf = loadedConfig
}
r := mux.NewRouter() r := mux.NewRouter()
r.HandleFunc("/", HomeHandler) r.HandleFunc("/", HomeHandler)
@ -152,7 +162,7 @@ func ConfigRESTHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
} }
conf.WriteConfig()
b, _ := json.Marshal(conf) b, _ := json.Marshal(conf)
w.Write(b) w.Write(b)
} }