look for and use any go.work file

This commit is contained in:
Jeff Carr 2024-03-07 19:30:09 -06:00
parent 57c61847c0
commit b5825e93dd
3 changed files with 87 additions and 32 deletions

1
.gitignore vendored
View File

@ -2,4 +2,5 @@
go.mod go.mod
go.sum go.sum
/files/* /files/*
/work/*
go-clone go-clone

View File

@ -1,7 +1,7 @@
VERSION = $(shell git describe --tags) VERSION = $(shell git describe --tags)
run: build run: build
./go-clone github.com/rclone/rclone ./go-clone --work github.com/rclone/rclone
vet: vet:
@GO111MODULE=off go vet @GO111MODULE=off go vet

112
main.go
View File

@ -1,6 +1,10 @@
package main package main
import ( import (
"fmt"
"os"
"path/filepath"
"go.wit.com/dev/alexflint/arg" "go.wit.com/dev/alexflint/arg"
"go.wit.com/gui" "go.wit.com/gui"
"go.wit.com/lib/gui/repolist" "go.wit.com/lib/gui/repolist"
@ -11,56 +15,106 @@ import (
var VERSION string var VERSION string
var rv *repolist.RepoList var rv *repolist.RepoList
var myargs args
func main() { func main() {
var myargs args
// tmp := arg.MustParse(&myargs)
arg.MustParse(&myargs) arg.MustParse(&myargs)
if myargs.Work { if myargs.Repo == "" {
shell.Mkdir("work") // tmp.WriteHelp(os.Stdout)
} else { // fmt.Println("hello world")
// filepath := filepath.Join("/home/jcarr/go/src") tmp := myargs.Description()
// os.Chdir(filepath) fmt.Println(tmp)
os.Exit(0)
} }
// if myargs.Repo == "" {
// // tmp.WriteHelp(os.Stdout) wdir, err := findWorkFile()
// // fmt.Println("hello world") if err != nil {
// tmp := myargs.Description() log.Info(err)
// fmt.Println(tmp) os.Exit(-1)
// os.Exit(0) }
// } log.Info("go.work directory:", wdir)
os.Setenv("REPO_WORK_PATH", wdir)
// readControlFile()
b := gui.RawBox() b := gui.RawBox()
rv = repolist.AutotypistView(b) rv = repolist.AutotypistView(b)
// shell.TestTerminalColor() // clone(myargs.Repo)
readControlFile()
clone(myargs.Repo)
rv.NewRepo(myargs.Repo) rv.NewRepo(myargs.Repo)
rv.NewRepo("go.wit.com/apps/helloworld") // rv.NewRepo("go.wit.com/apps/helloworld")
for _, repo := range rv.AllRepos() { for _, repo := range rv.AllRepos() {
log.Info("found repo", repo.GoPath(), repo.Status.Path()) log.Info("found repo", repo.GoPath(), repo.Status.Path())
} }
rv.Watchdog(func() { // rv.Watchdog(func() {
log.Info("watchdog") // log.Info("watchdog")
}) // })
} }
func clone(path string) { func clone(path string) {
shell.RunPath([]string{"git", "clone", path}) pwd, err := os.Getwd()
if err != nil {
return
}
shell.RunPath(pwd, []string{"git", "clone", path})
}
// look for or make 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
pwd, err = digup(pwd)
if err == nil {
os.Chdir(pwd)
return pwd, nil
} }
func findWorkDir() {
if myargs.Work { if myargs.Work {
shell.Mkdir("work") pwd := filepath.Join(pwd, "work")
shell.Mkdir("work") shell.Mkdir(pwd)
os.Chdir(pwd)
if _, err := os.Stat("go.work"); err == nil {
return pwd, nil
}
shell.RunPath(pwd, []string{"go", "work", "init"})
if shell.Exists("go.work") {
return pwd, nil
}
}
} }
// filepath := filepath.Join("/home/jcarr/go/src")
// os.Chdir(filepath)
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")
}