ConfigLoad() was totally broken
This commit is contained in:
parent
275a7db0e0
commit
338018376b
45
config.go
45
config.go
|
@ -5,6 +5,7 @@ package forgepb
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
|
@ -64,24 +65,12 @@ func (c *ForgeConfigs) ConfigLoad() error {
|
|||
// var err error
|
||||
if c == nil {
|
||||
// can't safely do c = new(ForgeConfig) if c is in a struct from the caller. notsure why
|
||||
// TODO: recheck this. it might work now? It's probably still a bad idea(?)
|
||||
return errors.New("It's not safe to run ConfigLoad() on a nil")
|
||||
}
|
||||
|
||||
// this lets the user hand edit the config
|
||||
if data, err := loadFile("forge.text"); err == nil {
|
||||
if data != nil {
|
||||
// this means the forge.text file exists and was read
|
||||
if len(data) != 0 {
|
||||
if err = c.UnmarshalTEXT(data); err != nil {
|
||||
log.Info("forge.ConfigLoad()", len(c.ForgeConfigs), "entries in ~/.config/forge")
|
||||
// forge.pb file was broken. save on load right away
|
||||
log.Info("attempting forge.ConfigSave()")
|
||||
c.ConfigSave()
|
||||
return nil
|
||||
}
|
||||
// todo: error out if the file is empty?
|
||||
}
|
||||
}
|
||||
if err := c.loadText(); err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// forge.text doesn't exist. try forge.json
|
||||
|
@ -93,7 +82,7 @@ func (c *ForgeConfigs) ConfigLoad() error {
|
|||
if len(data) != 0 {
|
||||
if err = c.UnmarshalJSON(data); err == nil {
|
||||
log.Info("forge.ConfigLoad()", len(c.ForgeConfigs), "entries in ~/.config/forge")
|
||||
// forge.pb file was broken. save on load right away
|
||||
// forge.text file was broken. save on load right away
|
||||
log.Info("attempting forge.ConfigSave()")
|
||||
c.ConfigSave()
|
||||
return nil
|
||||
|
@ -109,11 +98,33 @@ func (c *ForgeConfigs) ConfigLoad() error {
|
|||
}
|
||||
|
||||
// first time user. make a template config file
|
||||
c.sampleConfig()
|
||||
log.Info("crap")
|
||||
// c.sampleConfig()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *ForgeConfigs) loadText() error {
|
||||
// this lets the user hand edit the config
|
||||
data, err := loadFile("forge.text")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if data == nil {
|
||||
return fmt.Errorf("forge.text data was nil")
|
||||
}
|
||||
if len(data) == 0 {
|
||||
return fmt.Errorf("forge.text was empty")
|
||||
}
|
||||
|
||||
// attempt to marshal forge.text
|
||||
if err := c.UnmarshalTEXT(data); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Info("forge.ConfigLoad()", len(c.ForgeConfigs), "entries in ~/.config/forge")
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadFile(filename string) ([]byte, error) {
|
||||
fullname := filepath.Join(os.Getenv("FORGE_CONFIG"), filename)
|
||||
data, err := os.ReadFile(fullname)
|
||||
|
|
Loading…
Reference in New Issue