This commit is contained in:
Jeff Carr 2024-11-21 10:27:40 -06:00
parent 6d4a1be367
commit 5633f4204d
1 changed files with 14 additions and 10 deletions

View File

@ -13,6 +13,7 @@ import (
// writes out the cluster information it seperate files // writes out the cluster information it seperate files
// to make it humanly possible to hand edit things as needed // to make it humanly possible to hand edit things as needed
// write to ~/.config/forge/ unless ENV{FORGE_HOME} is set
func (m *Repos) ConfigSave() error { func (m *Repos) ConfigSave() error {
if os.Getenv("FORGE_HOME") == "" { if os.Getenv("FORGE_HOME") == "" {
homeDir, _ := os.UserHomeDir() homeDir, _ := os.UserHomeDir()
@ -39,7 +40,8 @@ func (m *Repos) ConfigSave() error {
return nil return nil
} }
func (m *Repos) ConfigLoad() error { // load the ~/.config/forge/ files
func (c *Repos) ConfigLoad() error {
if os.Getenv("FORGE_HOME") == "" { if os.Getenv("FORGE_HOME") == "" {
homeDir, _ := os.UserHomeDir() homeDir, _ := os.UserHomeDir()
fullpath := filepath.Join(homeDir, ".config/forge") fullpath := filepath.Join(homeDir, ".config/forge")
@ -47,8 +49,9 @@ func (m *Repos) ConfigLoad() error {
} }
var data []byte var data []byte
var err error var err error
if m == nil { if c == nil {
return errors.New("It's not safe to run ConfigLoad() on a nil ?") // can't safely do c = new(Repo) if c is in a struct from the caller. notsure why
return errors.New("It's not safe to run ConfigLoad() on a nil")
} }
if data, err = loadFile("forge.pb"); err != nil { if data, err = loadFile("forge.pb"); err != nil {
@ -61,11 +64,11 @@ func (m *Repos) ConfigLoad() error {
// todo: error out if the file is empty? // todo: error out if the file is empty?
// try forge.text & forge.json? // try forge.text & forge.json?
} }
if err = m.Unmarshal(data); err != nil { if err = c.Unmarshal(data); err != nil {
log.Warn("broken forge.pb config file") log.Warn("broken forge.pb config file")
return err return err
} }
log.Info("config load found", len(m.Repos), "repos") log.Info("config load found", len(c.Repos), "repos")
return nil return nil
} }
@ -81,11 +84,11 @@ func (m *Repos) ConfigLoad() error {
if len(data) == 0 { if len(data) == 0 {
// todo: error out if the file is empty? // todo: error out if the file is empty?
} }
if err = m.UnmarshalTEXT(data); err != nil { if err = c.UnmarshalTEXT(data); err != nil {
log.Warn("broken forge.text config file") log.Warn("broken forge.text config file")
return err return err
} }
log.Info("config load found", len(m.Repos), "repos") log.Info("config load found", len(c.Repos), "repos")
return nil return nil
} }
@ -101,16 +104,17 @@ func (m *Repos) ConfigLoad() error {
if len(data) == 0 { if len(data) == 0 {
// todo: error out if the file is empty? // todo: error out if the file is empty?
} }
if err = m.UnmarshalJSON(data); err != nil { if err = c.UnmarshalJSON(data); err != nil {
log.Warn("broken forge.json config file") log.Warn("broken forge.json config file")
return err return err
} }
log.Info("config load found", len(m.Repos), "repos") log.Info("config load found", len(c.Repos), "repos")
return nil return nil
} }
m.SampleConfig()
// first time user. make a template config file // first time user. make a template config file
c.SampleConfig()
return nil return nil
} }