From 27d052f513f6ae1f27391bac2769cfc7bf1bd61f Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 28 Nov 2024 08:35:39 -0600 Subject: [PATCH] attempt to scan ~/go/src --- Makefile | 4 ++ forgeConfig/argv.go | 2 +- forgeConfig/main.go | 2 +- init.go | 18 ++++++--- log.go | 16 ++++++++ scanGoSrc.go | 97 +++++++++++++++++++++++++++++++++++++++++++++ structs.go | 6 ++- 7 files changed, 136 insertions(+), 9 deletions(-) create mode 100644 log.go create mode 100644 scanGoSrc.go diff --git a/Makefile b/Makefile index 368120c..c73a6c5 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ all: forgeConfig.pb.go + reset make -C forgeConfig vet: lint @@ -29,6 +30,9 @@ clean: -rm -f go.* make -C forgeConfig clean +install: + make -C forgeConfig install + forgeConfig.pb.go: forgeConfig.proto # I'm using version v1.35.x from google.golang.org/protobuf/cmd/protoc-gen-go cd ~/go/src && protoc --go_out=. --proto_path=go.wit.com/lib/protobuf/forgepb \ diff --git a/forgeConfig/argv.go b/forgeConfig/argv.go index 5e01d4f..635989f 100644 --- a/forgeConfig/argv.go +++ b/forgeConfig/argv.go @@ -3,7 +3,7 @@ package main import ( "os" - "github.com/alexflint/go-arg" + "go.wit.com/dev/alexflint/arg" ) var argv args diff --git a/forgeConfig/main.go b/forgeConfig/main.go index bd65f26..ca958c7 100644 --- a/forgeConfig/main.go +++ b/forgeConfig/main.go @@ -15,7 +15,7 @@ func main() { f.Init() if argv.List { - f.PrintTable() + f.ConfigPrintTable() loop := f.SortByPath() // get the list of forge configs for loop.Scan() { r := loop.Next() diff --git a/init.go b/init.go index 8c6af82..f22d30d 100644 --- a/init.go +++ b/init.go @@ -2,8 +2,8 @@ package forgepb import ( "os" - "path/filepath" + "go.wit.com/lib/protobuf/gitpb" "go.wit.com/log" ) @@ -13,13 +13,12 @@ func init() { if gosrc != "" { // already set. ignore init() } - homeDir, err := os.UserHomeDir() + goSrcDir, err := FindGoSrc() if err != nil { - log.Warn("forge init() could not find UserHomeDir()", err) - panic("forge could not find UserHomeDir") + log.Warn("forge init() FindGoSrc()", err) + panic("forge init() FindGoSrc()") } - fullpath := filepath.Join(homeDir, "go/src") - os.Setenv("FORGE_GOSRC", fullpath) + os.Setenv("FORGE_GOSRC", goSrcDir) } func (f *Forge) Init() { @@ -34,6 +33,13 @@ func (f *Forge) Init() { log.Warn("forgepb.ConfigLoad() failed", err) os.Exit(-1) } + + if f.Repos == nil { + f.Repos = new(gitpb.Repos) + } + + f.goSrc = os.Getenv("FORGE_GOSRC") + f.ScanGoSrc() } func (f *Forge) SortByPath() *ForgeConfigIterator { diff --git a/log.go b/log.go new file mode 100644 index 0000000..e8663b1 --- /dev/null +++ b/log.go @@ -0,0 +1,16 @@ +package forgepb + +import ( + "go.wit.com/log" +) + +var FORGEPB *log.LogFlag +var FORGEPBWARN *log.LogFlag + +func init() { + full := "go.wit.com/lib/protobuf/forgepb" + short := "forgepb" + + FORGEPB = log.NewFlag("FORGEPB", false, full, short, "general forgepb things") + FORGEPBWARN = log.NewFlag("FORGEPBWARN", true, full, short, "forgepb warnings") +} diff --git a/scanGoSrc.go b/scanGoSrc.go new file mode 100644 index 0000000..4e41358 --- /dev/null +++ b/scanGoSrc.go @@ -0,0 +1,97 @@ +package forgepb + +import ( + "os" + "path/filepath" + "strings" + + "go.wit.com/lib/protobuf/gitpb" + "go.wit.com/log" +) + +func (f *Forge) ScanGoSrc() (bool, error) { + dirs, err := gitDirectories(f.goSrc) + if err != nil { + return false, err + } + + var repos *gitpb.Repos + repos = new(gitpb.Repos) + + newr, err := repos.NewGoPath("/home/jcarr/go/src", "go.wit.com/apps/wit-package") + if err != nil { + log.Info("init failed", err) + panic("crapnuts") + } else { + log.Info("init worked for", newr.GoPath) + } + + for _, dir := range dirs { + if strings.HasPrefix(dir, f.goSrc) { + gopath := strings.TrimPrefix(dir, f.goSrc) + gopath = strings.Trim(gopath, "/") + log.Info("ScanGoSrc() ok:", f.goSrc, gopath) + } else { + log.Info("ScanGoSrc() bad:", dir) + } + } + return true, err +} + +func gitDirectories(srcDir string) ([]string, error) { + var all []string + err := filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error { + if err != nil { + log.Log(FORGEPBWARN, "Error accessing path:", path, err) + return nil + } + + // Check if the path is a directory and has a .git subdirectory + if info.IsDir() && IsGitDir(path) { + all = append(all, path) + } + + return nil + }) + + if err != nil { + log.Log(FORGEPBWARN, "Error walking the path:", srcDir, err) + } + + return all, err +} + +/* +// rill is awesome. long live rill +func rillAddDirs(gopaths []string) { + // Convert a slice of user IDs into a channel + ids := rill.FromSlice(gopaths, nil) + + // Read users from the API. + // Concurrency = 20 + dirs := rill.Map(ids, 20, func(id string) (*repolist.RepoRow, error) { + return me.repos.View.FindByName(id), nil + }) + + // Activate users. + // Concurrency = 10 + err := rill.ForEach(dirs, 10, func(repo *repolist.RepoRow) error { + fmt.Printf("Repo found : %s\n", repo.GoPath()) + repo.Run([]string{"git", "pull"}) + return nil + }) + + // Handle errors + fmt.Println("Error:", err) +} +*/ + +// IsGitDir checks if a .git directory exists inside the given directory +func IsGitDir(dir string) bool { + gitDir := filepath.Join(dir, ".git") + info, err := os.Stat(gitDir) + if os.IsNotExist(err) { + return false + } + return info.IsDir() +} diff --git a/structs.go b/structs.go index dce9cb4..55b3188 100644 --- a/structs.go +++ b/structs.go @@ -1,6 +1,10 @@ package forgepb +import "go.wit.com/lib/protobuf/gitpb" + // maybe an interface someday? type Forge struct { - Config *ForgeConfigs + goSrc string // the path to go/src + Config *ForgeConfigs // config repos for readonly, private, etc + Repos *gitpb.Repos }