ConfigLoad() was totally broken

This commit is contained in:
Jeff Carr 2025-01-05 01:20:12 -06:00
parent 275a7db0e0
commit 338018376b
1 changed files with 28 additions and 17 deletions

View File

@ -5,6 +5,7 @@ package forgepb
import ( import (
"errors" "errors"
"fmt"
"os" "os"
"path/filepath" "path/filepath"
@ -64,24 +65,12 @@ func (c *ForgeConfigs) ConfigLoad() error {
// var err error // var err error
if c == nil { if c == nil {
// can't safely do c = new(ForgeConfig) if c is in a struct from the caller. notsure why // 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") return errors.New("It's not safe to run ConfigLoad() on a nil")
} }
// this lets the user hand edit the config if err := c.loadText(); err == nil {
if data, err := loadFile("forge.text"); err == nil { return 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?
}
}
} }
// forge.text doesn't exist. try forge.json // forge.text doesn't exist. try forge.json
@ -93,7 +82,7 @@ func (c *ForgeConfigs) ConfigLoad() error {
if len(data) != 0 { if len(data) != 0 {
if err = c.UnmarshalJSON(data); err == nil { if err = c.UnmarshalJSON(data); err == nil {
log.Info("forge.ConfigLoad()", len(c.ForgeConfigs), "entries in ~/.config/forge") 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()") log.Info("attempting forge.ConfigSave()")
c.ConfigSave() c.ConfigSave()
return nil return nil
@ -109,11 +98,33 @@ func (c *ForgeConfigs) ConfigLoad() error {
} }
// first time user. make a template config file // first time user. make a template config file
c.sampleConfig() log.Info("crap")
// c.sampleConfig()
return nil 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) { func loadFile(filename string) ([]byte, error) {
fullname := filepath.Join(os.Getenv("FORGE_CONFIG"), filename) fullname := filepath.Join(os.Getenv("FORGE_CONFIG"), filename)
data, err := os.ReadFile(fullname) data, err := os.ReadFile(fullname)