go mod init

Signed-off-by: jolheiser <john.olheiser@gmail.com>
main v0.0.1
jolheiser 2021-09-19 22:08:35 -05:00
commit 7a37a54485
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
6 changed files with 93 additions and 0 deletions

2
.gitignore vendored 100644
View File

@ -0,0 +1,2 @@
/gomodinit*
.idea/

19
LICENSE 100644
View File

@ -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.

14
README.md 100644
View File

@ -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)

5
go.mod 100644
View File

@ -0,0 +1,5 @@
module go.jolheiser.com/gomodinit
go 1.17
require github.com/peterbourgon/ff/v3 v3.1.0

7
go.sum 100644
View File

@ -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=

46
main.go 100644
View File

@ -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))
}