pass the full filename to ConfigSave()
This commit is contained in:
parent
e793c89712
commit
62e5fc396c
33
config.go
33
config.go
|
@ -13,35 +13,34 @@ import (
|
|||
)
|
||||
|
||||
// write to ~/.config/forge/ unless ENV{FORGE_REPOSDIR} is set
|
||||
func (all *Repos) ConfigSave() error {
|
||||
if os.Getenv("FORGE_REPOSDIR") == "" {
|
||||
homeDir, _ := os.UserHomeDir()
|
||||
fullpath := filepath.Join(homeDir, ".config/forge")
|
||||
os.Setenv("FORGE_REPOSDIR", fullpath)
|
||||
}
|
||||
func (all *Repos) ConfigSave(fname string) error {
|
||||
if all == nil {
|
||||
log.Warn("gitpb all == nil")
|
||||
return errors.New("gitpb.ConfigSave() all == nil")
|
||||
log.Warn("gitpb repos == nil")
|
||||
return errors.New("gitpb.ConfigSave() repos == nil")
|
||||
}
|
||||
|
||||
data, err := all.Marshal()
|
||||
if err != nil {
|
||||
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
|
||||
// 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 {
|
||||
log.Info("gitpb.ConfigSave() STILL FAILEd", err)
|
||||
return err
|
||||
} else {
|
||||
// re-attempt Marshal() here
|
||||
data, err = all.Marshal()
|
||||
if err == nil {
|
||||
// validate & sanitize strings worked
|
||||
configWrite("repos.pb", data)
|
||||
configWrite(fname, data)
|
||||
return nil
|
||||
}
|
||||
log.Info("gitpb.ConfigSave() STILL FAILEd", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
configWrite("repos.pb", data)
|
||||
configWrite(fname, data)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -172,9 +171,7 @@ func loadFile(fullname string) ([]byte, error) {
|
|||
return data, nil
|
||||
}
|
||||
|
||||
func configWrite(filename string, data []byte) error {
|
||||
fullname := filepath.Join(os.Getenv("FORGE_REPOSDIR"), filename)
|
||||
|
||||
func configWrite(fullname string, data []byte) error {
|
||||
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)
|
||||
defer cfgfile.Close()
|
||||
|
@ -182,16 +179,6 @@ func configWrite(filename string, data []byte) error {
|
|||
log.Warn("open config file :", 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)
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue