attempt to scan ~/go/src

This commit is contained in:
Jeff Carr 2024-11-28 08:35:39 -06:00
parent 0401b949c6
commit 27d052f513
7 changed files with 136 additions and 9 deletions

View File

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

View File

@ -3,7 +3,7 @@ package main
import (
"os"
"github.com/alexflint/go-arg"
"go.wit.com/dev/alexflint/arg"
)
var argv args

View File

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

18
init.go
View File

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

16
log.go Normal file
View File

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

97
scanGoSrc.go Normal file
View File

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

View File

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