commit 7a37a544859e1cb0b2c6219dcc974a1276ca2235 Author: jolheiser Date: Sun Sep 19 22:08:35 2021 -0500 go mod init Signed-off-by: jolheiser diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..edd2caf --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/gomodinit* +.idea/ \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..433f7db --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2021 John Olheiser + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..7fcba9c --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +# gomodinit + +`go mod init` + +1. Set your base URL prefix via flag or env variable +2. `gomodinit` will default to the current directory name as the module, otherwise the first command argument + +## This seems like a trivial thing + +very. + +## License + +[MIT](LICENSE) \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..00896c0 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module go.jolheiser.com/gomodinit + +go 1.17 + +require github.com/peterbourgon/ff/v3 v3.1.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..5dc37d8 --- /dev/null +++ b/go.sum @@ -0,0 +1,7 @@ +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys= +github.com/peterbourgon/ff/v3 v3.1.0 h1:5JAeDK5j/zhKFjyHEZQXwXBoDijERaos10RE+xamOsY= +github.com/peterbourgon/ff/v3 v3.1.0/go.mod h1:XNJLY8EIl6MjMVjBS4F0+G0LYoAqs0DTa4rmHHukKDE= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go new file mode 100644 index 0000000..3f702ac --- /dev/null +++ b/main.go @@ -0,0 +1,46 @@ +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)) +}