Skip to content

Commit a786380

Browse files
authored
Merge pull request #2 from jwaldrip/advanced-repo-lookup
add lookup feature
2 parents 914606c + 63b405f commit a786380

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ $ echo "export GITPATH=$HOME/dev" > .zshrc
4747
$ git get [repo-url]
4848
```
4949

50+
### Auto host
51+
By default, if no host is specified, git-get will lookup repos by the provided
52+
path on github. You can use the following flags to override this:
53+
54+
* `--bitbucket`
55+
* `--github`
56+
* `--host {hostname}`
57+
* `-h {hostname}`
58+
59+
### Force SSH
60+
By default git-get will either use the scheme passed, or default to https:, to
61+
use SSH use the `--ssh` or `-S` flag.
62+
5063
## Example
5164

5265
```

main.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@ import (
1111
var app = cli.New(version, "clone a repo into a common path", get)
1212

1313
func init() {
14-
app.DefineBoolFlag("force", false, "overwrite existing directory")
14+
app.DefineStringFlag("host", "github.com", "define the host for path based queries.")
15+
app.DefineBoolFlag("force", false, "overwrite existing directory.")
16+
app.DefineBoolFlag("ssh", false, "use ssh for the connection.")
17+
app.DefineBoolFlag("bitbucket", false, "lookup with bitbucket.")
18+
app.DefineBoolFlag("github", false, "lookup with github.")
1519
app.AliasFlag('f', "force")
20+
app.AliasFlag('h', "host")
21+
app.AliasFlag('S', "ssh")
1622
app.DefineParams("url")
1723
}
1824

@@ -26,7 +32,15 @@ func get(c cli.Command) {
2632
}
2733

2834
// Vars
29-
repoURL := parseURL(c.Param("url").String())
35+
var host string
36+
if (c.Flag("bitbucket").Get() == true) {
37+
host = "bitbucket.org"
38+
} else if (c.Flag("github").Get() == true){
39+
host = "github.com"
40+
} else {
41+
host = c.Flag("host").String()
42+
}
43+
repoURL := parseURL(c.Param("url").String(), host, c.Flag("ssh").Get() == true)
3044
clonePath := parsePath(repoURL)
3145
cloneOpts := cloneOptionsForURL(repoURL)
3246

@@ -36,7 +50,7 @@ func get(c cli.Command) {
3650
}
3751

3852
// Clone
39-
fmt.Printf("Cloning into '%s'...\n", clonePath)
53+
fmt.Printf("Cloning '%s' into '%s'...\n", repoURL, clonePath)
4054
_, err := git.Clone(repoURL.String(), clonePath, cloneOpts)
4155
exitIfErr(err)
4256
fmt.Println("\nDone!")

parsers.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,25 @@ import (
77
"strings"
88
)
99

10-
func parseURL(str string) *url.URL {
10+
func parseURL(str string, defaultHost string, ssh bool) *url.URL {
1111
u, err := url.Parse(str)
1212
exitIfErrWithMsg(err, "invalid url")
1313
if u.Scheme == "" {
14-
u = parseURL("ssh://" + str)
14+
u.Scheme = "https"
1515
parts := strings.Split(u.Host, ":")
16-
u.Host = parts[0]
17-
u.Path = path.Join(parts[1], u.Path)
16+
if len(parts) > 1 {
17+
u.Host= parts[0]
18+
u.Path = path.Join(parts[1], u.Path)
19+
}
20+
}
21+
if u.Host == "" {
22+
u.Host = defaultHost
23+
}
24+
if ssh {
25+
if (u.User == nil) {
26+
u.User = url.User("git")
27+
}
28+
u.Scheme = "ssh"
1829
}
1930
return u
2031
}

0 commit comments

Comments
 (0)