From 0f895e83e62749bb1d716033f5f300d9d1c548ac Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 13 Sep 2025 05:33:11 -0500 Subject: [PATCH] use config GO library --- branches.go | 17 ++++++++--------- config.go | 6 ------ forgeConfig.config.go | 4 +++- goSrcScan.go | 27 +++++++++++++++++++++++++-- humanTable.go | 2 +- init.go | 38 ++++++++++++++++++++++---------------- repoNew.go | 4 +++- rill.go | 7 +------ structs.go | 17 ++++++++--------- 9 files changed, 71 insertions(+), 51 deletions(-) diff --git a/branches.go b/branches.go index a503a96..0a13361 100644 --- a/branches.go +++ b/branches.go @@ -1,10 +1,10 @@ package forgepb import ( - "os" "path/filepath" "time" + "go.wit.com/lib/config" "go.wit.com/lib/gui/shell" "go.wit.com/lib/protobuf/gitpb" "go.wit.com/log" @@ -111,13 +111,12 @@ func rillCheckoutMaster(repo *gitpb.Repo) error { // 'giterr' means something is very wrong with this repo if repo.GetMasterVersion() == "giterr" { repo.CheckoutMaster() - log.Info("master == giterr. BAD REPO", repo.GetFullPath()) - log.Info("master == giterr. BAD REPO", repo.GetFullPath()) - log.Info("master == giterr. BAD REPO", repo.GetFullPath()) - cmd := []string{"git", "checkout", "main"} // todo: figure out main - repo.RunVerbose(cmd) - os.Exit(-1) - return nil + log.Info(repo.GetFullPath(), "master == giterr. BAD REPO") + log.Info(repo.GetFullPath(), "master == giterr. BAD REPO git describe --tags master --always") + log.Info(repo.GetFullPath(), "master == giterr. BAD REPO. todo: figure this out in rillCheckoutMaster()") + // cmd := []string{"git", "checkout", "main"} // todo: figure out main + // repo.RunVerbose(cmd) + return log.Errorf("master version can not be determined") } if repo.GetCurrentBranchName() == repo.GetMasterBranchName() { // repo is already on master @@ -245,7 +244,7 @@ func (f *Forge) DoAllCheckoutDevelNew(force bool) error { counter += 1 } log.Info("reloaded", counter, "repos") - f.configSave = true + config.SetChanged("repos", true) return nil } diff --git a/config.go b/config.go index 7cb93fc..1eff48c 100644 --- a/config.go +++ b/config.go @@ -4,7 +4,6 @@ package forgepb import ( "go.wit.com/lib/config" - "go.wit.com/lib/fhelp" "go.wit.com/lib/gui/prep" "go.wit.com/log" ) @@ -17,11 +16,6 @@ func (f *Forge) ConfigSave() error { return log.Errorf("forge.Config == nil") } - if f.Config.Mode != ForgeMode_NORMAL { - fhelp.DumpENV("forge:") - f.Config.DumpENV() - } - if config.HasChanged("forge") { // only let forge save the config files (?) diff --git a/forgeConfig.config.go b/forgeConfig.config.go index 8916329..6b7f084 100644 --- a/forgeConfig.config.go +++ b/forgeConfig.config.go @@ -3,6 +3,8 @@ package forgepb import ( + "os" + "go.wit.com/lib/config" "go.wit.com/log" ) @@ -26,7 +28,7 @@ func (cfg *ForgeConfigs) ConfigSave() error { } func (cfg *ForgeConfigs) DumpENV() { - log.Info("todo: DumpENV()") + log.Infof("RepoPB file: cfg.ReposPB=%s FORGE_REPOSPB=%s\n", cfg.ReposPB, os.Getenv("FORGE_REPOSPB")) } // load the ~/.config/forge/ files diff --git a/goSrcScan.go b/goSrcScan.go index c55bb6e..4597d0a 100644 --- a/goSrcScan.go +++ b/goSrcScan.go @@ -15,12 +15,36 @@ import ( func reloadCheck(repo *gitpb.Repo) error { if err := repo.ReloadCheck(); err != nil { log.Infof("%s reload() says %v\n", repo.FullPath, err) - // configSave = true return err } return nil } +func (f *Forge) TestScan() error { + f.Repos = gitpb.NewRepos() + dirs, err := gitDirectoriesNew(f.Config.ReposDir) + if err != nil { + return err + } + for i, fullpath := range dirs { + repo, err := gitpb.NewRepo(fullpath) + if err != nil { + log.Info("ReAdd() error", fullpath, err) + } + log.Info(i, "worked", repo.FullPath) + repo = f.Repos.Append(repo) + f.VerifyBranchNames(repo) + if f.Config.IsReadOnly(repo.GetGoPath()) { + repo.ReadOnly = true + } + repo.ReloadCheck() + if i > 5 { + break + } + } + return nil +} + func (f *Forge) ScanGoSrc() (bool, error) { dirs, err := gitDirectoriesNew(f.Config.ReposDir) if err != nil { @@ -220,6 +244,5 @@ func (f *Forge) ReAdd(repo *gitpb.Repo) (*gitpb.Repo, error) { log.Info("ReAdd() error", fullpath, err) return nil, err } - f.configSave = true return repo, err } diff --git a/humanTable.go b/humanTable.go index ea2ce59..8ececc2 100644 --- a/humanTable.go +++ b/humanTable.go @@ -215,7 +215,7 @@ func (f *Forge) printRepoToTable(repo *gitpb.Repo, sizes []int, full bool) { end += "(u:" + repo.GetUserBranchName() + ") " } - debname := f.Config.DebName(repo.GetGoPath()) + debname := f.Config.DebName(repo.GetNamespace()) if debname != filepath.Base(gopath) { end += "(deb:" + debname + ") " } diff --git a/init.go b/init.go index 49f04a0..f14b658 100644 --- a/init.go +++ b/init.go @@ -4,6 +4,7 @@ package forgepb import ( "os" + "path/filepath" "go.wit.com/lib/config" "go.wit.com/lib/fhelp" @@ -12,13 +13,14 @@ import ( ) /* better syntax from gin + Default returns an Engine instance with the Logger and Recovery middleware already attached. func Default(opts ...OptionFunc) *Engine { - debugPrintWARNINGDefault() engine := New() engine.Use(Logger(), Recovery()) return engine.With(opts...) } + */ func Init() *Forge { @@ -58,6 +60,18 @@ func initFromConfig(cfg *ForgeConfigs) *Forge { log.Info("ENV changed config. todo: save config here") f.Config.ConfigSave() } + if f.Config.ReposPB != os.Getenv("FORGE_REPOSPB") { + // if different, use the ENV var + // this probably means that it gets saved as the default in the config + // we probably want that (?) + f.Config.ReposPB = os.Getenv("FORGE_REPOSPB") + } + if _, s := filepath.Split(f.Config.ReposPB); s != "repos.pb" { + fhelp.DumpENV("forge:") + f.Config.DumpENV() + log.Infof("ReposPB invalid filename '%s'\n", f.Config.ReposPB) + os.Exit(-1) + } f.Repos = gitpb.NewRepos() f.Repos.ConfigLoad(f.Config.ReposPB) @@ -71,29 +85,22 @@ func initFromConfig(cfg *ForgeConfigs) *Forge { return f } -/* -func (f *Forge) InitMachine() { - if f.Config.Username == "" { - usr, _ := user.Current() - f.Config.Username = usr.Username - } - f.hostname, _ = os.Hostname() - // log.Info(hostname, err) -} -*/ - func (f *Forge) SetConfigSave(b bool) { - f.configSave = b + config.SetChanged("forge", b) } // saves the config if there have been changes func (f *Forge) Exit() { // log.Info("forge.configSave =", f.configSave) - if f.configSave { - f.ConfigSave() + if f.Config.Mode != ForgeMode_NORMAL { + fhelp.DumpENV("forge:") + f.Config.DumpENV() } + + f.ConfigSave() if f.Repos != nil { if config.HasChanged("repos") { + log.Info("TRYING FILENAME:", f.Config.ReposPB) if err := f.Repos.ConfigSave(f.Config.ReposPB); err != nil { log.Info("forge.Repos.ConfigSave() error", err) } @@ -116,7 +123,6 @@ func (f *Forge) setenv() { log.Info("forge.Config() was nil") os.Exit(-1) } - // f.configDir = os.Getenv("FORGE_CONFIG") // f.forgeURL = os.Getenv("FORGE_URL") f.hostname = os.Getenv("HOSTNAME") if os.Getenv("FORGE_GOWORK") == "true" { diff --git a/repoNew.go b/repoNew.go index db21181..2b55cd3 100644 --- a/repoNew.go +++ b/repoNew.go @@ -9,6 +9,7 @@ import ( "regexp" "strings" + "go.wit.com/lib/config" "go.wit.com/lib/protobuf/gitpb" "go.wit.com/log" ) @@ -58,7 +59,7 @@ func (f *Forge) AddNamespaceDir(ns string, fullpath string) (*gitpb.Repo, error) if f.Config.IsReadOnly(repo.GetGoPath()) { repo.ReadOnly = true } - f.configSave = true + config.SetChanged("repos", true) return repo, nil } @@ -170,6 +171,7 @@ func (f *Forge) VerifyBranchNames(repo *gitpb.Repo) { repo.SetUserBranchName(uname) } } + log.Info("VerifyBranchNames", repo.GetMasterBranchName(), repo.GetDevelBranchName(), repo.GetUserBranchName()) } // what name should be used for the user branch? diff --git a/rill.go b/rill.go index 024c1b2..d85283c 100644 --- a/rill.go +++ b/rill.go @@ -34,7 +34,6 @@ func (f *Forge) rillUpdate(pool1 int, pool2 int) (int, error) { // Concurrency = 10 err := rill.ForEach(rills, pool2, func(repo *gitpb.Repo) error { counter += 1 - // log.Info("rill.ForEach() gopath=", repo.GetGoPath()) return f.updateRepo(repo) }) @@ -43,14 +42,12 @@ func (f *Forge) rillUpdate(pool1 int, pool2 int) (int, error) { func (f *Forge) updateRepo(repo *gitpb.Repo) error { if !repo.IsValidDir() { - log.Printf("%10s %-50s gopath=%s\n", "git dir is missing\n", repo.FullPath, repo.GetGoPath()) + log.Printf("%10s %-50s gopath=%s\n", "git dir is missing\n", repo.FullPath, repo.GetNamespace()) f.Repos.DeleteByFullPath(repo.FullPath) - f.configSave = true return nil } if repo.HasChanged() { - f.configSave = true // log.Info("repo changed ", repo.FullPath, repo.StateChange) if err := repo.ReloadCheck(); err != nil { return err @@ -63,7 +60,6 @@ func (f *Forge) updateRepo(repo *gitpb.Repo) error { } else { log.Info("readonly flag on repo is wrong", repo.GetGoPath()) repo.ReadOnly = true - f.configSave = true } } return nil @@ -102,7 +98,6 @@ func (f *Forge) RillReload() int { if !repo.HasChanged() { return nil } - f.configSave = true repo.ReloadCheck() counter += 1 return nil diff --git a/structs.go b/structs.go index 08aa464..8eb38c7 100644 --- a/structs.go +++ b/structs.go @@ -9,15 +9,14 @@ import ( // maybe an interface someday? type Forge struct { // one-time initialized data - once sync.Once - Config *ForgeConfigs // config repos for readonly, private, etc - Repos *gitpb.Repos // the repo protobufs - Patchsets *Patchsets // patches that are in progress - configSave bool // if you need to save the config because things changed - hostname string // your hostname - rillX int // used for Rill() - rillY int // used for Rill() - goWork bool // means the user is currently using a go.work file + once sync.Once + Config *ForgeConfigs // config repos for readonly, private, etc + Repos *gitpb.Repos // the repo protobufs + Patchsets *Patchsets // patches that are in progress + hostname string // your hostname + rillX int // used for Rill() + rillY int // used for Rill() + goWork bool // means the user is currently using a go.work file // goSrc string // the path to go/src // forgeURL string // URL to use to forge.wit.com // configDir string // normally ~/.config/forge