making config files more viable

This commit is contained in:
Jeff Carr 2024-12-05 12:37:24 -06:00
parent 77cd0ae36e
commit 205e6f022b
2 changed files with 25 additions and 11 deletions

View File

@ -11,12 +11,12 @@ import (
"go.wit.com/log"
)
// write to ~/.config/forge/ unless ENV{FORGE_HOME} is set
// write to ~/.config/forge/ unless ENV{FORGE_CONFIG} is set
func (all *Repos) ConfigSave() error {
if os.Getenv("FORGE_HOME") == "" {
if os.Getenv("FORGE_CONFIG") == "" {
homeDir, _ := os.UserHomeDir()
fullpath := filepath.Join(homeDir, ".config/forge")
os.Setenv("FORGE_HOME", fullpath)
os.Setenv("FORGE_CONFIG", fullpath)
}
if all == nil {
log.Warn("gitpb all == nil")
@ -35,10 +35,10 @@ func (all *Repos) ConfigSave() error {
// load the ~/.config/forge/ files
func (all *Repos) ConfigLoad() error {
if os.Getenv("FORGE_HOME") == "" {
if os.Getenv("FORGE_CONFIG") == "" {
homeDir, _ := os.UserHomeDir()
fullpath := filepath.Join(homeDir, ".config/forge")
os.Setenv("FORGE_HOME", fullpath)
os.Setenv("FORGE_CONFIG", fullpath)
}
var data []byte
var err error
@ -52,19 +52,33 @@ func (all *Repos) ConfigLoad() error {
if len(data) == 0 {
// todo: error out if the file is empty?
// try forge.text & forge.json?
log.Warn("gitpb.ConfigLoad() repos.pb is empty")
return errors.New("gitpb.ConfigLoad() repos.pb is empty")
}
if all.Repos == nil {
log.Warn("gitpb.ConfigLoad() all.Repos == nil")
} else {
log.Warn("gitpb.ConfigLoad() all.Repos.Len()", all.Len())
}
if err = all.Unmarshal(data); err != nil {
log.Warn("broken forge.pb config file")
log.Warn("gitpb.ConfigLoad() failed", err)
if all.Repos == nil {
log.Warn("gitpb.ConfigLoad() all.Repos == nil")
} else {
log.Warn("gitpb.ConfigLoad() all.Repos.Len()", all.Len())
log.Warn("gitpb.ConfigLoad() trying to resave the file")
all.ConfigSave()
}
return err
}
log.Info("found", len(all.Repos), "repos in ~/.config/forge/repos.pb")
log.Info("gitpb.Init()", len(all.Repos), "repos in ~/.config/forge/repos.pb")
return nil
}
return nil
}
func loadFile(filename string) ([]byte, error) {
fullname := filepath.Join(os.Getenv("FORGE_HOME"), filename)
fullname := filepath.Join(os.Getenv("FORGE_CONFIG"), filename)
data, err := os.ReadFile(fullname)
if errors.Is(err, os.ErrNotExist) {
// if file does not exist, just return nil. this
@ -80,7 +94,7 @@ func loadFile(filename string) ([]byte, error) {
}
func configWrite(filename string, data []byte) error {
fullname := filepath.Join(os.Getenv("FORGE_HOME"), filename)
fullname := filepath.Join(os.Getenv("FORGE_CONFIG"), filename)
cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666)
defer cfgfile.Close()

View File

@ -36,14 +36,14 @@ func (a GitTagAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
// rill is awesome. long live rill
// attempt scan with rill
func (all *Repos) rillGitPull() error {
func (all *Repos) RillGitPull() error {
loop := all.SortByGoPath()
for loop.Scan() {
t := loop.Next()
log.Info("repo", t.GoPath)
}
/*
packs := all.selectAllRepo()
packs := all.SortByGoPath()
// Convert a slice of user IDs into a channel
ids := rill.FromSlice(packs, nil)