refactor: dependency parsing

Refactors the dependency parsing function chain to evaluate the type of the
dependency right in `parseDependency` to make it clearer what is going on while
reading the code. Before, functions were returning if it was a different type,
which was not that clear from `parseDependency`.
This commit is contained in:
sh0rez 2019-10-16 11:23:31 +02:00
parent 766a0c7fd8
commit a718f48cd8
No known key found for this signature in database
GPG key ID: 87C71DF9F8181FF1

View file

@ -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)