From 9222b99fd153faead9a24d55a09580898a4acc2b Mon Sep 17 00:00:00 2001 From: John Olheiser Date: Sat, 12 Sep 2020 19:53:28 +0000 Subject: [PATCH] QoL (#1) Update dependencies and add some QoL enhancements Signed-off-by: jolheiser Co-authored-by: jolheiser Reviewed-on: https://gitea.com/jolheiser/git-import/pulls/1 --- .golangci.yml | 23 -------- Makefile | 80 ++------------------------- go.mod | 6 +- go.sum | 8 +++ main.go | 149 +++++++++++++++++++++++++++++++++++++------------- main_test.go | 32 +++++------ parse.go | 16 ++++-- 7 files changed, 156 insertions(+), 158 deletions(-) delete mode 100644 .golangci.yml diff --git a/.golangci.yml b/.golangci.yml deleted file mode 100644 index 6d71439..0000000 --- a/.golangci.yml +++ /dev/null @@ -1,23 +0,0 @@ -linters: - enable: - - deadcode - - dogsled - - dupl - - errcheck - - funlen - - gocognit - - goconst - - gocritic - - gocyclo - - gofmt - - golint - - gosimple - - govet - - misspell - - prealloc - - staticcheck - - structcheck - - typecheck - - unparam - - unused - - varcheck \ No newline at end of file diff --git a/Makefile b/Makefile index 16ede18..69eb50e 100644 --- a/Makefile +++ b/Makefile @@ -1,32 +1,9 @@ -DIST := dist GO ?= go -SHASUM ?= shasum -a 256 - -ifneq ($(DRONE_TAG),) - VERSION ?= $(subst v,,$(DRONE_TAG)) - LONG_VERSION ?= $(VERSION) -else - ifneq ($(DRONE_BRANCH),) - VERSION ?= $(subst release/v,,$(DRONE_BRANCH)) - else - VERSION ?= master - endif - LONG_VERSION ?= $(shell git describe --tags --always | sed 's/-/+/' | sed 's/^v//') -endif - -LDFLAGS := $(LDFLAGS) -X "main.Version=$(LONG_VERSION)" +VERSION ?= $(shell git describe --tags --always | sed 's/-/+/' | sed 's/^v//') .PHONY: build build: - $(GO) build -ldflags '-s -w $(LDFLAGS)' - -.PHONY: lint -lint: - @hash golangci-lint > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ - export BINARY="golangci-lint"; \ - curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.23.1; \ - fi - golangci-lint run --timeout 5m + $(GO) build -ldflags '-s -w -X "main.Version=$(VERSION)"' .PHONY: fmt fmt: @@ -34,53 +11,8 @@ fmt: .PHONY: test test: - $(GO) test -race ./... + $(GO) test --race ./... -.PHONY: release -release: release-dirs check-xgo release-windows release-linux release-darwin release-copy release-compress release-check - -.PHONY: check-xgo -check-xgo: - @hash xgo > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ - $(GO) get -u src.techknowlogick.com/xgo; \ - fi - -.PHONY: release-dirs -release-dirs: - mkdir -p $(DIST)/binaries $(DIST)/release - -.PHONY: release-windows -release-windows: - xgo -dest $(DIST)/binaries -ldflags '-linkmode external -extldflags "-static" $(LDFLAGS)' -targets 'windows/*' -out git-import-$(VERSION) . -ifeq ($(CI),drone) - cp /build/* $(DIST)/binaries -endif - -.PHONY: release-linux -release-linux: - xgo -dest $(DIST)/binaries -ldflags '-linkmode external -extldflags "-static" $(LDFLAGS)' -targets 'linux/amd64,linux/386,linux/arm-5,linux/arm-6,linux/arm64,linux/mips64le,linux/mips,linux/mipsle' -out git-import-$(VERSION) . -ifeq ($(CI),drone) - cp /build/* $(DIST)/binaries -endif - -.PHONY: release-darwin -release-darwin: - xgo -dest $(DIST)/binaries -tags 'netgo osusergo $(TAGS)' -ldflags '$(LDFLAGS)' -targets 'darwin/*' -out git-import-$(VERSION) . -ifeq ($(CI),drone) - cp /build/* $(DIST)/binaries -endif - -.PHONY: release-copy -release-copy: - cd $(DIST); for file in `find /build -type f -name "*"`; do cp $${file} ./release/; done; - -.PHONY: release-check -release-check: - cd $(DIST)/release/; for file in `find . -type f -name "*"`; do echo "checksumming $${file}" && $(SHASUM) `echo $${file} | sed 's/^..//'` > $${file}.sha256; done; - -.PHONY: release-compress -release-compress: - @hash gxz > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ - $(GO) get -u github.com/ulikunitz/xz/cmd/gxz; \ - fi - cd $(DIST)/release/; for file in `find . -type f -name "*"`; do echo "compressing $${file}" && gxz -k -9 $${file}; done; \ No newline at end of file +.PHONY: vet +vet: + $(GO) vet ./... \ No newline at end of file diff --git a/go.mod b/go.mod index 9765a80..4cae73e 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,9 @@ module go.jolheiser.com/git-import go 1.12 require ( + github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect github.com/mattn/go-isatty v0.0.8 // indirect - github.com/urfave/cli/v2 v2.1.1 - go.jolheiser.com/beaver v1.0.1 + github.com/urfave/cli/v2 v2.2.0 + go.jolheiser.com/beaver v1.0.2 + golang.org/x/sys v0.0.0-20200909081042-eff7692f9009 // indirect ) diff --git a/go.sum b/go.sum index b2f030e..05e10bf 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/cpuguy83/go-md2man/v2 v2.0.0 h1:EoUDS0afbrsXAZ9YQ9jdu/mZ2sXgT1/2yyNng4PGlyM= +github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE= github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -11,8 +13,12 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV github.com/urfave/cli v1.22.2 h1:gsqYFH8bb9ekPA12kRo0hfjngWQjkJPlN9R0N78BoUo= github.com/urfave/cli/v2 v2.1.1 h1:Qt8FeAtxE/vfdrLmR3rxR6JRE0RoVmbXu8+6kZtYU4k= github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= +github.com/urfave/cli/v2 v2.2.0 h1:JTTnM6wKzdA0Jqodd966MVj4vWbbquZykeX1sKbe2C4= +github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= go.jolheiser.com/beaver v1.0.1 h1:gt3aGEr5Bj4ZjDF1g8t8OYOGRCRXGaanGR9CmXUxez8= go.jolheiser.com/beaver v1.0.1/go.mod h1:7X4F5+XOGSC3LejTShoBdqtRCnPWcnRgmYGmG3EKW8g= +go.jolheiser.com/beaver v1.0.2 h1:KA2D6iO8MQhZi1nZYi/Chak/f1Cxfrs6b1XO623+Khk= +go.jolheiser.com/beaver v1.0.2/go.mod h1:7X4F5+XOGSC3LejTShoBdqtRCnPWcnRgmYGmG3EKW8g= go.jolheiser.com/beaver v1.1.1 h1:WCbTD76qMzsaZ9EOZh8tTrjRKUFludYWmepXakTcnPQ= go.jolheiser.com/beaver v1.1.1/go.mod h1:InRbUdHTqDYNk0SB1GyO9nJRczk5gKOMmuwpyj6FlAM= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -20,5 +26,7 @@ golang.org/x/sys v0.0.0-20190710143415-6ec70d6a5542 h1:6ZQFf1D2YYDDI7eSwW8adlkka golang.org/x/sys v0.0.0-20190710143415-6ec70d6a5542/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ= golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200909081042-eff7692f9009 h1:W0lCpv29Hv0UaM1LXb9QlBHLNP8UFfcKjblhVCWftOM= +golang.org/x/sys v0.0.0-20200909081042-eff7692f9009/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go index a68b8b0..bbfc8db 100644 --- a/main.go +++ b/main.go @@ -3,15 +3,26 @@ package main import ( "errors" "fmt" - "github.com/urfave/cli/v2" - "go.jolheiser.com/beaver" "net/http" + "net/url" "os" "os/exec" - "strings" + + "github.com/urfave/cli/v2" + "go.jolheiser.com/beaver" ) -var Version = "develop" +var ( + Version = "develop" + + output string + author string + email string + gpg string + display bool + ssh bool + debug bool +) func main() { app := cli.NewApp() @@ -20,21 +31,51 @@ func main() { app.Version = Version app.Flags = []cli.Flag{ &cli.BoolFlag{ - Name: "display", - Aliases: []string{"d"}, - Usage: "Display URL instead of cloning", + Name: "display", + Aliases: []string{"d"}, + Usage: "Display URL instead of cloning", + Destination: &display, }, &cli.BoolFlag{ - Name: "ssh", - Aliases: []string{"s"}, - Usage: "Use SSH if available", + Name: "ssh", + Aliases: []string{"s"}, + Usage: "Use SSH if available", + Destination: &ssh, + }, + &cli.StringFlag{ + Name: "output", + Aliases: []string{"o"}, + Usage: "Path to output (default: git-import name)", + Destination: &output, + }, + &cli.StringFlag{ + Name: "author", + Aliases: []string{"a"}, + Usage: "Signature author", + Destination: &author, + }, + &cli.StringFlag{ + Name: "email", + Aliases: []string{"e"}, + Usage: "Signature email", + Destination: &email, + }, + &cli.StringFlag{ + Name: "gpg", + Aliases: []string{"g"}, + Usage: "GPG key to sign with", + Destination: &gpg, + }, + &cli.BoolFlag{ + Name: "debug", + Usage: "Enable debug logging", + Destination: &debug, }, } app.UseShortOptionHandling = true - app.EnableBashCompletion = true app.Action = doImport - err := app.Run(os.Args) - if err != nil { + + if err := app.Run(os.Args); err != nil { beaver.Error(err) } } @@ -43,14 +84,24 @@ func doImport(ctx *cli.Context) error { if ctx.NArg() < 1 { return errors.New("must specify an import URL") } - importURL := ctx.Args().First() - if !strings.HasPrefix(importURL, "https") { - importURL = fmt.Sprintf("http://%s", importURL) + + if debug { + beaver.Console.Level = beaver.DEBUG } - res, err := http.Get(importURL) + importURL := ctx.Args().First() + u, err := url.Parse(importURL) if err != nil { - return fmt.Errorf("could not request URL `%s`: %v", importURL, err) + return err + } + if u.Scheme == "" { + u.Scheme = "http" + } + + beaver.Debugf("Getting git-import from %s", u.String()) + res, err := http.Get(u.String()) + if err != nil { + return fmt.Errorf("could not request URL `%s`: %v", u.String(), err) } defer res.Body.Close() @@ -58,33 +109,33 @@ func doImport(ctx *cli.Context) error { if err != nil { return fmt.Errorf("could not parse: %v", err) } + beaver.Debug(gitImport) - if ctx.Bool("display") { - if ctx.Bool("ssh") { + if display { + if ssh { fmt.Println(gitImport.SSH) return nil } - fmt.Println(gitImport.HTTPS) + fmt.Println(gitImport.HTTP) return nil } - return doClone(ctx, gitImport) + return doClone(gitImport) } -func doClone(ctx *cli.Context, gitImport GitImport) error { - projectName := gitImport.ProjectName - if ctx.NArg() > 1 { - projectName = ctx.Args().Get(1) +func doClone(gitImport GitImport) error { + if output == "" { + output = gitImport.Name } - cmd := exec.Command("git", "clone", gitImport.HTTPS, projectName) - if ctx.Bool("ssh") { + cmd := exec.Command("git", "clone", gitImport.HTTP, output) + if ssh { if gitImport.SSH == "" { return errors.New("SSH was not provided by git-import") } if os.Getenv("GIT_SSH_COMMAND") == "" { return errors.New("no environment variable found for GIT_SSH_COMMAND") } - cmd = exec.Command("git", "clone", gitImport.SSH, projectName) + cmd = exec.Command("git", "clone", gitImport.SSH, output) } cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr @@ -93,16 +144,40 @@ func doClone(ctx *cli.Context, gitImport GitImport) error { return fmt.Errorf("could not clone: %v", err) } - if ctx.Bool("ssh") { - if err := os.Chdir(projectName); err != nil { - return fmt.Errorf("could not change to `%s` directory. Git config will not store SSH command", projectName) + if ssh { + if err := os.Chdir(output); err != nil { + return fmt.Errorf("could not change to `%s` directory. Git config will not store SSH command", output) } - cmd := exec.Command("git", "config", "--local", "core.sshCommand", os.Getenv("GIT_SSH_COMMAND")) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - if err := cmd.Run(); err != nil { - return fmt.Errorf("could not configure SSH: %v", err) + if err := gitConfig("core.sshCommand", os.Getenv("GIT_SSH_COMMAND")); err != nil { + beaver.Errorf("could not configure SSH: %v", err) } } + + if author != "" { + if err := gitConfig("user.name", author); err != nil { + beaver.Errorf("could not configure author: %v", err) + } + } + if email != "" { + if err := gitConfig("user.email", email); err != nil { + beaver.Errorf("could not configure email: %v", err) + } + } + if gpg != "" { + if err := gitConfig("user.signingkey", gpg); err != nil { + beaver.Errorf("could not configure GPG key: %v", err) + } + if err := gitConfig("commit.gpgsign", "true"); err != nil { + beaver.Errorf("could not configure GPG signing: %v", err) + } + } + return nil } + +func gitConfig(key, value string) error { + cmd := exec.Command("git", "config", "--local", key, value) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + return cmd.Run() +} diff --git a/main_test.go b/main_test.go index 531f832..4dc657a 100644 --- a/main_test.go +++ b/main_test.go @@ -10,14 +10,14 @@ import ( ) var ( - name = "repo" - https = "https://www.git.com/user/repo" - ssh = "git@git.com:user/repo" + name = "repo" + cloneHTTP = "http://www.git.com/user/repo.git" + cloneSSH = "git@git.com:user/repo.git" - tpl1 = `` - tpl2 = `` - tpl3 = `` - tpl4 = `` + tpl1 = `` + tpl2 = `` + tpl3 = `` + tpl4 = `` h1 = handle1{} h2 = handle2{} h3 = handle3{} @@ -37,12 +37,12 @@ func TestGitImport(t *testing.T) { value string err bool }{ - {name: "HTTPS 1", handler: h1, value: https}, - {name: "HTTPS 2", handler: h2, value: https}, + {name: "HTTPS 1", handler: h1, value: cloneHTTP}, + {name: "HTTPS 2", handler: h2, value: cloneHTTP}, {name: "HTTPS 3", handler: h3, err: true}, {name: "SSH 1", handler: h1, ssh: true, value: ""}, - {name: "SSH 2", handler: h2, ssh: true, value: ssh}, + {name: "SSH 2", handler: h2, ssh: true, value: cloneSSH}, {name: "SSH 4", handler: h4, ssh: true, err: true}, } @@ -60,16 +60,16 @@ func TestGitImport(t *testing.T) { t.FailNow() } if tc.err { - format := "GitImport.ProjectName: %s\nGitImport.HTTPS: %s\nGitImport.SSH: %s" - formatted := fmt.Sprintf(format, gi.ProjectName, gi.HTTPS, gi.SSH) + format := "GitImport.Name: %s\nGitImport.HTTP: %s\nGitImport.SSH: %s" + formatted := fmt.Sprintf(format, gi.Name, gi.HTTP, gi.SSH) t.Logf("test-case should have produced an error\n%s", formatted) t.FailNow() } - if gi.ProjectName != name { - expected(t, name, gi.ProjectName) + if gi.Name != name { + expected(t, name, gi.Name) } - if !tc.ssh && gi.HTTPS != tc.value { - expected(t, tc.value, gi.HTTPS) + if !tc.ssh && gi.HTTP != tc.value { + expected(t, tc.value, gi.HTTP) t.FailNow() } if tc.ssh && gi.SSH != tc.value { diff --git a/parse.go b/parse.go index 0a5eee6..42eb4aa 100644 --- a/parse.go +++ b/parse.go @@ -16,9 +16,13 @@ var ( ) type GitImport struct { - ProjectName string - HTTPS string - SSH string + Name string + HTTP string + SSH string +} + +func (g GitImport) String() string { + return fmt.Sprintf("%s %s %s", g.Name, g.HTTP, g.SSH) } // charsetReader returns a reader for the given charset. @@ -69,8 +73,8 @@ func parseMetaGitImport(r io.Reader) (gitImport GitImport, err error) { break } gitImport = GitImport{ - ProjectName: f[0], - HTTPS: f[1], + Name: f[0], + HTTP: f[1], } if len(f) >= 3 { if !SSHRegex.MatchString(f[2]) { @@ -81,7 +85,7 @@ func parseMetaGitImport(r io.Reader) (gitImport GitImport, err error) { } err = nil } else { - err = fmt.Errorf("incorrect number of import arguments\n\n wanted: project_name https://www.myproject.com/repo [git@myproject.com:repo]\n got: %s", content) + err = fmt.Errorf("incorrect number of import arguments\n\n wanted: project_name cloneHTTP://www.myproject.com/repo [git@myproject.com:repo]\n got: %s", content) } break }