Skip to content

Commit

Permalink
Merge pull request #2 from jwaldrip/advanced-repo-lookup
Browse files Browse the repository at this point in the history
add lookup feature
  • Loading branch information
jwaldrip authored Aug 15, 2016
2 parents 914606c + 63b405f commit a786380
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ $ echo "export GITPATH=$HOME/dev" > .zshrc
$ git get [repo-url]
```

### Auto host
By default, if no host is specified, git-get will lookup repos by the provided
path on github. You can use the following flags to override this:

* `--bitbucket`
* `--github`
* `--host {hostname}`
* `-h {hostname}`

### Force SSH
By default git-get will either use the scheme passed, or default to https:, to
use SSH use the `--ssh` or `-S` flag.

## Example

```
Expand Down
20 changes: 17 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ import (
var app = cli.New(version, "clone a repo into a common path", get)

func init() {
app.DefineBoolFlag("force", false, "overwrite existing directory")
app.DefineStringFlag("host", "github.com", "define the host for path based queries.")
app.DefineBoolFlag("force", false, "overwrite existing directory.")
app.DefineBoolFlag("ssh", false, "use ssh for the connection.")
app.DefineBoolFlag("bitbucket", false, "lookup with bitbucket.")
app.DefineBoolFlag("github", false, "lookup with github.")
app.AliasFlag('f', "force")
app.AliasFlag('h', "host")
app.AliasFlag('S', "ssh")
app.DefineParams("url")
}

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

// Vars
repoURL := parseURL(c.Param("url").String())
var host string
if (c.Flag("bitbucket").Get() == true) {
host = "bitbucket.org"
} else if (c.Flag("github").Get() == true){
host = "github.com"
} else {
host = c.Flag("host").String()
}
repoURL := parseURL(c.Param("url").String(), host, c.Flag("ssh").Get() == true)
clonePath := parsePath(repoURL)
cloneOpts := cloneOptionsForURL(repoURL)

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

// Clone
fmt.Printf("Cloning into '%s'...\n", clonePath)
fmt.Printf("Cloning '%s' into '%s'...\n", repoURL, clonePath)
_, err := git.Clone(repoURL.String(), clonePath, cloneOpts)
exitIfErr(err)
fmt.Println("\nDone!")
Expand Down
19 changes: 15 additions & 4 deletions parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,25 @@ import (
"strings"
)

func parseURL(str string) *url.URL {
func parseURL(str string, defaultHost string, ssh bool) *url.URL {
u, err := url.Parse(str)
exitIfErrWithMsg(err, "invalid url")
if u.Scheme == "" {
u = parseURL("ssh://" + str)
u.Scheme = "https"
parts := strings.Split(u.Host, ":")
u.Host = parts[0]
u.Path = path.Join(parts[1], u.Path)
if len(parts) > 1 {
u.Host= parts[0]
u.Path = path.Join(parts[1], u.Path)
}
}
if u.Host == "" {
u.Host = defaultHost
}
if ssh {
if (u.User == nil) {
u.User = url.User("git")
}
u.Scheme = "ssh"
}
return u
}
Expand Down

0 comments on commit a786380

Please sign in to comment.