go-clone/main.go

121 lines
2.2 KiB
Go
Raw Permalink Normal View History

2024-03-07 00:50:58 -06:00
package main
import (
2024-03-07 19:30:09 -06:00
"fmt"
"os"
"path/filepath"
2024-03-07 16:45:49 -06:00
"go.wit.com/dev/alexflint/arg"
"go.wit.com/gui"
"go.wit.com/lib/gui/repolist"
2024-03-07 00:50:58 -06:00
"go.wit.com/lib/gui/shell"
"go.wit.com/log"
)
2024-03-07 16:45:49 -06:00
var VERSION string
2024-03-07 00:50:58 -06:00
2024-03-07 16:45:49 -06:00
var rv *repolist.RepoList
2024-03-07 19:30:09 -06:00
var myargs args
2024-03-07 00:50:58 -06:00
func main() {
2024-03-07 16:45:49 -06:00
arg.MustParse(&myargs)
2024-03-07 19:30:09 -06:00
if myargs.Repo == "" {
// tmp.WriteHelp(os.Stdout)
// fmt.Println("hello world")
tmp := myargs.Description()
fmt.Println(tmp)
os.Exit(0)
}
wdir, err := findWorkFile()
if err != nil {
log.Info(err)
os.Exit(-1)
2024-03-07 00:50:58 -06:00
}
2024-03-07 19:30:09 -06:00
log.Info("go.work directory:", wdir)
os.Setenv("REPO_WORK_PATH", wdir)
// readControlFile()
2024-03-07 00:50:58 -06:00
2024-03-07 16:45:49 -06:00
b := gui.RawBox()
rv = repolist.AutotypistView(b)
2024-03-07 00:50:58 -06:00
2024-03-07 19:30:09 -06:00
// clone(myargs.Repo)
2024-03-07 16:45:49 -06:00
rv.NewRepo(myargs.Repo)
2024-03-07 19:30:09 -06:00
// rv.NewRepo("go.wit.com/apps/helloworld")
2024-03-07 16:45:49 -06:00
for _, repo := range rv.AllRepos() {
log.Info("found repo", repo.GoPath(), repo.Status.Path())
}
2024-03-07 19:30:09 -06:00
// rv.Watchdog(func() {
// log.Info("watchdog")
// })
2024-03-07 16:45:49 -06:00
}
func clone(path string) {
2024-03-07 19:30:09 -06:00
pwd, err := os.Getwd()
if err != nil {
return
}
shell.RunPath(pwd, []string{"git", "clone", path})
2024-03-07 00:50:58 -06:00
}
2024-03-07 16:45:49 -06:00
2024-03-07 19:30:09 -06:00
// 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
}
if myargs.Work {
pwd := filepath.Join(pwd, "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
}
}
}
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
2024-03-07 16:45:49 -06:00
}
2024-03-07 19:30:09 -06:00
return "", fmt.Errorf("no go.work file found")
}