summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Save.go4
-rw-r--r--cache.go51
-rw-r--r--forgeConfig.proto9
-rw-r--r--init.go51
-rw-r--r--load.go (renamed from Load.go)26
-rw-r--r--scanRepoDir.go1
-rw-r--r--structs.go2
-rw-r--r--tableDefault.go4
8 files changed, 138 insertions, 10 deletions
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()
@@ -76,12 +115,6 @@ func (f *Forge) postInit() error {
}
// 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
index d940323..5deed1d 100644
--- a/Load.go
+++ b/load.go
@@ -21,7 +21,9 @@ import (
)
func (pb *ForgeConfigs) loadConfig() error {
- pb.Filename = config.MakeConfigFilename("forge", "forge")
+ 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)
@@ -56,14 +58,34 @@ func (pb *ForgeConfigs) loadConfig() error {
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 = config.MakeCacheFilename("forge", "repos")
+ 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
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