mirror of
https://github.com/TECHNOFAB11/jsonnet-bundler.git
synced 2025-12-12 16:10:04 +01:00
commit
061fe0fb19
1 changed files with 141 additions and 63 deletions
116
cmd/jb/main.go
116
cmd/jb/main.go
|
|
@ -45,6 +45,11 @@ var (
|
||||||
initActionName,
|
initActionName,
|
||||||
installActionName,
|
installActionName,
|
||||||
}
|
}
|
||||||
|
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]+)")
|
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]+)@(.*)")
|
||||||
githubSlugWithPathRegex = regexp.MustCompile("github.com/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)/(.*)")
|
githubSlugWithPathRegex = regexp.MustCompile("github.com/([-_a-zA-Z0-9]+)/([-_a-zA-Z0-9]+)/(.*)")
|
||||||
|
|
@ -100,29 +105,78 @@ func initCommand() int {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func installCommand(jsonnetHome string, urls ...*url.URL) int {
|
func parseDepedency(urlString string) *spec.Dependency {
|
||||||
m, err := pkg.LoadJsonnetfile(pkg.JsonnetFile)
|
if spec := parseGitSSHDependency(urlString); spec != nil {
|
||||||
if err != nil {
|
return spec
|
||||||
kingpin.Fatalf("failed to load jsonnetfile: %v", err)
|
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(urls) > 0 {
|
if spec := parseGithubDependency(urlString); spec != nil {
|
||||||
for _, url := range urls {
|
return spec
|
||||||
// install package specified in command
|
}
|
||||||
// $ jsonnetpkg install ksonnet git@github.com:ksonnet/ksonnet-lib
|
|
||||||
// $ jsonnetpkg install grafonnet git@github.com:grafana/grafonnet-lib grafonnet
|
return nil
|
||||||
// $ jsonnetpkg install github.com/grafana/grafonnet-lib/grafonnet
|
}
|
||||||
//
|
|
||||||
// github.com/(slug)/(dir)
|
func parseGitSSHDependency(urlString string) *spec.Dependency {
|
||||||
|
if !gitSSHRegex.MatchString(urlString) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
subdir := ""
|
||||||
|
host := ""
|
||||||
|
org := ""
|
||||||
|
repo := ""
|
||||||
|
version := "master"
|
||||||
|
|
||||||
|
if gitSSHWithPathAndVersionRegex.MatchString(urlString) {
|
||||||
|
matches := gitSSHWithPathAndVersionRegex.FindStringSubmatch(urlString)
|
||||||
|
host = matches[1]
|
||||||
|
org = matches[2]
|
||||||
|
repo = matches[3]
|
||||||
|
subdir = matches[4]
|
||||||
|
version = matches[5]
|
||||||
|
} else if gitSSHWithPathRegex.MatchString(urlString) {
|
||||||
|
matches := gitSSHWithPathRegex.FindStringSubmatch(urlString)
|
||||||
|
host = matches[1]
|
||||||
|
org = matches[2]
|
||||||
|
repo = matches[3]
|
||||||
|
subdir = matches[4]
|
||||||
|
} else if gitSSHWithVersionRegex.MatchString(urlString) {
|
||||||
|
matches := gitSSHWithVersionRegex.FindStringSubmatch(urlString)
|
||||||
|
host = matches[1]
|
||||||
|
org = matches[2]
|
||||||
|
repo = matches[3]
|
||||||
|
version = matches[4]
|
||||||
|
} else {
|
||||||
|
matches := gitSSHRegex.FindStringSubmatch(urlString)
|
||||||
|
host = matches[1]
|
||||||
|
org = matches[2]
|
||||||
|
repo = matches[3]
|
||||||
|
}
|
||||||
|
|
||||||
|
return &spec.Dependency{
|
||||||
|
Name: repo,
|
||||||
|
Source: spec.Source{
|
||||||
|
GitSource: &spec.GitSource{
|
||||||
|
Remote: fmt.Sprintf("git@%s:%s/%s", host, org, repo),
|
||||||
|
Subdir: subdir,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Version: version,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseGithubDependency(urlString string) *spec.Dependency {
|
||||||
|
if !githubSlugRegex.MatchString(urlString) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
urlString := url.String()
|
|
||||||
if githubSlugRegex.MatchString(urlString) {
|
|
||||||
name := ""
|
name := ""
|
||||||
user := ""
|
user := ""
|
||||||
repo := ""
|
repo := ""
|
||||||
subdir := ""
|
subdir := ""
|
||||||
version := "master"
|
version := "master"
|
||||||
|
|
||||||
if githubSlugWithPathRegex.MatchString(urlString) {
|
if githubSlugWithPathRegex.MatchString(urlString) {
|
||||||
if githubSlugWithPathAndVersionRegex.MatchString(urlString) {
|
if githubSlugWithPathAndVersionRegex.MatchString(urlString) {
|
||||||
matches := githubSlugWithPathAndVersionRegex.FindStringSubmatch(urlString)
|
matches := githubSlugWithPathAndVersionRegex.FindStringSubmatch(urlString)
|
||||||
|
|
@ -153,7 +207,7 @@ func installCommand(jsonnetHome string, urls ...*url.URL) int {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
newDep := spec.Dependency{
|
return &spec.Dependency{
|
||||||
Name: name,
|
Name: name,
|
||||||
Source: spec.Source{
|
Source: spec.Source{
|
||||||
GitSource: &spec.GitSource{
|
GitSource: &spec.GitSource{
|
||||||
|
|
@ -163,12 +217,37 @@ func installCommand(jsonnetHome string, urls ...*url.URL) int {
|
||||||
},
|
},
|
||||||
Version: version,
|
Version: version,
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func installCommand(jsonnetHome string, urls ...*url.URL) int {
|
||||||
|
m, err := pkg.LoadJsonnetfile(pkg.JsonnetFile)
|
||||||
|
if err != nil {
|
||||||
|
kingpin.Fatalf("failed to load jsonnetfile: %v", err)
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(urls) > 0 {
|
||||||
|
for _, url := range urls {
|
||||||
|
// install package specified in command
|
||||||
|
// $ jsonnetpkg install ksonnet git@github.com:ksonnet/ksonnet-lib
|
||||||
|
// $ jsonnetpkg install grafonnet git@github.com:grafana/grafonnet-lib grafonnet
|
||||||
|
// $ jsonnetpkg install github.com/grafana/grafonnet-lib/grafonnet
|
||||||
|
//
|
||||||
|
// github.com/(slug)/(dir)
|
||||||
|
|
||||||
|
urlString := url.String()
|
||||||
|
newDep := parseDepedency(urlString)
|
||||||
|
if newDep == nil {
|
||||||
|
kingpin.Errorf("ignoring unrecognized url: %s", url)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
oldDeps := m.Dependencies
|
oldDeps := m.Dependencies
|
||||||
newDeps := []spec.Dependency{}
|
newDeps := []spec.Dependency{}
|
||||||
oldDepReplaced := false
|
oldDepReplaced := false
|
||||||
for _, d := range oldDeps {
|
for _, d := range oldDeps {
|
||||||
if d.Name == newDep.Name {
|
if d.Name == newDep.Name {
|
||||||
newDeps = append(newDeps, newDep)
|
newDeps = append(newDeps, *newDep)
|
||||||
oldDepReplaced = true
|
oldDepReplaced = true
|
||||||
} else {
|
} else {
|
||||||
newDeps = append(newDeps, d)
|
newDeps = append(newDeps, d)
|
||||||
|
|
@ -176,13 +255,12 @@ func installCommand(jsonnetHome string, urls ...*url.URL) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
if !oldDepReplaced {
|
if !oldDepReplaced {
|
||||||
newDeps = append(newDeps, newDep)
|
newDeps = append(newDeps, *newDep)
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Dependencies = newDeps
|
m.Dependencies = newDeps
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
srcPath := filepath.Join(jsonnetHome)
|
srcPath := filepath.Join(jsonnetHome)
|
||||||
err = os.MkdirAll(srcPath, os.ModePerm)
|
err = os.MkdirAll(srcPath, os.ModePerm)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue