pass the full filename to ConfigSave()

This commit is contained in:
Jeff Carr 2025-09-12 10:13:59 -05:00
parent e793c89712
commit 62e5fc396c
1 changed files with 10 additions and 23 deletions

View File

@ -13,35 +13,34 @@ import (
) )
// write to ~/.config/forge/ unless ENV{FORGE_REPOSDIR} is set // write to ~/.config/forge/ unless ENV{FORGE_REPOSDIR} is set
func (all *Repos) ConfigSave() error { func (all *Repos) ConfigSave(fname string) error {
if os.Getenv("FORGE_REPOSDIR") == "" {
homeDir, _ := os.UserHomeDir()
fullpath := filepath.Join(homeDir, ".config/forge")
os.Setenv("FORGE_REPOSDIR", fullpath)
}
if all == nil { if all == nil {
log.Warn("gitpb all == nil") log.Warn("gitpb repos == nil")
return errors.New("gitpb.ConfigSave() all == nil") return errors.New("gitpb.ConfigSave() repos == nil")
} }
data, err := all.Marshal() data, err := all.Marshal()
if err != nil { if err != nil {
log.Info("gitpb proto.Marshal() failed len", len(data), err) log.Info("gitpb proto.Marshal() failed len", len(data), err)
// often this is because strings have invalid UTF-8. This should probably be fixed in the protobuf code // often this is because strings have invalid UTF-8. This should probably be fixed in the protobuf code
// this might be fixed in the create code, but it can't hurt to try this as a last ditch effort here
log.Info("gitpb.ConfigSave() ATTEMPTING TO VALIDATE UTF-8 strings in the protobuf file")
if err := all.tryValidate(); err != nil { if err := all.tryValidate(); err != nil {
log.Info("gitpb.ConfigSave() STILL FAILEd", err)
return err return err
} else { } else {
// re-attempt Marshal() here // re-attempt Marshal() here
data, err = all.Marshal() data, err = all.Marshal()
if err == nil { if err == nil {
// validate & sanitize strings worked // validate & sanitize strings worked
configWrite("repos.pb", data) configWrite(fname, data)
return nil return nil
} }
log.Info("gitpb.ConfigSave() STILL FAILEd", err)
} }
return err return err
} }
configWrite("repos.pb", data) configWrite(fname, data)
return nil return nil
} }
@ -172,9 +171,7 @@ func loadFile(fullname string) ([]byte, error) {
return data, nil return data, nil
} }
func configWrite(filename string, data []byte) error { func configWrite(fullname string, data []byte) error {
fullname := filepath.Join(os.Getenv("FORGE_REPOSDIR"), filename)
log.Infof("%s your repos have changed state. cached state. (%d) bytes\n", fullname, len(data)) log.Infof("%s your repos have changed state. cached state. (%d) bytes\n", fullname, len(data))
cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
defer cfgfile.Close() defer cfgfile.Close()
@ -182,16 +179,6 @@ func configWrite(filename string, data []byte) error {
log.Warn("open config file :", err) log.Warn("open config file :", err)
return err return err
} }
if filename == "forge.text" {
// add header
cfgfile.Write([]byte("# this file is automatically re-generated from forge.pb, however,\n"))
cfgfile.Write([]byte("# if you want to edit it by hand, you can:\n"))
cfgfile.Write([]byte("# stop forge; remove forge.pb; edit forge.text; start forge\n"))
cfgfile.Write([]byte("# this will cause the default behavior to fallback to parsing this file for the config\n"))
cfgfile.Write([]byte("\n"))
cfgfile.Write([]byte("# this file is intended to be used to customize settings on what\n"))
cfgfile.Write([]byte("# git repos you have write access to. That is, where you can run 'git push'\n"))
}
cfgfile.Write(data) cfgfile.Write(data)
return nil return nil
} }