1
0
Fork 0
mirror of https://github.com/Luzifer/sii.git synced 2024-12-21 08:21:16 +00:00
sii/cmd/sii-editor/config.go
Knut Ahlers ab39609f45
Implement Save-Game Editor (#1)
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2019-12-28 13:06:28 +00:00

53 lines
1.1 KiB
Go

package main
import (
"os"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
var errUserConfigNotFound = errors.New("User config not found")
type configFile struct {
GameDirectories map[string]string `yaml:"game_directories"`
ProfileDirectories map[string]string `yaml:"profile_directories"`
}
func (c *configFile) loadDefaults() error {
for k, dv := range gamePaths {
if c.GameDirectories[k] == "" {
c.GameDirectories[k] = dv
}
}
for k, dv := range profilePaths {
if c.ProfileDirectories[k] == "" {
c.ProfileDirectories[k] = dv
}
}
return nil
}
func loadUserConfig(p string) (*configFile, error) {
var c = &configFile{
GameDirectories: map[string]string{},
ProfileDirectories: map[string]string{},
}
if _, err := os.Stat(p); err != nil {
if os.IsNotExist(err) {
return c, errUserConfigNotFound
}
return c, errors.Wrap(err, "Unable to stat user config")
}
f, err := os.Open(p)
if err != nil {
return c, errors.Wrap(err, "Unable to open user config")
}
defer f.Close()
return c, errors.Wrap(yaml.NewDecoder(f).Decode(&c), "Unable to read user config")
}