diff options
| author | Jeff Carr <[email protected]> | 2024-11-20 10:31:25 -0600 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2024-11-20 10:31:25 -0600 |
| commit | 39a8d9e13ecb9619418bd15dba5ef3ecf4e2d9f9 (patch) | |
| tree | b516483849e974eb8fdb1ae9421f892ebccc98d6 /config.go | |
| parent | 35a2db858f1635f947c2b5f9bbfe9bb64ef7a472 (diff) | |
config files are cool!
Diffstat (limited to 'config.go')
| -rw-r--r-- | config.go | 45 |
1 files changed, 41 insertions, 4 deletions
@@ -9,7 +9,6 @@ import ( "path/filepath" "go.wit.com/log" - "google.golang.org/protobuf/proto" ) // writes out the cluster information it seperate files @@ -29,18 +28,50 @@ func (m *Repos) ConfigSave() error { } func (m *Repos) ConfigLoad() error { + var data []byte + var err error if m == nil { return errors.New("It's not safe to run ConfigLoad() on a nil ?") } - if data, err := loadFile("forge.pb"); err == nil { - if err = proto.Unmarshal(data, m); err != nil { + if data, err = loadFile("forge.pb"); err != nil { + // something went wrong loading the file + return err + } + + if data != nil { + // this means the forge.pb file exists and was read + if len(data) == 0 { + // todo: error out if the file is empty? + // try forge.text & forge.json? + } + if err = m.Unmarshal(data); err != nil { log.Warn("broken forge.pb config file") return err } - } else { + return nil + } + + // forge.db doesn't exist. try forge.text + // this lets the user hand edit the config + if data, err = loadFile("forge.text"); err != nil { + // something went wrong loading the file return err } + + if data != nil { + // this means the forge.text file exists and was read + if len(data) == 0 { + // todo: error out if the file is empty? + } + if err = m.UnmarshalTEXT(data); err != nil { + log.Warn("broken forge.text config file") + return err + } + return nil + } + + // first time user. make a template config file return nil } @@ -49,6 +80,12 @@ func loadFile(filename string) ([]byte, error) { p := filepath.Join(homeDir, ".config/forge") fullname := filepath.Join(p, filename) data, err := os.ReadFile(fullname) + if errors.Is(err, os.ErrNotExist) { + // if file does not exist, just return nil. this + // will cause ConfigLoad() to try the next config file like "forge.text" + // because the user might want to edit the .config by hand + return nil, nil + } if err != nil { // log.Info("open config file :", err) return nil, err |
