--go-src + --recursive

This commit is contained in:
Jeff Carr 2024-03-21 22:47:17 -05:00
parent 7a095d1566
commit 84b048aa29
3 changed files with 21 additions and 7 deletions

View File

@ -3,7 +3,7 @@ VERSION = $(shell git describe --tags)
run: build run: build
@#./go-clone --work github.com/rclone/rclone @#./go-clone --work github.com/rclone/rclone
@# ./go-clone --work go.wit.com/apps/basicwindow @# ./go-clone --work go.wit.com/apps/basicwindow
./go-clone go.wit.com/apps/basicwindow ./go-clone
# test using --no-work against ~/go/src # test using --no-work against ~/go/src
homeGoSrc: build homeGoSrc: build
@ -13,7 +13,7 @@ homeGoSrc: build
./go-clone --no-work go.wit.com/apps/basicwindow ./go-clone --no-work go.wit.com/apps/basicwindow
modernc: build modernc: build
./go-clone --no-work modernc.org/sqlite ./go-clone --no-work --recursive modernc.org/sqlite
vet: vet:
@GO111MODULE=off go vet @GO111MODULE=off go vet

View File

@ -8,14 +8,18 @@ package main
type args struct { type args struct {
Repo string `arg:"positional" help:"go import path"` Repo string `arg:"positional" help:"go import path"`
Work bool `arg:"--work" help:"make a work directory"` // Work bool `arg:"--work" help:"make a work directory"`
NoWork bool `arg:"--no-work" help:"do not make or modify the go.work file"` NoWork bool `arg:"--no-work" help:"do not make or modify the go.work file"`
GoSrc bool `arg:"--go-src" help:"only work in ~/go/src"`
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" help:"resursively clone all dependencies"` Recursive bool `arg:"--recursive" help:"resursively clone all dependencies"`
} }
func (a args) Description() string { func (a args) Description() string {
return ` return `
By default, go-clone will find your go.work file and work from there.
Otherwise, it will create a work/ directory.
This will recursively clone the sources for this app into a work/ directory: This will recursively clone the sources for this app into a work/ directory:
go-clone --recursive go.wit.com/apps/go-clone go-clone --recursive go.wit.com/apps/go-clone

18
main.go
View File

@ -27,6 +27,7 @@ func main() {
os.Exit(0) os.Exit(0)
} }
// figures out where you're go.work file is
wdir, err := findWorkFile() wdir, err := findWorkFile()
if err != nil { if err != nil {
log.Info(err) log.Info(err)
@ -93,6 +94,11 @@ func main() {
// look for or make a go.work file // look for or make a go.work file
// otherwise use ~/go/src // otherwise use ~/go/src
func findWorkFile() (string, error) { func findWorkFile() (string, error) {
if myargs.GoSrc {
// user put --go-src on the command line so use ~/go/src
return useGoSrc()
}
pwd, err := os.Getwd() pwd, err := os.Getwd()
if err == nil { if err == nil {
// Check for go.work in the current directory and then move up until root // Check for go.work in the current directory and then move up until root
@ -103,7 +109,7 @@ func findWorkFile() (string, error) {
} }
// if the user added '--work' on the cmdline, make a work directory and init the go.work file // if the user added '--work' on the cmdline, make a work directory and init the go.work file
if myargs.Work { if ! myargs.NoWork {
pwd, err = os.Getwd() pwd, err = os.Getwd()
newpwd := filepath.Join(pwd, "work") newpwd := filepath.Join(pwd, "work")
shell.Mkdir(newpwd) shell.Mkdir(newpwd)
@ -119,13 +125,17 @@ func findWorkFile() (string, error) {
} }
// there are no go.work files, resume the ~/go/src behavior from prior to golang 1.22 // there are no go.work files, resume the ~/go/src behavior from prior to golang 1.22
// this is the 'old way" and works fine for me. I use it because I like the ~/go/src directory return useGoSrc()
// because I know exactly what is in it: GO stuff & nothing else }
// 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() homeDir, err := os.UserHomeDir()
if err != nil { if err != nil {
return "", err return "", err
} }
pwd = filepath.Join(homeDir, "go/src") pwd := filepath.Join(homeDir, "go/src")
shell.Mkdir(pwd) shell.Mkdir(pwd)
os.Chdir(pwd) os.Chdir(pwd)
return pwd, nil return pwd, nil