From 081c31324c997faede810da3805446b45ec22a23 Mon Sep 17 00:00:00 2001 From: jolheiser Date: Sat, 12 Sep 2020 14:48:33 -0500 Subject: [PATCH 1/4] Update dependencies and add some QoL enhancements Signed-off-by: jolheiser --- .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 } -- 2.41.0 From 9222b99fd153faead9a24d55a09580898a4acc2b Mon Sep 17 00:00:00 2001 From: John Olheiser Date: Sat, 12 Sep 2020 19:53:28 +0000 Subject: [PATCH 2/4] 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 } -- 2.41.0 From 3c1a19d31cb8afb235f7ddfbadadf373722a266c Mon Sep 17 00:00:00 2001 From: John Olheiser Date: Mon, 22 Feb 2021 04:22:34 +0800 Subject: [PATCH 3/4] Update query parameter and make HTTPS by default (#4) Resolves #2 Resolves #3 Co-authored-by: jolheiser Reviewed-on: https://gitea.com/jolheiser/git-import/pulls/4 Co-authored-by: John Olheiser Co-committed-by: John Olheiser --- main.go | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index bbfc8db..f953d9a 100644 --- a/main.go +++ b/main.go @@ -15,13 +15,14 @@ import ( var ( Version = "develop" - output string - author string - email string - gpg string - display bool - ssh bool - debug bool + output string + author string + email string + gpg string + display bool + ssh bool + insecure bool + debug bool ) func main() { @@ -66,13 +67,17 @@ func main() { Usage: "GPG key to sign with", Destination: &gpg, }, + &cli.BoolFlag{ + Name: "insecure", + Usage: "Use HTTP instead of HTTPS by default", + Destination: &insecure, + }, &cli.BoolFlag{ Name: "debug", Usage: "Enable debug logging", Destination: &debug, }, } - app.UseShortOptionHandling = true app.Action = doImport if err := app.Run(os.Args); err != nil { @@ -95,8 +100,12 @@ func doImport(ctx *cli.Context) error { return err } if u.Scheme == "" { - u.Scheme = "http" + u.Scheme = "https" + if insecure { + u.Scheme = "http" + } } + u.RawQuery = "git-import=1" beaver.Debugf("Getting git-import from %s", u.String()) res, err := http.Get(u.String()) -- 2.41.0 From ea7a455d75f2dba7a7367e605911c7045ab02dc0 Mon Sep 17 00:00:00 2001 From: jolheiser Date: Tue, 22 Jun 2021 05:04:10 +0000 Subject: [PATCH 4/4] Move to lib/cli and add drone (#5) Reviewed-on: https://git.jojodev.com/jolheiser/git-import/pulls/5 Co-authored-by: jolheiser Co-committed-by: jolheiser --- .drone.yml | 41 +++++++ .gitignore | 2 +- Makefile | 6 +- README.md | 31 +++--- cmd/git-get/main.go | 167 ++++++++++++++++++++++++++++ go.mod | 4 +- go.sum | 30 +----- parse.go => import.go | 6 +- main_test.go => import_test.go | 4 +- main.go | 192 --------------------------------- 10 files changed, 239 insertions(+), 244 deletions(-) create mode 100644 .drone.yml create mode 100644 cmd/git-get/main.go rename parse.go => import.go (94%) rename main_test.go => import_test.go (98%) delete mode 100644 main.go diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..084ce61 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,41 @@ +--- +kind: pipeline +name: compliance +trigger: + event: + - pull_request +steps: + - name: test + pull: always + image: golang:1.16 + commands: + - make test + - name: check + pull: always + image: golang:1.16 + commands: + - make vet + +--- +kind: pipeline +name: release +trigger: + event: + - push + branch: + - main +steps: + - name: build + pull: always + image: golang:1.16 + commands: + - make build + - name: gitea-release + pull: always + image: jolheiser/drone-gitea-main:latest + settings: + token: + from_secret: gitea_token + base: https://git.jojodev.com + files: + - "git-get" \ No newline at end of file diff --git a/.gitignore b/.gitignore index 4f2918a..8702c0d 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,4 @@ .idea # Binaries -/git-import* \ No newline at end of file +/git-get* \ No newline at end of file diff --git a/Makefile b/Makefile index 69eb50e..29ce984 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,11 @@ VERSION ?= $(shell git describe --tags --always | sed 's/-/+/' | sed 's/^v//') .PHONY: build build: - $(GO) build -ldflags '-s -w -X "main.Version=$(VERSION)"' + $(GO) build -ldflags '-w -X "main.Version=$(VERSION)"' go.jolheiser.com/git-import/cmd/git-get + +.PHONY: install +install: + $(GO) install -ldflags '-w -X "main.Version=$(VERSION)"' go.jolheiser.com/git-import/cmd/git-get .PHONY: fmt fmt: diff --git a/README.md b/README.md index f18d73c..e286d49 100644 --- a/README.md +++ b/README.md @@ -4,40 +4,37 @@ Similar to `go-import`, a way to create vanity URLs for git repository paths. Information on `go-import` can be found on the [golang website](https://golang.org/cmd/go/#hdr-Remote_import_paths) -`git-import` strives to work in a similar manner +`git-import` strives to work similarly By providing a `meta` tag with appropriate information, `git-import` will clone the specified repository -The following is the `meta` tag for this repository, hosted on https://go.jolheiser.com/git-import with [Vanity](https://gitea.com/jolheiser/vanity) +The following is the `meta` tag for this repository, hosted on https://go.jolheiser.com/git-import with [Vanity](https://git.jojodev.com/jolheiser/vanity) ```html - + ``` +## Installation (Git extension) + +To install the Git extension: + +`go install go.jolheiser.com/git-import/cmd/git-get` ## SSH -`git-import` can set up SSH if applicable, however it must be ran with `GIT_SSH_COMMAND` set in order to configure the repository properly. +`git-get` can set up SSH if applicable, however it must be run with `GIT_SSH_COMMAND` set in order to configure the repository properly. ## Examples Clone this repository -`git-import go.jolheiser.com/git-import` +`git get go.jolheiser.com/git-import` Clone this repository with SSH -`GIT_SSH_COMMAND="/usr/bin/ssh -i /home/user/.ssh/id_rsa" git-import -s go.jolheiser.com/git-import` +`GIT_SSH_COMMAND="/usr/bin/ssh -i /home/user/.ssh/id_rsa" git-import -ssh go.jolheiser.com/git-import` Clone this repository, but clone into "import-git" -`git-import go.jolheiser.com/git-import import-git` +`git get -out import-git go.jolheiser.com/git-import` Output the repository URL of this repo (without cloning) -`git-import -d go.jolheiser.com/git-import` +`git get -display go.jolheiser.com/git-import` Output the repository SSH URL of this repo (without cloning) -`git-import -d -s go.jolheiser.com/git-import` - -## Bonus Points - -Create a git alias for `git-import` -``` -git config --global alias.get '!/path/to/git-import' -``` -and use like `git get go.jolheiser.com/git-import` \ No newline at end of file +`git get -display -ssh go.jolheiser.com/git-import` \ No newline at end of file diff --git a/cmd/git-get/main.go b/cmd/git-get/main.go new file mode 100644 index 0000000..ff5cc04 --- /dev/null +++ b/cmd/git-get/main.go @@ -0,0 +1,167 @@ +package main + +import ( + "context" + "errors" + "flag" + "fmt" + "net/http" + "net/url" + "os" + "os/exec" + "path/filepath" + "strings" + + "go.jolheiser.com/git-import" + + "github.com/peterbourgon/ff/v3" + "github.com/peterbourgon/ff/v3/ffcli" + "go.jolheiser.com/beaver" +) + +var ( + Version = "develop" + + fs = flag.NewFlagSet("git-get ", flag.ExitOnError) + output = fs.String("out", "", "Path to clone into (default: git-import name)") + author = fs.String("author", "", "Signature Author ($GIT_IMPORT_AUTHOR)") + email = fs.String("email", "", "Signature Email ($GIT_IMPORT_EMAIL)") + gpg = fs.String("gpg", "", "GPG key to sign with ($GIT_IMPORT_GPG)") + noMeta = fs.Bool("no-meta", false, "Don't check for meta, just clone") + display = fs.Bool("display", false, "Display URL instead of cloning") + ssh = fs.Bool("ssh", false, "Use SSH if $GIT_SSH_COMMAND is set") + insecure = fs.Bool("insecure", false, "Use HTTP instead of HTTPS by default") + debug = fs.Bool("debug", false, "Debug logging") +) + +func main() { + cmd := &ffcli.Command{ + Name: "git-get", + ShortUsage: "git-get ", + LongHelp: fmt.Sprintf("VERSION\n %s", Version), + FlagSet: fs, + Options: []ff.Option{ + ff.WithEnvVarPrefix("GIT_IMPORT"), + }, + Exec: doImport, + } + + if err := cmd.ParseAndRun(context.Background(), os.Args[1:]); err != nil { + beaver.Error(err) + } +} + +func doImport(_ context.Context, _ []string) error { + if *debug { + beaver.Console.Level = beaver.DEBUG + } + + if fs.NArg() == 0 { + fs.Usage() + return nil + } + + raw := fs.Arg(0) + if !strings.HasPrefix(raw, "http") { + raw = fmt.Sprintf("https://%s", raw) + } + + u, err := url.Parse(raw) + if err != nil { + return err + } + if *insecure { + u.Scheme = "http" + } + + var gitImport gimport.GitImport + if *noMeta { + p := strings.Split(u.Path, "/") + gitImport = gimport.GitImport{ + Name: p[len(p)-1], + HTTP: u.String(), + SSH: fmt.Sprintf("git@%s:%s.git", u.Host, u.Path[1:]), + } + } else { + u.RawQuery = "git-import=1" + 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() + gitImport, err = gimport.ParseMetaGitImport(res.Body) + if err != nil { + return fmt.Errorf("could not parse: %v", err) + } + } + beaver.Debug(gitImport) + + if *display { + if *ssh { + fmt.Println(gitImport.SSH) + return nil + } + fmt.Println(gitImport.HTTP) + return nil + } + return doClone(gitImport) +} + +func doClone(gitImport gimport.GitImport) error { + if *output == "" { + *output = gitImport.Name + } + + 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, *output) + } + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + if err := cmd.Run(); err != nil { + return fmt.Errorf("could not clone: %v", err) + } + + if *ssh { + 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.Dir = filepath.Join(".", *output) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + return cmd.Run() +} diff --git a/go.mod b/go.mod index 4cae73e..8b360bd 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,7 @@ 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.2.0 + github.com/peterbourgon/ff/v3 v3.0.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 05e10bf..9923a12 100644 --- a/go.sum +++ b/go.sum @@ -1,32 +1,12 @@ 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= -github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= -github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= -github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= -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= +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.0.0 h1:eQzEmNahuOjQXfuegsKQTSTDbf4dNvr/eNLrmJhiH7M= +github.com/peterbourgon/ff/v3 v3.0.0/go.mod h1:UILIFjRH5a/ar8TjXYLTkIvSvekZqPm5Eb/qbGk6CT0= 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= -golang.org/x/sys v0.0.0-20190710143415-6ec70d6a5542 h1:6ZQFf1D2YYDDI7eSwW8adlkkavTB9sw5I24FVtEvNUQ= -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= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/parse.go b/import.go similarity index 94% rename from parse.go rename to import.go index 42eb4aa..36fa977 100644 --- a/parse.go +++ b/import.go @@ -1,4 +1,4 @@ -package main +package gimport import ( "encoding/xml" @@ -35,9 +35,9 @@ func charsetReader(charset string, input io.Reader) (io.Reader, error) { } } -// parseMetaGoImports returns meta imports from the HTML in r. +// ParseMetaGitImport returns meta imports from the HTML in r. // Parsing ends at the end of the section or the beginning of the . -func parseMetaGitImport(r io.Reader) (gitImport GitImport, err error) { +func ParseMetaGitImport(r io.Reader) (gitImport GitImport, err error) { d := xml.NewDecoder(r) d.CharsetReader = charsetReader d.Strict = false diff --git a/main_test.go b/import_test.go similarity index 98% rename from main_test.go rename to import_test.go index 4dc657a..77f7ec1 100644 --- a/main_test.go +++ b/import_test.go @@ -1,4 +1,4 @@ -package main +package gimport import ( "fmt" @@ -51,7 +51,7 @@ func TestGitImport(t *testing.T) { rec := httptest.NewRecorder() req, _ := http.NewRequest(http.MethodGet, "", nil) tc.handler.ServeHTTP(rec, req) - gi, err := parseMetaGitImport(rec.Body) + gi, err := ParseMetaGitImport(rec.Body) if err != nil { if tc.err { return diff --git a/main.go b/main.go deleted file mode 100644 index f953d9a..0000000 --- a/main.go +++ /dev/null @@ -1,192 +0,0 @@ -package main - -import ( - "errors" - "fmt" - "net/http" - "net/url" - "os" - "os/exec" - - "github.com/urfave/cli/v2" - "go.jolheiser.com/beaver" -) - -var ( - Version = "develop" - - output string - author string - email string - gpg string - display bool - ssh bool - insecure bool - debug bool -) - -func main() { - app := cli.NewApp() - app.Name = "git-import" - app.Usage = "Import from vanity git URLs" - app.Version = Version - app.Flags = []cli.Flag{ - &cli.BoolFlag{ - 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", - 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: "insecure", - Usage: "Use HTTP instead of HTTPS by default", - Destination: &insecure, - }, - &cli.BoolFlag{ - Name: "debug", - Usage: "Enable debug logging", - Destination: &debug, - }, - } - app.Action = doImport - - if err := app.Run(os.Args); err != nil { - beaver.Error(err) - } -} - -func doImport(ctx *cli.Context) error { - if ctx.NArg() < 1 { - return errors.New("must specify an import URL") - } - - if debug { - beaver.Console.Level = beaver.DEBUG - } - - importURL := ctx.Args().First() - u, err := url.Parse(importURL) - if err != nil { - return err - } - if u.Scheme == "" { - u.Scheme = "https" - if insecure { - u.Scheme = "http" - } - } - u.RawQuery = "git-import=1" - - 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() - - gitImport, err := parseMetaGitImport(res.Body) - if err != nil { - return fmt.Errorf("could not parse: %v", err) - } - beaver.Debug(gitImport) - - if display { - if ssh { - fmt.Println(gitImport.SSH) - return nil - } - fmt.Println(gitImport.HTTP) - return nil - } - return doClone(gitImport) -} - -func doClone(gitImport GitImport) error { - if output == "" { - output = gitImport.Name - } - - 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, output) - } - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - - if err := cmd.Run(); err != nil { - return fmt.Errorf("could not clone: %v", err) - } - - 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) - } - 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() -} -- 2.41.0