diff --git a/cmd/jb/main.go b/cmd/jb/main.go index 221a56e..ba12762 100644 --- a/cmd/jb/main.go +++ b/cmd/jb/main.go @@ -20,11 +20,11 @@ import ( "path" "path/filepath" "regexp" - "strings" + + "github.com/pkg/errors" + kingpin "gopkg.in/alecthomas/kingpin.v2" "github.com/jsonnet-bundler/jsonnet-bundler/spec" - "github.com/pkg/errors" - "gopkg.in/alecthomas/kingpin.v2" ) const ( @@ -34,10 +34,10 @@ const ( ) var ( - gitSSHRegex = regexp.MustCompile("git\\+ssh://git@([^:]+):([^/]+)/([^/]+).git") - gitSSHWithVersionRegex = regexp.MustCompile("git\\+ssh://git@([^:]+):([^/]+)/([^/]+).git@(.*)") - gitSSHWithPathRegex = regexp.MustCompile("git\\+ssh://git@([^:]+):([^/]+)/([^/]+).git/(.*)") - gitSSHWithPathAndVersionRegex = regexp.MustCompile("git\\+ssh://git@([^:]+):([^/]+)/([^/]+).git/(.*)@(.*)") + gitSSHRegex = regexp.MustCompile(`git\+ssh://git@([^:]+):([^/]+)/([^/]+).git`) + gitSSHWithVersionRegex = regexp.MustCompile(`git\+ssh://git@([^:]+):([^/]+)/([^/]+).git@(.*)`) + gitSSHWithPathRegex = regexp.MustCompile(`git\+ssh://git@([^:]+):([^/]+)/([^/]+).git/(.*)`) + gitSSHWithPathAndVersionRegex = regexp.MustCompile(`git\+ssh://git@([^:]+):([^/]+)/([^/]+).git/(.*)@(.*)`) githubSlugRegex = regexp.MustCompile("github.com/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)") githubSlugWithVersionRegex = regexp.MustCompile("github.com/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)@(.*)") @@ -94,52 +94,49 @@ func Main() int { } func parseDependency(dir, uri string) *spec.Dependency { - if d := parseGitSSHDependency(uri); d != nil { - return d - } - - if d := parseGithubDependency(uri); d != nil { - return d - } - - if d := parseLocalDependency(dir, uri); d != nil { - return d - } - - return nil -} - -func parseGitSSHDependency(p string) *spec.Dependency { - if !gitSSHRegex.MatchString(p) { + if uri == "" { return nil } + if githubSlugRegex.MatchString(uri) { + return parseGithubDependency(uri) + } + + if gitSSHRegex.MatchString(uri) { + return parseGitSSHDependency(uri) + } + + return parseLocalDependency(dir, uri) +} + +func parseGitSSHDependency(p string) *spec.Dependency { subdir := "" host := "" org := "" repo := "" version := "master" - if gitSSHWithPathAndVersionRegex.MatchString(p) { + switch { + case gitSSHWithPathAndVersionRegex.MatchString(p): matches := gitSSHWithPathAndVersionRegex.FindStringSubmatch(p) host = matches[1] org = matches[2] repo = matches[3] subdir = matches[4] version = matches[5] - } else if gitSSHWithPathRegex.MatchString(p) { + case gitSSHWithPathRegex.MatchString(p): matches := gitSSHWithPathRegex.FindStringSubmatch(p) host = matches[1] org = matches[2] repo = matches[3] subdir = matches[4] - } else if gitSSHWithVersionRegex.MatchString(p) { + case gitSSHWithVersionRegex.MatchString(p): matches := gitSSHWithVersionRegex.FindStringSubmatch(p) host = matches[1] org = matches[2] repo = matches[3] version = matches[4] - } else { + default: matches := gitSSHRegex.FindStringSubmatch(p) host = matches[1] org = matches[2] @@ -212,16 +209,6 @@ func parseGithubDependency(p string) *spec.Dependency { } func parseLocalDependency(dir, p string) *spec.Dependency { - if p == "" { - return nil - } - if strings.HasPrefix(p, "github.com") { - return nil - } - if strings.HasPrefix(p, "git+ssh") { - return nil - } - clean := filepath.Clean(p) abs := filepath.Join(dir, clean)