From dbde2f51b8acb4043203b5592531c6715896c800 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 11 Sep 2025 01:53:53 -0500 Subject: cleanup config file handling --- config.common.go | 337 +++++++++++++++++++++++++++++++++++++++++++++++++++++ config.go | 30 ++--- configBackup.go | 74 ------------ configDefault.go | 15 --- configLookup.go | 337 ----------------------------------------------------- init.go | 99 ++++++++++++---- patchset.config.go | 4 +- structs.go | 5 +- 8 files changed, 425 insertions(+), 476 deletions(-) create mode 100644 config.common.go delete mode 100644 configBackup.go delete mode 100644 configDefault.go delete mode 100644 configLookup.go diff --git a/config.common.go b/config.common.go new file mode 100644 index 0000000..5a230a8 --- /dev/null +++ b/config.common.go @@ -0,0 +1,337 @@ +package forgepb + +/* + lookup settings for a particular *gitpb.Repo or gopath string + + user settings are configured in ~/.config/forge/forge.text + + // searchs by string + Configs.IsReadOnly(path) // user can't push commits + Configs.IsWritable(path) // the opposite, but maybe different so I put both here + + IsPrivate(repo) // repo can't be published to the pkg.go.dev system + DebName() // for 'zookeeper' returns 'zookeeper-go' + + This code is practical, not perfect +*/ + +import ( + "path/filepath" + "strings" + + "go.wit.com/lib/protobuf/gitpb" +) + +/* +func (all *ForgeConfigs) UpdateGoPath(name string, gopath string) bool { + oldr := all.DeleteByGoPath(name) + if oldr == nil { + // nothing to update + return false + } + + // update gopath and append it back to the list + oldr.GoPath = gopath + return all.Append(oldr) +} +*/ + +// returns true if gopath is readonly() +// will attempt to match IsWritable("foo") against anything ending in "foo" +func (fc *ForgeConfigs) IsReadOnly(gopath string) bool { + var match *ForgeConfig + + all := fc.SortByGoPath() // get the list of repos + for all.Scan() { + r := all.Next() + if r.GoPath == gopath { + // exact gopath match + if r.Writable { + return false + } + if r.ReadOnly { + return true + } + // private is assumed to be r/w unless above is specifically set + if r.Private { + return false + } + } + // if gopath == "foo" will return false if "go.wit.com/apps/foo" is Writable + base := filepath.Base(r.GoPath) + if base == gopath { + if r.Writable { + return false + } + } + // search for potential dir matches + if r.Directory { + // test the dir + if strings.HasPrefix(gopath, r.GoPath) { + match = r + } + } + } + + if match == nil { + // log.Info("did not match in IsReadOnly()", gopath) + return true + } + + // take the settings from the directory match + if match.Writable { + return false + } + if match.ReadOnly { + return true + } + // private is assumed to be r/w unless above is specifically set + if match.Private { + return false + } + + // always assume readonly + return true +} + +// returns the deb package name +// this let's you check a git tag version against a package .deb version +// allows gopath's to not need to match the .deb name +// this is important in lots of cases! It is normal and happens often enough. +func (fc *ForgeConfigs) DebName(gopath string) string { + // get "zookeeper" from "go.wit.com/apps/zookeeper" + normalBase := filepath.Base(gopath) + + all := fc.SortByGoPath() + for all.Scan() { + r := all.Next() + if r.GoPath == gopath { + // returns "zookeeper-go" for "go.wit.com/apps/zookeeper" + if r.DebName != "" { + // log.Info("FOUND DebName", r.DebName) + return r.DebName + } else { + return normalBase + } + } + } + return normalBase +} + +// a work in progress +func (f *Forge) IsPrivate(repo *gitpb.Repo) bool { + namespace := repo.GetNamespace() + if namespace == "" { + // assume true + return true + } + + return f.Config.IsPrivate(namespace) +} + +// is this a non-publishable repo? +// matches package names from apt +// +// IsPrivate("foo") will match anything in the config file ending in "foo" +// +// IsPrivate("go.foo.com/jcarr/foo") returns true if private +// IsPrivate("foo") also returns true if "go.bar.com/jcarr/foo" is private +func (fc *ForgeConfigs) IsPrivate(thing string) bool { + var match *ForgeConfig + + // sort by path means the simple 'match' logic + // here works in the sense the last directory match + // is the one that is used + all := fc.SortByGoPath() // get the list of repos + for all.Scan() { + r := all.Next() + if r.GoPath == thing { + // if private is set here, then ok, otherwise + // still check if a Directory match exists + if r.Private { + return true + } + } + base := filepath.Base(r.GoPath) + if base == thing { + if r.Private { + return true + } + } + // check to see if IsPrivate("foo") + // search for potential dir matches + if r.Directory { + // test the dir + if strings.HasPrefix(thing, r.GoPath) { + match = r + } + } + } + if match == nil { + // log.Info("did not match in IsPrivate()", thing) + return false + } + + // otherwise, assume not private + return match.Private +} + +// IsFavorite() -- fun option for the config +// file that lets you set things as favorites +// so you can just go-clone a bunch of common things +// on a new box or after you reset/delete your ~/go/src dir +func (fc *ForgeConfigs) IsFavorite(thing string) bool { + var match *ForgeConfig + + all := fc.SortByGoPath() // get the list of repos + for all.Scan() { + r := all.Next() + if r.GoPath == thing { + if r.Favorite { + return true + } + } + base := filepath.Base(r.GoPath) + if base == thing { + if r.Favorite { + return true + } + } + if r.Directory { + if strings.HasPrefix(thing, r.GoPath) { + match = r + } + } + } + if match == nil { + return false + } + + return match.Favorite +} + +// IsWritable() checks your .config/forge/ settings +// looks for an exact match, then +// looks for a directory match +func (fc *ForgeConfigs) IsWritable(thing string) bool { + var match *ForgeConfig + + all := fc.SortByGoPath() // get the list of repos + for all.Scan() { + r := all.Next() + if r.GoPath == thing { + if r.Writable { + return true + } + } + base := filepath.Base(r.GoPath) + if base == thing { + if r.Writable { + return true + } + } + if r.Directory { + if strings.HasPrefix(thing, r.GoPath) { + match = r + } + } + } + if match == nil { + return false + } + + return match.Writable +} + +// allows custom user branch names in the forge config +func (fc *ForgeConfigs) FindUserBranch(thing string) string { + var match *ForgeConfig + + all := fc.SortByGoPath() // get the list of repos + for all.Scan() { + r := all.Next() + if r.GoPath == thing { + if r.UserBranchName != "" { + return r.UserBranchName + } + } + base := filepath.Base(r.GoPath) + if base == thing { + if r.UserBranchName != "" { + return r.UserBranchName + } + } + if r.Directory { + if strings.HasPrefix(thing, r.GoPath) { + match = r + } + } + } + if match == nil { + return "" + } + + return match.UserBranchName +} + +// allows custom devel branch names in the forge config +func (fc *ForgeConfigs) FindDevelBranch(thing string) string { + var match *ForgeConfig + + all := fc.SortByGoPath() // get the list of repos + for all.Scan() { + r := all.Next() + if r.GoPath == thing { + if r.DevelBranchName != "" { + return r.DevelBranchName + } + } + base := filepath.Base(r.GoPath) + if base == thing { + if r.DevelBranchName != "" { + return r.DevelBranchName + } + } + if r.Directory { + if strings.HasPrefix(thing, r.GoPath) { + match = r + } + } + } + if match == nil { + return "" + } + + return match.DevelBranchName +} + +// allows custom devel branch names in the forge config +func (fc *ForgeConfigs) FindMasterBranch(thing string) string { + var match *ForgeConfig + + all := fc.SortByGoPath() // get the list of repos + for all.Scan() { + r := all.Next() + if r.GoPath == thing { + if r.MasterBranchName != "" { + return r.MasterBranchName + } + } + base := filepath.Base(r.GoPath) + if base == thing { + if r.MasterBranchName != "" { + return r.MasterBranchName + } + } + if r.Directory { + if strings.HasPrefix(thing, r.GoPath) { + match = r + } + } + } + if match == nil { + return "" + } + + return match.MasterBranchName +} diff --git a/config.go b/config.go index a6cfd4a..963202f 100644 --- a/config.go +++ b/config.go @@ -18,13 +18,15 @@ import ( func (f *Forge) ConfigSave() error { var err error - // backup the current config files - if e := f.backupConfig(); e != nil { - log.Info("forge.BackupConfig() error", e) - err = e - // continue here? notsure. could be bad either way - // out of disk space? - } + /* + // backup the current config files + if e := f.backupConfig(); e != nil { + log.Info("forge.BackupConfig() error", e) + err = e + // continue here? notsure. could be bad either way + // out of disk space? + } + */ if f.Config != nil { if e := f.Config.ConfigSave(); e != nil { log.Info("forge.Config.ConfigSave() error", e) @@ -101,9 +103,6 @@ func (c *ForgeConfigs) ConfigLoad(fullpath string) error { log.Info("However, the config files could not be loaded") } - // first time user. make a template config file - c.sampleConfig() - return nil } @@ -161,17 +160,6 @@ func configWrite(filename string, data []byte) error { cfgfile.Write([]byte("# git repos you have write access to. That is, where you can run 'git push'\n")) cfgfile.Write([]byte("\n")) } - if filename == "forge.json" { - // add header - 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([]byte("\n")) - cfgfile.Write([]byte("# this file is parsed only if forge.text is missing\n")) - cfgfile.Write([]byte("# also, these comment lines don't work in json files and have to be removed for Marshal() to work\n")) - cfgfile.Write([]byte("# probably, JSON syntax for this is just going to be deprecated for the TEXT syntax\n")) - cfgfile.Write([]byte("\n")) - } cfgfile.Write(data) return nil } diff --git a/configBackup.go b/configBackup.go deleted file mode 100644 index b1c9d0d..0000000 --- a/configBackup.go +++ /dev/null @@ -1,74 +0,0 @@ -package forgepb - -// thank chatgpt for this because why. why write this if you can have it -// kick this out in 30 seconds - -import ( - "errors" - "fmt" - "io" - "os" - "path/filepath" - "time" -) - -func (f *Forge) backupConfig() error { - // make a new dir to backup the files - srcDir := filepath.Join(f.configDir) - destDir := filepath.Join(f.configDir, "backup") - return backupFiles(srcDir, destDir) -} - -func backupFiles(srcDir string, destDir string) error { - // Create the destination directory - err := os.MkdirAll(destDir, os.ModePerm) - if err != nil { - return errors.New(fmt.Sprintf("Failed to create directory: %v", err)) - } - - // Read the contents of the source directory - entries, err := os.ReadDir(srcDir) - if err != nil { - return errors.New(fmt.Sprintf("Failed to read directory: %v", err)) - } - - // Iterate over the entries in the source directory - for _, entry := range entries { - // Skip directories and files that do not have the .test extension - if entry.IsDir() { - continue - } - - // log.Println("backing up file", entry.Name()) - srcPath := filepath.Join(srcDir, entry.Name()) - destPath := filepath.Join(destDir, entry.Name()) - - // Copy the file - if err := copyFile(srcPath, destPath); err != nil { - return errors.New(fmt.Sprintf("Failed to copy file %s: %v", entry.Name(), err)) - } - } - return nil -} - -// copyFile copies a file from src to dest -func copyFile(src, dest string) error { - srcFile, err := os.Open(src) - if err != nil { - return err - } - defer srcFile.Close() - - now := time.Now() - timestamp := now.Format("2006.01.02.150405") // bummer. other date doesn't work? - dest = dest + timestamp - destFile, err := os.Create(dest) - if err != nil { - return err - } - defer destFile.Close() - - // Copy the content - _, err = io.Copy(destFile, srcFile) - return err -} diff --git a/configDefault.go b/configDefault.go deleted file mode 100644 index 3246798..0000000 --- a/configDefault.go +++ /dev/null @@ -1,15 +0,0 @@ -package forgepb - -import ( - "fmt" -) - -func (all *ForgeConfigs) sampleConfig() { - new1 := new(ForgeConfig) - new1.GoPath = "go.wit.com" - new1.Writable = true - new1.Directory = true - all.Append(new1) - - fmt.Println("first time user. adding an example config file with", len(all.ForgeConfigs), "repos") -} diff --git a/configLookup.go b/configLookup.go deleted file mode 100644 index 5a230a8..0000000 --- a/configLookup.go +++ /dev/null @@ -1,337 +0,0 @@ -package forgepb - -/* - lookup settings for a particular *gitpb.Repo or gopath string - - user settings are configured in ~/.config/forge/forge.text - - // searchs by string - Configs.IsReadOnly(path) // user can't push commits - Configs.IsWritable(path) // the opposite, but maybe different so I put both here - - IsPrivate(repo) // repo can't be published to the pkg.go.dev system - DebName() // for 'zookeeper' returns 'zookeeper-go' - - This code is practical, not perfect -*/ - -import ( - "path/filepath" - "strings" - - "go.wit.com/lib/protobuf/gitpb" -) - -/* -func (all *ForgeConfigs) UpdateGoPath(name string, gopath string) bool { - oldr := all.DeleteByGoPath(name) - if oldr == nil { - // nothing to update - return false - } - - // update gopath and append it back to the list - oldr.GoPath = gopath - return all.Append(oldr) -} -*/ - -// returns true if gopath is readonly() -// will attempt to match IsWritable("foo") against anything ending in "foo" -func (fc *ForgeConfigs) IsReadOnly(gopath string) bool { - var match *ForgeConfig - - all := fc.SortByGoPath() // get the list of repos - for all.Scan() { - r := all.Next() - if r.GoPath == gopath { - // exact gopath match - if r.Writable { - return false - } - if r.ReadOnly { - return true - } - // private is assumed to be r/w unless above is specifically set - if r.Private { - return false - } - } - // if gopath == "foo" will return false if "go.wit.com/apps/foo" is Writable - base := filepath.Base(r.GoPath) - if base == gopath { - if r.Writable { - return false - } - } - // search for potential dir matches - if r.Directory { - // test the dir - if strings.HasPrefix(gopath, r.GoPath) { - match = r - } - } - } - - if match == nil { - // log.Info("did not match in IsReadOnly()", gopath) - return true - } - - // take the settings from the directory match - if match.Writable { - return false - } - if match.ReadOnly { - return true - } - // private is assumed to be r/w unless above is specifically set - if match.Private { - return false - } - - // always assume readonly - return true -} - -// returns the deb package name -// this let's you check a git tag version against a package .deb version -// allows gopath's to not need to match the .deb name -// this is important in lots of cases! It is normal and happens often enough. -func (fc *ForgeConfigs) DebName(gopath string) string { - // get "zookeeper" from "go.wit.com/apps/zookeeper" - normalBase := filepath.Base(gopath) - - all := fc.SortByGoPath() - for all.Scan() { - r := all.Next() - if r.GoPath == gopath { - // returns "zookeeper-go" for "go.wit.com/apps/zookeeper" - if r.DebName != "" { - // log.Info("FOUND DebName", r.DebName) - return r.DebName - } else { - return normalBase - } - } - } - return normalBase -} - -// a work in progress -func (f *Forge) IsPrivate(repo *gitpb.Repo) bool { - namespace := repo.GetNamespace() - if namespace == "" { - // assume true - return true - } - - return f.Config.IsPrivate(namespace) -} - -// is this a non-publishable repo? -// matches package names from apt -// -// IsPrivate("foo") will match anything in the config file ending in "foo" -// -// IsPrivate("go.foo.com/jcarr/foo") returns true if private -// IsPrivate("foo") also returns true if "go.bar.com/jcarr/foo" is private -func (fc *ForgeConfigs) IsPrivate(thing string) bool { - var match *ForgeConfig - - // sort by path means the simple 'match' logic - // here works in the sense the last directory match - // is the one that is used - all := fc.SortByGoPath() // get the list of repos - for all.Scan() { - r := all.Next() - if r.GoPath == thing { - // if private is set here, then ok, otherwise - // still check if a Directory match exists - if r.Private { - return true - } - } - base := filepath.Base(r.GoPath) - if base == thing { - if r.Private { - return true - } - } - // check to see if IsPrivate("foo") - // search for potential dir matches - if r.Directory { - // test the dir - if strings.HasPrefix(thing, r.GoPath) { - match = r - } - } - } - if match == nil { - // log.Info("did not match in IsPrivate()", thing) - return false - } - - // otherwise, assume not private - return match.Private -} - -// IsFavorite() -- fun option for the config -// file that lets you set things as favorites -// so you can just go-clone a bunch of common things -// on a new box or after you reset/delete your ~/go/src dir -func (fc *ForgeConfigs) IsFavorite(thing string) bool { - var match *ForgeConfig - - all := fc.SortByGoPath() // get the list of repos - for all.Scan() { - r := all.Next() - if r.GoPath == thing { - if r.Favorite { - return true - } - } - base := filepath.Base(r.GoPath) - if base == thing { - if r.Favorite { - return true - } - } - if r.Directory { - if strings.HasPrefix(thing, r.GoPath) { - match = r - } - } - } - if match == nil { - return false - } - - return match.Favorite -} - -// IsWritable() checks your .config/forge/ settings -// looks for an exact match, then -// looks for a directory match -func (fc *ForgeConfigs) IsWritable(thing string) bool { - var match *ForgeConfig - - all := fc.SortByGoPath() // get the list of repos - for all.Scan() { - r := all.Next() - if r.GoPath == thing { - if r.Writable { - return true - } - } - base := filepath.Base(r.GoPath) - if base == thing { - if r.Writable { - return true - } - } - if r.Directory { - if strings.HasPrefix(thing, r.GoPath) { - match = r - } - } - } - if match == nil { - return false - } - - return match.Writable -} - -// allows custom user branch names in the forge config -func (fc *ForgeConfigs) FindUserBranch(thing string) string { - var match *ForgeConfig - - all := fc.SortByGoPath() // get the list of repos - for all.Scan() { - r := all.Next() - if r.GoPath == thing { - if r.UserBranchName != "" { - return r.UserBranchName - } - } - base := filepath.Base(r.GoPath) - if base == thing { - if r.UserBranchName != "" { - return r.UserBranchName - } - } - if r.Directory { - if strings.HasPrefix(thing, r.GoPath) { - match = r - } - } - } - if match == nil { - return "" - } - - return match.UserBranchName -} - -// allows custom devel branch names in the forge config -func (fc *ForgeConfigs) FindDevelBranch(thing string) string { - var match *ForgeConfig - - all := fc.SortByGoPath() // get the list of repos - for all.Scan() { - r := all.Next() - if r.GoPath == thing { - if r.DevelBranchName != "" { - return r.DevelBranchName - } - } - base := filepath.Base(r.GoPath) - if base == thing { - if r.DevelBranchName != "" { - return r.DevelBranchName - } - } - if r.Directory { - if strings.HasPrefix(thing, r.GoPath) { - match = r - } - } - } - if match == nil { - return "" - } - - return match.DevelBranchName -} - -// allows custom devel branch names in the forge config -func (fc *ForgeConfigs) FindMasterBranch(thing string) string { - var match *ForgeConfig - - all := fc.SortByGoPath() // get the list of repos - for all.Scan() { - r := all.Next() - if r.GoPath == thing { - if r.MasterBranchName != "" { - return r.MasterBranchName - } - } - base := filepath.Base(r.GoPath) - if base == thing { - if r.MasterBranchName != "" { - return r.MasterBranchName - } - } - if r.Directory { - if strings.HasPrefix(thing, r.GoPath) { - match = r - } - } - } - if match == nil { - return "" - } - - return match.MasterBranchName -} diff --git a/init.go b/init.go index 5205e4b..a8fc63c 100644 --- a/init.go +++ b/init.go @@ -3,14 +3,10 @@ package forgepb import ( - "errors" "os" "os/user" - "path/filepath" - "time" "go.wit.com/lib/fhelp" - "go.wit.com/lib/gui/shell" "go.wit.com/lib/protobuf/gitpb" "go.wit.com/log" ) @@ -25,15 +21,10 @@ func Default(opts ...OptionFunc) *Engine { } */ +/* func Init() *Forge { f := InitPB() - /* - f.Machine = new(zoopb.Machine) - if err := f.Machine.ConfigLoad(); err != nil { - log.Log(WARN, "zoopb.ConfigLoad() failed", err) - } - */ if f.Config.Username == "" { usr, _ := user.Current() f.Config.Username = usr.Username @@ -82,24 +73,31 @@ func Init() *Forge { log.Log(INFO, "update() check took", shell.FormatDuration(time.Since(now))) return f } +*/ -func FirstTimeUser() bool { - if checkenv() { - return false - } - - // setup the env +func InitFromConfig(cfg *ForgeConfigs) *Forge { f := new(Forge) - f.setenv() + f.Config = cfg + if f.configENV() { + log.Info("ENV changed config. todo: save config here") + } - fullname := filepath.Join(os.Getenv("FORGE_CONFIG"), "forge.text") - _, err := os.ReadFile(fullname) - if errors.Is(err, os.ErrNotExist) { - return true + f.Repos = gitpb.NewRepos() + f.Repos.ConfigLoad() + if f.Repos.HasFullScan { + f.hasFullScan = true } - return false + + // init the Patchsets + f.Patchsets = NewPatchsets() + + // todo: play with these / determine good values based on user's machine + f.rillX = 10 + f.rillY = 20 + return f } +/* func (f *Forge) InitPB() { f.setenv() @@ -122,6 +120,7 @@ func (f *Forge) InitPB() { f.rillX = 10 f.rillY = 20 } +*/ func (f *Forge) InitMachine() { if f.Config.Username == "" { @@ -132,6 +131,7 @@ func (f *Forge) InitMachine() { // log.Info(hostname, err) } +/* // only init's the protobuf. intended to not scan or change anything func InitPB() *Forge { f := new(Forge) @@ -139,6 +139,7 @@ func InitPB() *Forge { f.InitPB() return f } +*/ func (f *Forge) SetConfigSave(b bool) { f.configSave = b @@ -154,6 +155,7 @@ func (f *Forge) Exit() { os.Exit(0) } +/* func RawInitPB() *Forge { f := new(Forge) f.RawInitPB() @@ -163,6 +165,7 @@ func RawInitPB() *Forge { func (f *Forge) RawInitPB() { f.InitPB() } +*/ // the first thing done is process any ENV settings // try to NOT use the ENV settings anywhere but here @@ -173,15 +176,20 @@ func (f *Forge) setenv() { log.Info("forge ConfigureENV() failed", err) os.Exit(-1) } + if f.Config == nil { + log.Info("forge.Config() was nil") + os.Exit(-1) + } f.configDir = os.Getenv("FORGE_CONFIG") f.goSrc = os.Getenv("FORGE_GOSRC") - f.repoPB = os.Getenv("FORGE_REPOPB") f.forgeURL = os.Getenv("FORGE_URL") - f.patchDir = os.Getenv("FORGE_PATCHDIR") f.hostname = os.Getenv("HOSTNAME") if os.Getenv("FORGE_GOWORK") == "true" { f.goWork = true } + + f.Config.ReposPB = os.Getenv("FORGE_REPOPB") + f.Config.PatchDir = os.Getenv("FORGE_PATCHDIR") }) } @@ -224,3 +232,46 @@ func (f *Forge) SetForgeURL(url string) { os.Setenv("FORGE_URL", f.forgeURL) log.Info("Forge URL has been set to", f.forgeURL) } + +// the first thing done is process any ENV settings +// try to NOT use the ENV settings anywhere but here +// all initial ENV settings should be stored in the forge struct +func (f *Forge) configENV() bool { + var changed bool + f.once.Do(func() { + if err := fhelp.ConfigureENV(); err != nil { + log.Info("forge ConfigureENV() failed", err) + os.Exit(-1) + } + if os.Getenv("FORGE_REPOPB") != "" && f.Config.ReposPB != os.Getenv("FORGE_REPOPB") { + log.Info("ENV: updating FORGE_REPOSPB from", f.Config.ReposPB, "to", os.Getenv("FORGE_REPOPB")) + f.Config.ReposPB = os.Getenv("FORGE_REPOPB") + changed = true + } + + if os.Getenv("FORGE_GOSRC") != "" && f.Config.ReposDir != os.Getenv("FORGE_GOSRC") { + log.Info("ENV: updating FORGE_GOSRC from", f.Config.ReposDir, "to", os.Getenv("FORGE_GOSRC")) + f.Config.ReposDir = os.Getenv("FORGE_GOSRC") + changed = true + } + + if os.Getenv("FORGE_PATCHDIR") != "" && f.Config.PatchDir != os.Getenv("FORGE_PATCHDIR") { + log.Info("ENV: updating FORGE_PATCHDIR from", f.Config.PatchDir, "to", os.Getenv("FORGE_PATCHDIRC")) + f.Config.PatchDir = os.Getenv("FORGE_PATCHDIR") + changed = true + } + + f.configDir = os.Getenv("FORGE_CONFIG") + f.goSrc = os.Getenv("FORGE_GOSRC") + f.forgeURL = os.Getenv("FORGE_URL") + // f.patchDir = os.Getenv("FORGE_PATCHDIR") + f.hostname = os.Getenv("HOSTNAME") + if os.Getenv("FORGE_GOWORK") == "true" { + f.goWork = true + } + }) + if changed { + // save config here + } + return changed +} diff --git a/patchset.config.go b/patchset.config.go index fd9b509..06a59da 100644 --- a/patchset.config.go +++ b/patchset.config.go @@ -16,7 +16,7 @@ import ( func (f *Forge) LoadPatchsets() error { f.Patchsets = NewPatchsets() - filename := filepath.Join(f.patchDir, "all-patches.pb") + filename := filepath.Join(f.Config.PatchDir, "all-patches.pb") data, err := os.ReadFile(filename) if err != nil { @@ -45,7 +45,7 @@ func (f *Forge) InitPatchsets() error { } func (f *Forge) SavePatchsets() error { - filename := filepath.Join(f.patchDir, "all-patches.pb") + filename := filepath.Join(f.Config.PatchDir, "all-patches.pb") regfile, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666) if err != nil { log.Info("SavePatchsets() filename open error:", filename, err) diff --git a/structs.go b/structs.go index 97e088d..99d5172 100644 --- a/structs.go +++ b/structs.go @@ -13,8 +13,6 @@ type Forge struct { once sync.Once initErr error // init error, if any goSrc string // the path to go/src - repoPB string // the path to the repos.pb cache file - configDir string // normally ~/.config/forge goWork bool // means the user is currently using a go.work file Config *ForgeConfigs // config repos for readonly, private, etc Repos *gitpb.Repos // the repo protobufs @@ -26,7 +24,8 @@ type Forge struct { rillX int // used for Rill() rillY int // used for Rill() Patchsets *Patchsets // patches that are in progress - patchDir string // where patches are stored + configDir string // normally ~/.config/forge + // patchDir string // where patches are stored } func (f *Forge) GetGoSrc() string { -- cgit v1.2.3