Compare commits
4 Commits
Author | SHA1 | Date |
---|---|---|
jolheiser | 6686d64913 | |
jolheiser | 0ee47079f0 | |
jolheiser | 91cbf15887 | |
jolheiser | b7d07b1d84 |
|
@ -1,2 +1,3 @@
|
||||||
/gomodinit*
|
/gomodinit
|
||||||
|
/gomodinit.exe
|
||||||
.idea/
|
.idea/
|
|
@ -0,0 +1,25 @@
|
||||||
|
builds:
|
||||||
|
- env:
|
||||||
|
- CGO_ENABLED=0
|
||||||
|
goos:
|
||||||
|
- linux
|
||||||
|
- windows
|
||||||
|
- darwin
|
||||||
|
ldflags:
|
||||||
|
- "-s -w -X main.Version={{.Version}}"
|
||||||
|
archives:
|
||||||
|
- replacements:
|
||||||
|
386: i386
|
||||||
|
amd64: x86_64
|
||||||
|
format_overrides:
|
||||||
|
- goos: windows
|
||||||
|
format: zip
|
||||||
|
checksum:
|
||||||
|
name_template: 'checksums.txt'
|
||||||
|
release:
|
||||||
|
gitea:
|
||||||
|
owner: jolheiser
|
||||||
|
name: gomodinit
|
||||||
|
gitea_urls:
|
||||||
|
api: https://git.jojodev.com/api/v1/
|
||||||
|
download: https://git.jojodev.com
|
|
@ -0,0 +1,38 @@
|
||||||
|
clone:
|
||||||
|
git:
|
||||||
|
image: woodpeckerci/plugin-git
|
||||||
|
settings:
|
||||||
|
tags: true
|
||||||
|
|
||||||
|
pipeline:
|
||||||
|
compliance:
|
||||||
|
image: golang:1.18
|
||||||
|
commands:
|
||||||
|
- go test -race ./...
|
||||||
|
- go vet ./...
|
||||||
|
when:
|
||||||
|
event: pull_request
|
||||||
|
|
||||||
|
build:
|
||||||
|
image: goreleaser/goreleaser
|
||||||
|
commands:
|
||||||
|
- goreleaser build --snapshot
|
||||||
|
when:
|
||||||
|
event: pull_request
|
||||||
|
|
||||||
|
release:
|
||||||
|
image: goreleaser/goreleaser
|
||||||
|
commands:
|
||||||
|
- goreleaser release
|
||||||
|
secrets: [ gitea_token ]
|
||||||
|
when:
|
||||||
|
event: tag
|
||||||
|
|
||||||
|
prune:
|
||||||
|
image: jolheiser/drone-gitea-prune
|
||||||
|
settings:
|
||||||
|
base: https://git.jojodev.com
|
||||||
|
token:
|
||||||
|
from_secret: gitea_token
|
||||||
|
when:
|
||||||
|
event: tag
|
|
@ -2,8 +2,8 @@
|
||||||
|
|
||||||
`go mod init`
|
`go mod init`
|
||||||
|
|
||||||
1. Set your base URL prefix via flag or env variable
|
`gomodinit` walks up the current dir path until it finds something vaguely resembling a URI (i.e. a path part with at least one `.`)
|
||||||
2. `gomodinit` will default to the current directory name as the module, otherwise the first command argument
|
and constructs a `go mod init <path>` call from it.
|
||||||
|
|
||||||
## This seems like a trivial thing
|
## This seems like a trivial thing
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
{
|
||||||
|
"nodes": {
|
||||||
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1703206931,
|
||||||
|
"narHash": "sha256-GHJl3HHdH4jWjRVBuyDRIaUoNNMTuVbw0i9+EaYzcBU=",
|
||||||
|
"owner": "nixos",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "bfbfb344598544058481e254b9f0f174d05b5e6a",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "nixos",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"inputs": {
|
||||||
|
"nixpkgs": "nixpkgs"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": "root",
|
||||||
|
"version": 7
|
||||||
|
}
|
|
@ -0,0 +1,39 @@
|
||||||
|
{
|
||||||
|
description = "go mod init";
|
||||||
|
|
||||||
|
inputs.nixpkgs.url = "github:nixos/nixpkgs";
|
||||||
|
|
||||||
|
outputs = {
|
||||||
|
self,
|
||||||
|
nixpkgs,
|
||||||
|
}: let
|
||||||
|
system = "x86_64-linux";
|
||||||
|
pkgs = nixpkgs.legacyPackages.${system};
|
||||||
|
in {
|
||||||
|
packages.${system}.default = pkgs.buildGoModule rec {
|
||||||
|
pname = "gomodinit";
|
||||||
|
version = "0.1.1";
|
||||||
|
src = ./.;
|
||||||
|
vendorHash = "sha256-7RMTRFHYgzQD7K5JDdjV7ABg6BpCZbQXS9gSRlm27X8=";
|
||||||
|
ldflags = [
|
||||||
|
"-s"
|
||||||
|
"-w"
|
||||||
|
"-X=main.Version=${version}"
|
||||||
|
];
|
||||||
|
|
||||||
|
meta = with pkgs.lib; {
|
||||||
|
description = "go mod init";
|
||||||
|
homepage = "https://git.jojodev.com/jolheiser/gomodinit";
|
||||||
|
maintainers = with maintainers; [jolheiser];
|
||||||
|
mainProgram = "gomodinit";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
devShells.${system}.default = pkgs.mkShell {
|
||||||
|
nativeBuildInputs = with pkgs; [
|
||||||
|
go
|
||||||
|
gopls
|
||||||
|
];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
7
go.mod
7
go.mod
|
@ -1,5 +1,8 @@
|
||||||
module go.jolheiser.com/gomodinit
|
module go.jolheiser.com/gomodinit
|
||||||
|
|
||||||
go 1.17
|
go 1.18
|
||||||
|
|
||||||
require github.com/peterbourgon/ff/v3 v3.1.0
|
require (
|
||||||
|
github.com/matryer/is v1.4.0
|
||||||
|
github.com/peterbourgon/ff/v3 v3.1.2
|
||||||
|
)
|
||||||
|
|
6
go.sum
6
go.sum
|
@ -1,7 +1,9 @@
|
||||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
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/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/matryer/is v1.4.0 h1:sosSmIWwkYITGrxZ25ULNDeKiMNzFSr4V/eqBQP0PeE=
|
||||||
|
github.com/matryer/is v1.4.0/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU=
|
||||||
github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys=
|
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.2 h1:0GNhbRhO9yHA4CC27ymskOsuRpmX0YQxwxM9UPiP6JM=
|
||||||
github.com/peterbourgon/ff/v3 v3.1.0/go.mod h1:XNJLY8EIl6MjMVjBS4F0+G0LYoAqs0DTa4rmHHukKDE=
|
github.com/peterbourgon/ff/v3 v3.1.2/go.mod h1:XNJLY8EIl6MjMVjBS4F0+G0LYoAqs0DTa4rmHHukKDE=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
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=
|
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
|
|
@ -0,0 +1,61 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/matryer/is"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestModule(t *testing.T) {
|
||||||
|
tmp := t.TempDir()
|
||||||
|
|
||||||
|
tt := []struct {
|
||||||
|
Name string
|
||||||
|
Path string
|
||||||
|
Expected string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Name: "gitea",
|
||||||
|
Path: "gitea.com/user1/repo",
|
||||||
|
Expected: "gitea.com/user1/repo",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "gitea_sub",
|
||||||
|
Path: "gitea.com/user2/repo/subpkg",
|
||||||
|
Expected: "gitea.com/user2/repo/subpkg",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "no_uri",
|
||||||
|
Path: "giteacom/user3/repo",
|
||||||
|
Expected: "%s/giteacom/user3/repo",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tt {
|
||||||
|
t.Run(tc.Name, func(t *testing.T) {
|
||||||
|
assert := is.New(t)
|
||||||
|
|
||||||
|
dir := filepath.Join(tmp, tc.Path)
|
||||||
|
|
||||||
|
err := os.MkdirAll(dir, os.ModePerm)
|
||||||
|
assert.NoErr(err) // Should create temp dir
|
||||||
|
|
||||||
|
err = os.Chdir(dir)
|
||||||
|
assert.NoErr(err) // Should chdir to temp dir
|
||||||
|
|
||||||
|
name, err := module()
|
||||||
|
assert.NoErr(err) // Should get module name
|
||||||
|
|
||||||
|
expected := tc.Expected
|
||||||
|
if strings.Contains(expected, "%s") {
|
||||||
|
expected = fmt.Sprintf(expected, tmp)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(name, expected) // Module name should match expected name
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
54
main.go
54
main.go
|
@ -11,36 +11,54 @@ import (
|
||||||
"github.com/peterbourgon/ff/v3"
|
"github.com/peterbourgon/ff/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var Version = "develop"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fs := flag.NewFlagSet("gomodinit", flag.ExitOnError)
|
fs := flag.NewFlagSet("gomodinit", flag.ExitOnError)
|
||||||
base := fs.String("base", "", "Base URL prefix for module")
|
versionFlag := fs.Bool("version", false, "Print version and exit")
|
||||||
if err := ff.Parse(fs, os.Args[1:], ff.WithEnvVarPrefix("GMI")); err != nil {
|
if err := ff.Parse(fs, os.Args[1:], ff.WithEnvVarPrefix("GMI")); err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if *base == "" {
|
if *versionFlag {
|
||||||
fmt.Println("Base URL is required")
|
fmt.Printf("gomodinit %s\n", Version)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
dir, err := os.Getwd()
|
var name string
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
name := filepath.Base(dir)
|
|
||||||
if fs.NArg() > 0 {
|
if fs.NArg() > 0 {
|
||||||
name = fs.Arg(0)
|
name = fs.Arg(0)
|
||||||
|
} else {
|
||||||
|
n, err := module()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
name = n
|
||||||
}
|
}
|
||||||
|
|
||||||
*base = strings.TrimSuffix(*base, "/")
|
cmd := exec.Command("go", "mod", "init", name)
|
||||||
|
cmd.Stdin = os.Stdin
|
||||||
out, err := exec.Command("go", "mod", "init", fmt.Sprintf("%s/%s", *base, name)).CombinedOutput()
|
cmd.Stdout = os.Stdout
|
||||||
if err != nil {
|
cmd.Stderr = os.Stderr
|
||||||
fmt.Println(err)
|
_ = cmd.Run()
|
||||||
return
|
}
|
||||||
}
|
|
||||||
fmt.Print(string(out))
|
func module() (string, error) {
|
||||||
|
dir, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
list := strings.Split(dir, string(filepath.Separator))
|
||||||
|
name := list[len(list)-1]
|
||||||
|
for idx := len(list) - 2; idx >= 0; idx-- {
|
||||||
|
part := list[idx]
|
||||||
|
name = fmt.Sprintf("%s/%s", part, name)
|
||||||
|
if strings.Contains(part, ".") {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return name, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue