move FindGitSrc to forgepb package

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-11-23 17:24:45 -06:00
parent 3a6226ea53
commit bff6eb093e
1 changed files with 21 additions and 72 deletions

93
main.go
View File

@ -1,7 +1,6 @@
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
@ -11,6 +10,7 @@ import (
"go.wit.com/lib/gui/repolist"
"go.wit.com/lib/gui/repostatus"
"go.wit.com/lib/gui/shell"
"go.wit.com/lib/protobuf/forgepb"
"go.wit.com/log"
)
@ -19,6 +19,7 @@ var VERSION string
var BUILDTIME string
var rv *repolist.RepoList
var goSrcPath string
func main() {
pp := arg.MustParse(&argv)
@ -30,13 +31,13 @@ func main() {
}
// use ~/go/src unless we find a go.work file in a parent directory
wdir, err := findWorkFile()
goSrcPath, err := forgepb.FindGoSrc()
if err != nil {
log.Info(err)
os.Exit(-1)
}
if wdir != "" {
os.Setenv("REPO_WORK_PATH", wdir)
if goSrcPath != "" {
os.Setenv("REPO_WORK_PATH", goSrcPath)
}
@ -54,24 +55,24 @@ func main() {
}
} else {
// the user specified a repo, check if it's already cloned
fullgitdir := filepath.Join(wdir, argv.Repo, ".git")
fullgitdir := filepath.Join(goSrcPath, argv.Repo, ".git")
if shell.IsDir(fullgitdir) {
// if --recursive, continue
// if --git-pull, continue
if argv.Recursive || argv.Pull || argv.RedoGoMod {
log.Info("repo already cloned", filepath.Join(wdir, argv.Repo))
log.Info("repo already cloned", filepath.Join(goSrcPath, argv.Repo))
// there is more to do
log.Info("argv.Recursive is", argv.Recursive)
log.Info("argv.Pull is", argv.Pull)
log.Info("argv.RedoGoMod is", argv.RedoGoMod)
log.Info("onward and upward")
} else {
log.Info("repo already cloned", filepath.Join(wdir, argv.Repo))
log.Info("repo already cloned", filepath.Join(goSrcPath, argv.Repo))
os.Exit(0)
}
} else {
// need to download this new repo!
log.Info("repo is new. going to clone it to:", filepath.Join(wdir, argv.Repo))
log.Info("repo is new. going to clone it to:", filepath.Join(goSrcPath, argv.Repo))
}
}
@ -88,14 +89,14 @@ func main() {
os.Exit(-1)
}
if argv.Recursive || argv.Pull || argv.RedoGoMod {
log.Info("repo already cloned", filepath.Join(wdir, argv.Repo))
log.Info("repo already cloned", filepath.Join(goSrcPath, argv.Repo))
// there is more to do
log.Info("argv.Recursive is", argv.Recursive)
log.Info("argv.Pull is", argv.Pull)
log.Info("argv.RedoGoMod is", argv.RedoGoMod)
log.Info("onward and upward")
} else {
log.Info("repo cloned worked to", filepath.Join(wdir, argv.Repo))
log.Info("repo cloned worked to", filepath.Join(goSrcPath, argv.Repo))
os.Exit(0)
}
@ -105,16 +106,16 @@ func main() {
newr.Status.MakeRedomod()
// double check it actually downloaded
fullgitdir := filepath.Join(wdir, argv.Repo, ".git")
fullgitdir := filepath.Join(goSrcPath, argv.Repo, ".git")
if !shell.IsDir(fullgitdir) {
log.Info("repo cloned failed", filepath.Join(wdir, argv.Repo))
log.Info("repo cloned failed", filepath.Join(goSrcPath, argv.Repo))
os.Exit(-1)
}
}
os.Setenv("REPO_AUTO_CLONE", "false")
// look recursively in your working directory for git repos
totalcount := scanForRepos(wdir)
totalcount := scanForRepos(goSrcPath)
// if --git-pull, run git pull on everything here
if argv.Pull {
@ -174,12 +175,12 @@ func main() {
// remake the go.work file
if argv.AutoWork {
log.Info("About to re-create", wdir+"/go.work")
log.Info("About to re-create", goSrcPath+"/go.work")
log.Info("Sleep 3. original go.work saved as go.work.last (hit ctrl-c to cancel)")
log.Sleep(3)
shell.PathRun(wdir, []string{"mv", "go.work", "go.work.last"})
shell.PathRun(goSrcPath, []string{"mv", "go.work", "go.work.last"})
rv.MakeGoWork()
shell.PathRun(wdir, []string{"go", "work", "use"})
shell.PathRun(goSrcPath, []string{"go", "work", "use"})
log.Info("")
log.Info("original go.work file saved as go.work.last")
log.Info("")
@ -189,66 +190,14 @@ func main() {
log.Info("Finished go-clone for", argv.Repo)
}
// look for a go.work file
// otherwise use ~/go/src
func findWorkFile() (string, error) {
pwd, err := os.Getwd()
if err == nil {
// Check for go.work in the current directory and then move up until root
if pwd, err := digup(pwd); err == nil {
log.Info("using go.work file in directory", pwd)
// found an existing go.work file
os.Chdir(pwd)
return pwd, nil
}
}
// there are no go.work files, resume the ~/go/src behavior from prior to golang 1.22
pwd, err = useGoSrc()
log.Info("using ~/go/src directory", pwd)
return pwd, err
}
// this is the 'old way" and works fine for me. I use it because I like the ~/go/src directory
// because I know exactly what is in it: GO stuff & nothing else
func useGoSrc() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}
pwd := filepath.Join(homeDir, "go/src")
shell.Mkdir(pwd)
os.Chdir(pwd)
return pwd, nil
}
func digup(path string) (string, error) {
for {
workFilePath := filepath.Join(path, "go.work")
if _, err := os.Stat(workFilePath); err == nil {
return path, nil // Found the go.work file
} else if !os.IsNotExist(err) {
return "", err // An error other than not existing
}
parentPath := filepath.Dir(path)
if parentPath == path {
break // Reached the filesystem root
}
path = parentPath
}
return "", fmt.Errorf("no go.work file found")
}
func scanForRepos(wdir string) int {
func scanForRepos(goSrcPath string) int {
var count int
log.Info("scanning for repo in:", filepath.Join(wdir, argv.Repo))
log.Info("scanning for repo in:", filepath.Join(goSrcPath, argv.Repo))
// rv.NewRepo("go.wit.com/apps/helloworld")
for _, path := range repostatus.ScanGitDirectories(wdir) {
for _, path := range repostatus.ScanGitDirectories(goSrcPath) {
count += 1
gopath := strings.TrimPrefix(path, wdir)
gopath := strings.TrimPrefix(path, goSrcPath)
gopath = strings.Trim(gopath, "/")
// log.Info("Also should add:", gopath)
rv.NewRepo(gopath)