package main import ( "flag" "fmt" "os" "os/exec" "path/filepath" "strings" "github.com/peterbourgon/ff/v3" ) func main() { fs := flag.NewFlagSet("gomodinit", flag.ExitOnError) base := fs.String("base", "", "Base URL prefix for module") if err := ff.Parse(fs, os.Args[1:], ff.WithEnvVarPrefix("GMI")); err != nil { fmt.Println(err) return } if *base == "" { fmt.Println("Base URL is required") return } dir, err := os.Getwd() if err != nil { fmt.Println(err) return } name := filepath.Base(dir) if fs.NArg() > 0 { name = fs.Arg(0) } *base = strings.TrimSuffix(*base, "/") out, err := exec.Command("go", "mod", "init", fmt.Sprintf("%s/%s", *base, name)).CombinedOutput() if err != nil { fmt.Println(err) return } fmt.Print(string(out)) }