use config GO library

This commit is contained in:
Jeff Carr 2025-09-13 05:33:11 -05:00
parent ce4af38e8b
commit 0f895e83e6
9 changed files with 71 additions and 51 deletions

View File

@ -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
}

View File

@ -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 (?)

View File

@ -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

View File

@ -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
}

View File

@ -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 + ") "
}

38
init.go
View File

@ -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" {

View File

@ -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?

View File

@ -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

View File

@ -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