From 9e98e0b36d794d4acb7bc81909ee94598a43b37c Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Mon, 27 Oct 2025 10:45:25 -0500 Subject: work on a Cache setting concept --- Load.go | 120 --------------------------------------------- Save.go | 4 ++ cache.go | 51 ++++++++++++++++++++ forgeConfig.proto | 9 ++++ init.go | 51 +++++++++++++++++--- load.go | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ scanRepoDir.go | 1 + structs.go | 2 +- tableDefault.go | 4 +- 9 files changed, 256 insertions(+), 128 deletions(-) delete mode 100644 Load.go create mode 100644 cache.go create mode 100644 load.go diff --git a/Load.go b/Load.go deleted file mode 100644 index d940323..0000000 --- a/Load.go +++ /dev/null @@ -1,120 +0,0 @@ -// Code generated by go.wit.com/apps/autogenpb DO NOT EDIT. -// go install go.wit.com/apps/autogenpb@latest -// -// This file was autogenerated with autogenpb: -// autogenpb v0.5.25-2-g30e8de2 Built on 2025/10/16 09:16:09 ( 3 m) -// Theese sort.pb.go and marshal.pb.go files are autogenerated -// The autogenpb sources have example .proto files with instructions -// - -package forgepb - -import ( - "errors" - "os" - - "go.wit.com/lib/config" - "go.wit.com/lib/env" - "go.wit.com/lib/protobuf/argvpb" - "go.wit.com/lib/protobuf/gitpb" - "go.wit.com/log" -) - -func (pb *ForgeConfigs) loadConfig() error { - pb.Filename = config.MakeConfigFilename("forge", "forge") - err := config.ReLoad(pb) - if env.Verbose() { - log.Printf("%s loadConfig() about to load Config from %s\n", argvpb.GetAPPNAME(), pb.Filename) - } - if err != nil { - log.Info("couldn't load filename:", pb.Filename) - log.Info("forge has not been configured") - panic("config failed to load. make a new/blank forge config here?") - } - env.SetGlobal("lib/forgepb", "ForgeCfg", pb.Filename) - var changed bool - // migrate from the old gopath to "namespace" - for fc := range pb.IterAll() { - if fc.Namespace != "" { - continue - } - if fc.Namespace != fc.GoPath { - fc.Namespace = fc.GoPath - changed = true - } - // todo: deprecate this - fc.GoPath = "" // I want to do this but it might be a bad idea at this point - } - if env.Verbose() { - log.Printf("%s loadConfig() loaded Config from %s\n", argvpb.GetAPPNAME(), pb.Filename) - } - if changed { - config.SetChanged("forge", true) - } - - // init new config here - return err -} - -func (f *Forge) reposCacheLoad() error { - if f.Repos != nil { - return errors.New("already loaded") - } - f.Repos = gitpb.NewRepos() - f.Repos.Filename = config.MakeCacheFilename("forge", "repos") - err := config.ReLoad(f.Repos) - if err == nil { - env.SetGlobal("lib/forgepb", "ReposPB", f.Repos.Filename) - } - return err -} - -func (f *Forge) patchesCacheLoad() error { - if f.Patches != nil { - return errors.New("already loaded") - } - f.Patches = NewPatches() - f.Patches.Filename = config.MakeCacheFilename("forge", "patches") - err := config.ReLoad(f.Patches) - if errors.Is(err, os.ErrNotExist) { - // is a new file - f.Patches.Save() - return os.ErrNotExist - } - if err != nil { - log.Printf("patchesCacheLoad() failed err (%v) filename (%s)\n", err, f.Patches.Filename) - panic("failed to load patches") - } - env.SetGlobal("lib/forgepb", "PatchesPB", f.Patches.Filename) - return err -} - -func makeDefaultConfig() (*ForgeConfigs, error) { - cfg := NewForgeConfigs() - // Get fullpath to ~/.config/forge/forge.text - cfg.loadConfig() - - cfg.addSampleConfigs() - env.PrintTable() - var err error - if err = cfg.saveVerbose(); err != nil { - log.Info("config save error:", err) - } - return cfg, err -} - -// first time user. add go.wit.com as an example -func (cfg *ForgeConfigs) addSampleConfigs() { - newc := new(ForgeConfig) - newc.GoPath = "go.wit.com" - newc.Writable = true - newc.Directory = true - cfg.Append(newc) - - newc = new(ForgeConfig) - newc.GoPath = "priv.wit.com/corp" - newc.Writable = true - newc.Private = true - newc.Directory = true - cfg.Append(newc) -} diff --git a/Save.go b/Save.go index c02bc83..48c3aa7 100644 --- a/Save.go +++ b/Save.go @@ -162,3 +162,7 @@ func (f *Forge) flush() error { } return nil } + +func (pb *Cache) Save() error { + return config.Save(pb) +} diff --git a/cache.go b/cache.go new file mode 100644 index 0000000..0957b26 --- /dev/null +++ b/cache.go @@ -0,0 +1,51 @@ +// Code generated by go.wit.com/apps/autogenpb DO NOT EDIT. +// go install go.wit.com/apps/autogenpb@latest +// +// This file was autogenerated with autogenpb: +// autogenpb v0.5.25-2-g30e8de2 Built on 2025/10/16 09:16:09 ( 3 m) +// Theese sort.pb.go and marshal.pb.go files are autogenerated +// The autogenpb sources have example .proto files with instructions +// + +package forgepb + +import ( + "errors" + "os" + "path/filepath" + + "go.wit.com/lib/config" + "go.wit.com/lib/env" + "go.wit.com/log" +) + +func (f *Forge) configureCache(cacheName string) error { + // construct the full file name + fullname := config.MakeConfigFilename("forge", cacheName) + + // try to load the file + f.cache = new(Cache) + f.cache.Filename = fullname + if env.Verbose() { + log.Info("configCache() attempting to ReLoad()", fullname) + } + err := config.ReLoad(f.cache) + if errors.Is(err, os.ErrNotExist) { + // is a new file + return os.ErrNotExist + } + if err != nil { + log.Printf("patchesCacheLoad() failed err (%v) filename (%s)\n", err, f.cache.Filename) + panic("failed to load patches") + } + if cacheName == "gosrc" { + f.cache.Name = cacheName + f.cache.Filename = fullname + gosrc := filepath.Join(env.Get("homeDir"), "go/src") + f.cache.Dirs = []string{gosrc} + f.cache.Save() + } + env.SetGlobal("lib/forgepb", "cache", cacheName) + env.SetGlobal("lib/forgepb", "cacheTEXT", f.cache.Filename) + return err +} diff --git a/forgeConfig.proto b/forgeConfig.proto index df5e825..f533cc2 100644 --- a/forgeConfig.proto +++ b/forgeConfig.proto @@ -15,6 +15,14 @@ import "google/protobuf/timestamp.proto"; // Import the well-known type for Time // for example 'zookeeper' is packaged as 'zookeeper-go' // due to the prior apache foundation project. This happens and is ok! +message Cache { // `autogenpb:marshal` + string uuid = 1; // `autogenpb:uuid:21ef38cd-7564-49e4-99df-7e7e4c957e32` + string version = 2; // `autogenpb:version:v0.0.1` + string name = 3; // will use ~/.cache/forge/name.pb & ~/.config/forge/name.text + repeated string dirs = 4; // what dirs are to be scanned in for this cache.pb file + string Filename = 5; // what to use for the user branch (default ENV{USER}) +} + message ForgeConfig { // `autogenpb:nomutex` string namespace = 1; // `autogenpb:unique` `autogenpb:sort` // Examples: 'go.wit.com/apps/go-clone' or "~/mythings" or "/home/src/foo" @@ -54,4 +62,5 @@ message ForgeConfigs { // `autogenpb:mar string version = 2; // `autogenpb:version:v0.0.48` repeated ForgeConfig ForgeConfigs = 3; string Filename = 4; // what to use for the user branch (default ENV{USER}) + repeated Cache caches = 5; // cache config files } diff --git a/init.go b/init.go index 37f6e4b..d5dfb79 100644 --- a/init.go +++ b/init.go @@ -11,6 +11,7 @@ import ( "go.wit.com/lib/cobol" "go.wit.com/lib/config" "go.wit.com/lib/env" + "go.wit.com/lib/fhelp" "go.wit.com/log" ) @@ -61,6 +62,44 @@ func (f *Forge) Close() error { } func (f *Forge) postInit() error { + cache := "auto" + if env.Get("cache") == "" { + // leave "auto" by default + } else { + // get the users default preference + cache = env.Get("cache") + } + + // if cache is set to "auto", this detects if you are in a + // a golang directory (~/go/src) or in a dir that has a "go.work" file + if cache == "auto" { + if godir, ok := fhelp.FindGoWork(); ok { + f.mode = ForgeMode_GOLANG + cache = "gowork" + env.SetGlobal("lib/forgepb", "gopath", godir) + fullname := filepath.Join(godir, "gowork.pb") + env.SetGlobal("lib/forgepb", "ReposPB", fullname) + } else { + gopath := filepath.Join(env.Get("homedir"), "go/src") + + pwd, err := os.Getwd() + if err == nil { + _, err := filepath.Rel(gopath, pwd) + if err == nil { + f.mode = ForgeMode_GOLANG + // user is running forge from within ~/go/src + env.SetGlobal("lib/forgepb", "gopath", gopath) + cache = "gosrc" + } + } + // always define + if env.Get("gopath") == "" { + env.SetGlobal("lib/forgepb", "gopath", gopath) + } + } + } + f.configureCache(cache) + // always define if env.Get("username") == "" { usr, _ := user.Current() @@ -75,12 +114,6 @@ func (f *Forge) postInit() error { env.Save() } - // always define - if env.Get("gopath") == "" { - gopath := filepath.Join(env.Get("homedir"), "go/src") - env.SetGlobal("lib/forgepb", "gopath", gopath) - } - // always define if env.Get("curpatches") == "" { env.SetGlobal("lib/forgepb", "curpatches", "curpatches.pb") @@ -90,10 +123,16 @@ func (f *Forge) postInit() error { switch env.Get("Mode") { case "NORMAL": f.mode = ForgeMode_NORMAL + case "CUSTOM": + f.mode = ForgeMode_CUSTOM + case "GOLANG": + f.mode = ForgeMode_GOLANG case "CLEAN": f.mode = ForgeMode_CLEAN case "MASTER": f.mode = ForgeMode_MASTER + case "DEVEL": + f.mode = ForgeMode_DEVEL default: f.mode = ForgeMode_NEWUSER } diff --git a/load.go b/load.go new file mode 100644 index 0000000..5deed1d --- /dev/null +++ b/load.go @@ -0,0 +1,142 @@ +// Code generated by go.wit.com/apps/autogenpb DO NOT EDIT. +// go install go.wit.com/apps/autogenpb@latest +// +// This file was autogenerated with autogenpb: +// autogenpb v0.5.25-2-g30e8de2 Built on 2025/10/16 09:16:09 ( 3 m) +// Theese sort.pb.go and marshal.pb.go files are autogenerated +// The autogenpb sources have example .proto files with instructions +// + +package forgepb + +import ( + "errors" + "os" + + "go.wit.com/lib/config" + "go.wit.com/lib/env" + "go.wit.com/lib/protobuf/argvpb" + "go.wit.com/lib/protobuf/gitpb" + "go.wit.com/log" +) + +func (pb *ForgeConfigs) loadConfig() error { + cfgfile := "forge" + + pb.Filename = config.MakeConfigFilename("forge", cfgfile) + err := config.ReLoad(pb) + if env.Verbose() { + log.Printf("%s loadConfig() about to load Config from %s\n", argvpb.GetAPPNAME(), pb.Filename) + } + if err != nil { + log.Info("couldn't load filename:", pb.Filename) + log.Info("forge has not been configured") + panic("config failed to load. make a new/blank forge config here?") + } + env.SetGlobal("lib/forgepb", "ForgeCfg", pb.Filename) + var changed bool + // migrate from the old gopath to "namespace" + for fc := range pb.IterAll() { + if fc.Namespace != "" { + continue + } + if fc.Namespace != fc.GoPath { + fc.Namespace = fc.GoPath + changed = true + } + // todo: deprecate this + fc.GoPath = "" // I want to do this but it might be a bad idea at this point + } + if env.Verbose() { + log.Printf("%s loadConfig() loaded Config from %s\n", argvpb.GetAPPNAME(), pb.Filename) + } + if changed { + config.SetChanged("forge", true) + } + + // init new config here + return err +} + +// loads ~/.cache/forge/repos.pb +// user configurable, so it can load: +// ~/.cache/forge/mystuff.pb +func (f *Forge) reposCacheLoad() error { + if f.Repos != nil { + return errors.New("already loaded") + } + reposName := "repos" + if env.Get("cache") == "gosrc" { + reposName = "gosrc" + } + if env.Get("cache") == "home" { + reposName = "homedir" + } + + // get the + fullname := config.MakeCacheFilename("forge", reposName) + if env.Get("cache") == "gowork" { + fullname = env.Get("ReposPB") + } + f.Repos = gitpb.NewRepos() + f.Repos.Filename = fullname + err := config.ReLoad(f.Repos) + if err == nil { + if f.Repos.Filename != fullname { + // reset the filename, user probably did "mv" or "cp" + f.Repos.Filename = fullname + } + env.SetGlobal("lib/forgepb", "ReposPB", f.Repos.Filename) + } + return err +} + +func (f *Forge) patchesCacheLoad() error { + if f.Patches != nil { + return errors.New("already loaded") + } + f.Patches = NewPatches() + f.Patches.Filename = config.MakeCacheFilename("forge", "patches") + err := config.ReLoad(f.Patches) + if errors.Is(err, os.ErrNotExist) { + // is a new file + f.Patches.Save() + return os.ErrNotExist + } + if err != nil { + log.Printf("patchesCacheLoad() failed err (%v) filename (%s)\n", err, f.Patches.Filename) + panic("failed to load patches") + } + env.SetGlobal("lib/forgepb", "PatchesPB", f.Patches.Filename) + return err +} + +func makeDefaultConfig() (*ForgeConfigs, error) { + cfg := NewForgeConfigs() + // Get fullpath to ~/.config/forge/forge.text + cfg.loadConfig() + + cfg.addSampleConfigs() + env.PrintTable() + var err error + if err = cfg.saveVerbose(); err != nil { + log.Info("config save error:", err) + } + return cfg, err +} + +// first time user. add go.wit.com as an example +func (cfg *ForgeConfigs) addSampleConfigs() { + newc := new(ForgeConfig) + newc.GoPath = "go.wit.com" + newc.Writable = true + newc.Directory = true + cfg.Append(newc) + + newc = new(ForgeConfig) + newc.GoPath = "priv.wit.com/corp" + newc.Writable = true + newc.Private = true + newc.Directory = true + cfg.Append(newc) +} diff --git a/scanRepoDir.go b/scanRepoDir.go index d5050f7..537acee 100644 --- a/scanRepoDir.go +++ b/scanRepoDir.go @@ -130,6 +130,7 @@ func gitDirectoriesNew(srcDir string) ([]string, error) { _, fname := filepath.Split(path) switch fname { case "repos.pb": + case "gowork.pb": case "go.work": case "go.work.last": case "go.work.sum": diff --git a/structs.go b/structs.go index fc92b35..84f8675 100644 --- a/structs.go +++ b/structs.go @@ -13,7 +13,7 @@ type Forge struct { hostname string // your hostname goWork bool // means the user is currently using a go.work file mode ForgeMode // what "mode" forge is in - // once sync.Once // one-time initialized data + cache *Cache // decides what repos to work against } func (f *Forge) IsGoWork() bool { diff --git a/tableDefault.go b/tableDefault.go index 909a6ca..eb6d135 100644 --- a/tableDefault.go +++ b/tableDefault.go @@ -3,6 +3,8 @@ package forgepb import ( + "fmt" + "go.wit.com/lib/cobol" "go.wit.com/lib/protobuf/gitpb" "go.wit.com/log" @@ -62,7 +64,7 @@ func (f *Forge) makeDefaultTB(pb *gitpb.Repos) *gitpb.ReposTable { col = t.AddNamespace() col.Width = 33 - col.Header.Name = "Namespace " + f.mode.String() + col.Header.Name = fmt.Sprintf("Cache(%s) mode(%s)", f.cache.Name, f.mode.String()) col = t.AddCurrentBranchName() col.Width = 7 -- cgit v1.2.3