prepare to use go-clone to build

This commit is contained in:
Jeff Carr 2024-11-30 12:44:29 -06:00
parent dd258155df
commit fa98d97dfb
3 changed files with 137 additions and 145 deletions

View File

@ -12,6 +12,7 @@ no-gui: build
./go-clone --no-gui ./go-clone --no-gui
build: build:
make -C ../../lib/protobuf/gitpb/
GO111MODULE=off go build \ GO111MODULE=off go build \
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}" -ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"

34
argv.go
View File

@ -14,27 +14,27 @@ type args struct {
DryRun bool `arg:"--dry-run" help:"show what would be run"` DryRun bool `arg:"--dry-run" help:"show what would be run"`
Recursive bool `arg:"--recursive" default:"false" help:"resursively clone all dependencies"` Recursive bool `arg:"--recursive" default:"false" help:"resursively clone all dependencies"`
Pull bool `arg:"--git-pull" default:"false" help:"run 'git pull' on all your repos"` Pull bool `arg:"--git-pull" default:"false" help:"run 'git pull' on all your repos"`
Build bool `arg:"--build" default:"true" help:"also try to build it"`
Install bool `arg:"--install" default:"false" help:"try to install every binary package"`
RedoGoMod bool `arg:"--go-reset" default:"false" help:"remake all the go.sum and go.mod files"` RedoGoMod bool `arg:"--go-reset" default:"false" help:"remake all the go.sum and go.mod files"`
// Fetch bool `arg:"--git-fetch" default:"false" help:"run 'git fetch' on all your repos"` // Fetch bool `arg:"--git-fetch" default:"false" help:"run 'git fetch' on all your repos"`
} }
func (a args) Description() string {
return `
go-clone does git clone on go package repositories.
It uses ~/go/src unless it finds a go.work file in a parent directory.
This will clone the sources for go-clone:
go-clone go.wit.com/apps/go-clone
If a go.work file is found, this will auto generate a new go.work file.
The old work file is saved as go.work.last
go-clone --auto-work go.wit.com/apps/go-clone
This will recursively clone a package and all the build requirements:
go-clone --recursive go.wit.com/apps/go-clone
`
}
func (args) Version() string { func (args) Version() string {
return "go-clone " + VERSION + " Built on " + BUILDTIME return "go-clone " + VERSION + " Built on " + BUILDTIME
} }
func (a args) Description() string {
return `
git clone go repositories
Examples:
go-clone go.wit.com/apps/go-clone # simply try to git clone this
go-clone --recursive go.wit.com/apps/go-clone # recursively clone all the dependancies
go-clone --auto-work go.wit.com/apps/go-clone # if you are using a go.work file, recreate the go.work file
go-clone --go-reset # recreate every go.mod and go.sum file
go-clone --git-pull # run 'git pull' in every repo
go-clone --build # build every binary package
go-clone --install # install every binary package
`
}

247
main.go
View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"errors"
"os" "os"
"path/filepath" "path/filepath"
@ -16,91 +17,94 @@ import (
var VERSION string var VERSION string
var BUILDTIME string var BUILDTIME string
var pp *arg.Parser
var forge *forgepb.Forge
var rv *repolist.RepoList var rv *repolist.RepoList
var goSrcPath string
func main() { func main() {
pp := arg.MustParse(&argv) pp = arg.MustParse(&argv)
// for very new users or users unfamilar with the command line, this may help them // for very new users or users unfamilar with the command line, this may help them
if argv.Repo == "version" || argv.Repo == "help" || argv.Repo == "?" { if argv.Repo == "help" || argv.Repo == "?" {
pp.WriteHelp(os.Stdout) pp.WriteHelp(os.Stdout)
os.Exit(0) os.Exit(0)
} }
if argv.Repo == "version" {
log.Info(argv.Version())
os.Exit(0)
}
// load the ~/.config/forge/ config // load the ~/.config/forge/ config
forge := forgepb.Init() // this lets you configure repos you have read/write access too
forge = forgepb.Init()
forge.ConfigPrintTable() forge.ConfigPrintTable()
os.Setenv("REPO_WORK_PATH", forge.GetGoSrc()) os.Setenv("REPO_WORK_PATH", forge.GetGoSrc())
if argv.Repo == "" { // gui is in testing
// if there isn't anything else, just exit here
// if --git-pull, continue
if argv.Pull || argv.RedoGoMod {
// there is more to do
log.Info("onward and upward")
} else {
// user needs to pick something to do
pp.WriteHelp(os.Stdout)
log.Info("give me something to do!")
os.Exit(0)
}
} else {
// the user specified a repo, check if it's already cloned
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(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(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(goSrcPath, argv.Repo))
}
}
// testing gui idea
myGui := gui.New() myGui := gui.New()
// myGui.Default() // myGui.Default()
// find and scan all repos
rv := repolist.Init(forge, myGui) rv := repolist.Init(forge, myGui)
rv.Enable() rv.Enable()
rv.ScanRepositories() rv.ScanRepositories()
// run 'git pull' if argv --git-pull
if argv.Pull {
gitPull()
okExit("git pull")
}
// remake all the go.sum & go.mod in every repo
// todo: make go.sum and go.mod git commit metadata
if argv.RedoGoMod {
redoGoModAll()
}
// this works sometimes
if argv.Recursive {
recursiveClone()
autoWork()
build()
okExit("--recursive")
}
clone()
autoWork()
build()
if argv.Repo == "" {
pp.WriteHelp(os.Stdout)
os.Exit(0)
}
okExit(argv.Repo)
}
func okExit(thing string) {
log.Info("Total repositories:", forge.Repos.Len())
log.Info("Finished go-clone", thing, "ok")
os.Exit(0)
}
func badExit(err error) {
log.Info("Total repositories:", forge.Repos.Len())
log.Info("Finished go-clone with error", err)
os.Exit(-1)
}
func clone() {
// if the user defined a repo, attempt to download it now // if the user defined a repo, attempt to download it now
if argv.Repo != "" { if argv.Repo != "" {
os.Setenv("REPO_AUTO_CLONE", "true") os.Setenv("REPO_AUTO_CLONE", "true")
// pb, _ := forge.NewGoPath(argv.Repo) // pb, _ := forge.NewGoPath(argv.Repo)
pb, err := forge.Clone(argv.Repo) pb, err := forge.Clone(argv.Repo)
if err != nil { if err != nil {
log.Info("could not download:", err) log.Info("could not download")
os.Exit(-1) badExit(err)
} }
newr, err := rv.AddRepo(pb) newr, err := rv.AddRepo(pb)
if err != nil { if err != nil {
log.Info("AddRepo() failed", err) log.Info("repolist.AddRepo() failed")
os.Exit(-1) badExit(err)
}
if argv.Recursive || argv.Pull || argv.RedoGoMod {
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(goSrcPath, argv.Repo))
os.Exit(0)
} }
// update go.sum and go.mod // update go.sum and go.mod
@ -109,57 +113,61 @@ func main() {
newr.MakeRedoMod() newr.MakeRedoMod()
// double check it actually downloaded // double check it actually downloaded
fullgitdir := filepath.Join(goSrcPath, argv.Repo, ".git") fullgitdir := filepath.Join(forge.GetGoSrc(), argv.Repo, ".git")
if !shell.IsDir(fullgitdir) { if !shell.IsDir(fullgitdir) {
log.Info("repo cloned failed", filepath.Join(goSrcPath, argv.Repo)) log.Info("repo cloned failed", filepath.Join(forge.GetGoSrc(), argv.Repo))
os.Exit(-1) badExit(errors.New(fullgitdir + " was not created"))
}
build()
// exit unless other --argv options are set
if !(argv.Recursive || argv.Pull || argv.RedoGoMod) {
log.Info("repo cloned worked to", filepath.Join(forge.GetGoSrc(), argv.Repo))
okExit(argv.Repo)
}
log.Info("onward and upward")
}
}
func gitPull() {
log.Info("Total repositories:", forge.Repos.Len())
log.Info("Going to run git pull in each one. TODO: use rill here")
log.Sleep(1)
pull := []string{"git", "pull"}
var trycount, errcount int
repos := forge.Repos.SortByPath()
for repos.Scan() {
repo := repos.Next()
if argv.DryRun {
log.Info("git pull --dry-run", repo.GoPath)
continue
}
log.Info("git pull:", repo.FullPath)
trycount += 1
log.Info("actually run: git pull:", repo.GoPath)
if result := shell.PathRunRealtime(repo.FullPath, pull); result.Error != nil {
log.Info("git pull error:", result.Error)
errcount += 1
} }
} }
log.Info("Total repositories:", forge.Repos.Len(), "Total attempted:", trycount, "Errors:", errcount)
}
os.Setenv("REPO_AUTO_CLONE", "false") func recursiveClone() {
// look recursively in your working directory for git repos // this works sometimes
totalcount := forge.Repos.Len()
// if --git-pull, run git pull on everything here
if argv.Pull {
log.Info("Total repositories:", totalcount)
log.Info("Going to run git pull in each one")
log.Sleep(1)
pull := []string{"git", "pull"}
var trycount, errcount int
loop := rv.ReposSortByName()
for loop.Scan() {
repo := loop.Repo()
if argv.DryRun {
log.Info("git pull --dry-run", repo.Status.Path())
} else {
trycount += 1
log.Info("actually run: git pull:", repo.Status.Path())
if result := shell.PathRunRealtime(repo.Status.Path(), pull); result.Error != nil {
log.Info("git pull error:", result.Error)
errcount += 1
}
}
}
log.Info("Total repositories:", totalcount, "Total attempted:", trycount, "Errors:", errcount)
os.Exit(0)
}
// this is experiemental but works for me
if argv.Recursive { if argv.Recursive {
newr := rv.FindByName(argv.Repo) newr := rv.FindByName(argv.Repo)
if newr == nil { if newr == nil {
log.Info("how did this repo still not exist?", argv.Repo) log.Info("how did this repo still not exist?", argv.Repo)
os.Exit(-1) badExit(errors.New("failed to clone repo: " + argv.Repo))
} }
os.Setenv("REPO_AUTO_CLONE", "true") os.Setenv("REPO_AUTO_CLONE", "true")
godep := newr.Status.GetGoDeps() godep := newr.Status.GetGoDeps()
for gopath, version := range godep { for gopath, version := range godep {
pb, err := forge.Clone(gopath) pb, err := forge.Clone(gopath)
if err != nil { if err != nil {
log.Info("could not download:", err) log.Info("could not download")
os.Exit(-1) badExit(err)
} }
repo, err := rv.AddRepo(pb) repo, err := rv.AddRepo(pb)
if err != nil { if err != nil {
@ -170,52 +178,35 @@ func main() {
repo.MakeRedoMod() repo.MakeRedoMod()
} }
} }
}
// remake all the go.sum & go.mod in every repo func redoGoModAll() {
// todo: make go.sum and go.mod git commit metadata loop := rv.ReposSortByName()
if argv.RedoGoMod { for loop.Scan() {
loop := rv.ReposSortByName() repo := loop.Repo()
for loop.Scan() { repo.MakeRedoMod()
repo := loop.Repo()
repo.MakeRedoMod()
}
} }
}
func build() {
if argv.Build {
log.Info("need to try to build here")
}
}
func autoWork() {
// remake the go.work file // remake the go.work file
if argv.AutoWork { if argv.AutoWork {
log.Info("About to re-create", goSrcPath+"/go.work") log.Info("About to re-create", forge.GetGoSrc()+"/go.work")
log.Info("Sleep 3. original go.work saved as go.work.last (hit ctrl-c to cancel)") log.Info("Sleep 3. original go.work saved as go.work.last (hit ctrl-c to cancel)")
log.Sleep(3) log.Sleep(3)
shell.PathRun(goSrcPath, []string{"mv", "go.work", "go.work.last"}) shell.PathRun(forge.GetGoSrc(), []string{"mv", "go.work", "go.work.last"})
rv.MakeGoWork() rv.MakeGoWork()
shell.PathRun(goSrcPath, []string{"go", "work", "use"}) shell.PathRun(forge.GetGoSrc(), []string{"go", "work", "use"})
log.Info("") log.Info("")
log.Info("original go.work file saved as go.work.last") log.Info("original go.work file saved as go.work.last")
log.Info("") log.Info("")
okExit("go.work create")
} }
log.Info("Total repositories:", totalcount)
log.Info("Finished go-clone for", argv.Repo)
} }
/*
func scanForRepos(goSrcPath string) int {
var count int
log.Info("scanning for repo in:", filepath.Join(goSrcPath, argv.Repo))
// rv.NewRepo("go.wit.com/apps/helloworld")
for _, path := range repostatus.ScanGitDirectories(goSrcPath) {
count += 1
gopath := strings.TrimPrefix(path, goSrcPath)
gopath = strings.Trim(gopath, "/")
// log.Info("Also should add:", gopath)
pb, err := forge.Clone(gopath)
if err != nil {
log.Info("could not download:", err)
os.Exit(-1)
}
repo, err := rv.AddRepo(pb)
}
return count
}
*/