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"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings"
"github.com/pkg/errors"
kingpin "gopkg.in/alecthomas/kingpin.v2"
"github.com/jsonnet-bundler/jsonnet-bundler/spec" "github.com/jsonnet-bundler/jsonnet-bundler/spec"
"github.com/pkg/errors"
"gopkg.in/alecthomas/kingpin.v2"
) )
const ( const (
@ -34,10 +34,10 @@ const (
) )
var ( var (
gitSSHRegex = regexp.MustCompile("git\\+ssh://git@([^:]+):([^/]+)/([^/]+).git") gitSSHRegex = regexp.MustCompile(`git\+ssh://git@([^:]+):([^/]+)/([^/]+).git`)
gitSSHWithVersionRegex = regexp.MustCompile("git\\+ssh://git@([^:]+):([^/]+)/([^/]+).git@(.*)") gitSSHWithVersionRegex = regexp.MustCompile(`git\+ssh://git@([^:]+):([^/]+)/([^/]+).git@(.*)`)
gitSSHWithPathRegex = regexp.MustCompile("git\\+ssh://git@([^:]+):([^/]+)/([^/]+).git/(.*)") gitSSHWithPathRegex = regexp.MustCompile(`git\+ssh://git@([^:]+):([^/]+)/([^/]+).git/(.*)`)
gitSSHWithPathAndVersionRegex = 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]+)") 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]+)@(.*)") 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 { func parseDependency(dir, uri string) *spec.Dependency {
if d := parseGitSSHDependency(uri); d != nil { if uri == "" {
return d
}
if d := parseGithubDependency(uri); d != nil {
return d
}
if d := parseLocalDependency(dir, uri); d != nil {
return d
}
return nil 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 { func parseGitSSHDependency(p string) *spec.Dependency {
if !gitSSHRegex.MatchString(p) {
return nil
}
subdir := "" subdir := ""
host := "" host := ""
org := "" org := ""
repo := "" repo := ""
version := "master" version := "master"
if gitSSHWithPathAndVersionRegex.MatchString(p) { switch {
case gitSSHWithPathAndVersionRegex.MatchString(p):
matches := gitSSHWithPathAndVersionRegex.FindStringSubmatch(p) matches := gitSSHWithPathAndVersionRegex.FindStringSubmatch(p)
host = matches[1] host = matches[1]
org = matches[2] org = matches[2]
repo = matches[3] repo = matches[3]
subdir = matches[4] subdir = matches[4]
version = matches[5] version = matches[5]
} else if gitSSHWithPathRegex.MatchString(p) { case gitSSHWithPathRegex.MatchString(p):
matches := gitSSHWithPathRegex.FindStringSubmatch(p) matches := gitSSHWithPathRegex.FindStringSubmatch(p)
host = matches[1] host = matches[1]
org = matches[2] org = matches[2]
repo = matches[3] repo = matches[3]
subdir = matches[4] subdir = matches[4]
} else if gitSSHWithVersionRegex.MatchString(p) { case gitSSHWithVersionRegex.MatchString(p):
matches := gitSSHWithVersionRegex.FindStringSubmatch(p) matches := gitSSHWithVersionRegex.FindStringSubmatch(p)
host = matches[1] host = matches[1]
org = matches[2] org = matches[2]
repo = matches[3] repo = matches[3]
version = matches[4] version = matches[4]
} else { default:
matches := gitSSHRegex.FindStringSubmatch(p) matches := gitSSHRegex.FindStringSubmatch(p)
host = matches[1] host = matches[1]
org = matches[2] org = matches[2]
@ -212,16 +209,6 @@ func parseGithubDependency(p string) *spec.Dependency {
} }
func parseLocalDependency(dir, 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) clean := filepath.Clean(p)
abs := filepath.Join(dir, clean) abs := filepath.Join(dir, clean)