Move to lib/cli and add drone
continuous-integration/drone/pr Build is passing
Details
continuous-integration/drone/pr Build is passing
Details
Signed-off-by: jolheiser <john.olheiser@gmail.com>pull/5/head
parent
3c1a19d31c
commit
6930b30648
|
@ -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"
|
|
@ -2,4 +2,4 @@
|
|||
.idea
|
||||
|
||||
# Binaries
|
||||
/git-import*
|
||||
/git-get*
|
6
Makefile
6
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:
|
||||
|
|
31
README.md
31
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
|
||||
<meta name="git-import" content="git-import https://gitea.com/jolheiser/git-import.git git@gitea.com:jolheiser/git-import.git" />
|
||||
<meta name="git-import" content="git-import https://git.jojodev.com/jolheiser/git-import.git git@git.jojodev.com:jolheiser/git-import.git" />
|
||||
```
|
||||
|
||||
## 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`
|
||||
`git get -display -ssh go.jolheiser.com/git-import`
|
|
@ -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 <import-url>", 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 <import-url>",
|
||||
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()
|
||||
}
|
4
go.mod
4
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
|
||||
)
|
||||
|
|
30
go.sum
30
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=
|
||||
|
|
|
@ -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 <head> section or the beginning of the <body>.
|
||||
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
|
|
@ -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
|
192
main.go
192
main.go
|
@ -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()
|
||||
}
|
Loading…
Reference in New Issue