mirror of
https://github.com/TECHNOFAB11/jsonnet-bundler.git
synced 2025-12-11 23:50:05 +01:00
Merge remote-tracking branch 'origin/master' into git-clone-optim
# Conflicts: # pkg/git.go
This commit is contained in:
commit
926830713e
13 changed files with 363 additions and 146 deletions
2
Makefile
2
Makefile
|
|
@ -28,7 +28,7 @@ install: build
|
|||
|
||||
test:
|
||||
@echo ">> running all unit tests"
|
||||
@go test $(PKGS)
|
||||
go test -v $(PKGS)
|
||||
|
||||
test-integration:
|
||||
@echo ">> running all integration tests"
|
||||
|
|
|
|||
|
|
@ -95,7 +95,7 @@ Commands:
|
|||
init
|
||||
Initialize a new empty jsonnetfile
|
||||
|
||||
install [<packages>...]
|
||||
install [<uris>...]
|
||||
Install all dependencies or install specific ones
|
||||
|
||||
update
|
||||
|
|
@ -104,3 +104,6 @@ Commands:
|
|||
|
||||
```
|
||||
|
||||
## Design
|
||||
|
||||
This is an implemention of the design specified in this document: https://docs.google.com/document/d/1czRScSvvOiAJaIjwf3CogOULgQxhY9MkiBKOQI1yR14/edit#heading=h.upn4d5pcxy4c
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@ import (
|
|||
"context"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
|
|
@ -28,7 +27,7 @@ import (
|
|||
"gopkg.in/alecthomas/kingpin.v2"
|
||||
)
|
||||
|
||||
func installCommand(dir, jsonnetHome string, urls ...*url.URL) int {
|
||||
func installCommand(dir, jsonnetHome string, uris ...string) int {
|
||||
if dir == "" {
|
||||
dir = "."
|
||||
}
|
||||
|
|
@ -45,19 +44,11 @@ func installCommand(dir, jsonnetHome string, urls ...*url.URL) int {
|
|||
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 len(uris) > 0 {
|
||||
for _, uri := range uris {
|
||||
newDep := parseDependency(dir, uri)
|
||||
if newDep == nil {
|
||||
kingpin.Errorf("ignoring unrecognized url: %s", url)
|
||||
kingpin.Errorf("ignoring unrecognized uri: %s", uri)
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@ package main
|
|||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
|
@ -30,7 +29,7 @@ import (
|
|||
func TestInstallCommand(t *testing.T) {
|
||||
testcases := []struct {
|
||||
Name string
|
||||
URLs []*url.URL
|
||||
URIs []string
|
||||
ExpectedCode int
|
||||
ExpectedJsonnetFile []byte
|
||||
ExpectedJsonnetLockFile []byte
|
||||
|
|
@ -42,25 +41,27 @@ func TestInstallCommand(t *testing.T) {
|
|||
ExpectedJsonnetLockFile: []byte(`{"dependencies":null}`),
|
||||
}, {
|
||||
Name: "OneURL",
|
||||
URLs: []*url.URL{
|
||||
{
|
||||
Scheme: "https",
|
||||
Host: "github.com",
|
||||
Path: "jsonnet-bundler/jsonnet-bundler@v0.1.0",
|
||||
},
|
||||
},
|
||||
URIs: []string{"github.com/jsonnet-bundler/jsonnet-bundler@v0.1.0"},
|
||||
ExpectedCode: 0,
|
||||
ExpectedJsonnetFile: []byte(`{"dependencies": [{"name": "jsonnet-bundler", "source": {"git": {"remote": "https://github.com/jsonnet-bundler/jsonnet-bundler", "subdir": ""}}, "version": "v0.1.0"}]}`),
|
||||
ExpectedJsonnetLockFile: []byte(`{"dependencies": [{"name": "jsonnet-bundler", "source": {"git": {"remote": "https://github.com/jsonnet-bundler/jsonnet-bundler", "subdir": ""}}, "version": "080f157c7fb85ad0281ea78f6c641eaa570a582f"}]}`),
|
||||
}, {
|
||||
Name: "Relative",
|
||||
URIs: []string{"test/jsonnet/foobar"},
|
||||
ExpectedCode: 0,
|
||||
ExpectedJsonnetFile: []byte(`{"dependencies": [{"name": "foobar", "source": {"local": {"directory": "test/jsonnet/foobar"}}, "version": ""}]}`),
|
||||
ExpectedJsonnetLockFile: []byte(`{"dependencies": [{"name": "foobar", "source": {"local": {"directory": "test/jsonnet/foobar"}}, "version": ""}]}`),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testcases {
|
||||
t.Run(tc.Name, func(t *testing.T) {
|
||||
_ = t.Run(tc.Name, func(t *testing.T) {
|
||||
tempDir, err := ioutil.TempDir("", "jb-install")
|
||||
assert.NoError(t, err)
|
||||
err = os.MkdirAll(filepath.Join(tempDir, "test/jsonnet/foobar"), os.ModePerm)
|
||||
assert.NoError(t, err)
|
||||
defer os.Remove(tempDir)
|
||||
defer os.RemoveAll("vendor") // delete test vendor folder
|
||||
defer os.RemoveAll("vendor") // cloning jsonnet-bundler will create this folder
|
||||
|
||||
jsonnetFile := filepath.Join(tempDir, jsonnetfile.File)
|
||||
jsonnetLockFile := filepath.Join(tempDir, jsonnetfile.LockFile)
|
||||
|
|
@ -70,7 +71,7 @@ func TestInstallCommand(t *testing.T) {
|
|||
|
||||
jsonnetFileContent(t, jsonnetFile, []byte(`{}`))
|
||||
|
||||
code = installCommand(tempDir, "vendor", tc.URLs...)
|
||||
code = installCommand(tempDir, "vendor", tc.URIs...)
|
||||
assert.Equal(t, tc.ExpectedCode, code)
|
||||
|
||||
jsonnetFileContent(t, jsonnetFile, tc.ExpectedJsonnetFile)
|
||||
|
|
@ -80,6 +81,8 @@ func TestInstallCommand(t *testing.T) {
|
|||
}
|
||||
|
||||
func jsonnetFileContent(t *testing.T, filename string, content []byte) {
|
||||
t.Helper()
|
||||
|
||||
bytes, err := ioutil.ReadFile(filename)
|
||||
assert.NoError(t, err)
|
||||
if eq := assert.JSONEq(t, string(content), string(bytes)); !eq {
|
||||
|
|
|
|||
121
cmd/jb/main.go
121
cmd/jb/main.go
|
|
@ -15,17 +15,13 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/pkg"
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/spec"
|
||||
"github.com/pkg/errors"
|
||||
"gopkg.in/alecthomas/kingpin.v2"
|
||||
|
|
@ -35,15 +31,9 @@ const (
|
|||
installActionName = "install"
|
||||
updateActionName = "update"
|
||||
initActionName = "init"
|
||||
basePath = ".jsonnetpkg"
|
||||
srcDirName = "src"
|
||||
)
|
||||
|
||||
var (
|
||||
availableSubcommands = []string{
|
||||
initActionName,
|
||||
installActionName,
|
||||
}
|
||||
gitSSHRegex = regexp.MustCompile("git\\+ssh://git@([^:]+):([^/]+)/([^/]+).git")
|
||||
gitSSHWithVersionRegex = regexp.MustCompile("git\\+ssh://git@([^:]+):([^/]+)/([^/]+).git@(.*)")
|
||||
gitSSHWithPathRegex = regexp.MustCompile("git\\+ssh://git@([^:]+):([^/]+)/([^/]+).git/(.*)")
|
||||
|
|
@ -73,7 +63,7 @@ func Main() int {
|
|||
initCmd := a.Command(initActionName, "Initialize a new empty jsonnetfile")
|
||||
|
||||
installCmd := a.Command(installActionName, "Install all dependencies or install specific ones")
|
||||
installCmdURLs := installCmd.Arg("packages", "URLs to package to install").URLList()
|
||||
installCmdURIs := installCmd.Arg("uris", "URIs to packages to install, URLs or file paths").Strings()
|
||||
|
||||
updateCmd := a.Command(updateActionName, "Update all dependencies.")
|
||||
|
||||
|
|
@ -93,7 +83,7 @@ func Main() int {
|
|||
case initCmd.FullCommand():
|
||||
return initCommand(workdir)
|
||||
case installCmd.FullCommand():
|
||||
return installCommand(workdir, cfg.JsonnetHome, *installCmdURLs...)
|
||||
return installCommand(workdir, cfg.JsonnetHome, *installCmdURIs...)
|
||||
case updateCmd.FullCommand():
|
||||
return updateCommand(cfg.JsonnetHome)
|
||||
default:
|
||||
|
|
@ -103,20 +93,24 @@ func Main() int {
|
|||
return 0
|
||||
}
|
||||
|
||||
func parseDepedency(urlString string) *spec.Dependency {
|
||||
if spec := parseGitSSHDependency(urlString); spec != nil {
|
||||
return spec
|
||||
func parseDependency(dir, uri string) *spec.Dependency {
|
||||
if d := parseGitSSHDependency(uri); d != nil {
|
||||
return d
|
||||
}
|
||||
|
||||
if spec := parseGithubDependency(urlString); spec != nil {
|
||||
return spec
|
||||
if d := parseGithubDependency(uri); d != nil {
|
||||
return d
|
||||
}
|
||||
|
||||
if d := parseLocalDependency(dir, uri); d != nil {
|
||||
return d
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseGitSSHDependency(urlString string) *spec.Dependency {
|
||||
if !gitSSHRegex.MatchString(urlString) {
|
||||
func parseGitSSHDependency(p string) *spec.Dependency {
|
||||
if !gitSSHRegex.MatchString(p) {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -126,27 +120,27 @@ func parseGitSSHDependency(urlString string) *spec.Dependency {
|
|||
repo := ""
|
||||
version := "master"
|
||||
|
||||
if gitSSHWithPathAndVersionRegex.MatchString(urlString) {
|
||||
matches := gitSSHWithPathAndVersionRegex.FindStringSubmatch(urlString)
|
||||
if 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(urlString) {
|
||||
matches := gitSSHWithPathRegex.FindStringSubmatch(urlString)
|
||||
} else if gitSSHWithPathRegex.MatchString(p) {
|
||||
matches := gitSSHWithPathRegex.FindStringSubmatch(p)
|
||||
host = matches[1]
|
||||
org = matches[2]
|
||||
repo = matches[3]
|
||||
subdir = matches[4]
|
||||
} else if gitSSHWithVersionRegex.MatchString(urlString) {
|
||||
matches := gitSSHWithVersionRegex.FindStringSubmatch(urlString)
|
||||
} else if gitSSHWithVersionRegex.MatchString(p) {
|
||||
matches := gitSSHWithVersionRegex.FindStringSubmatch(p)
|
||||
host = matches[1]
|
||||
org = matches[2]
|
||||
repo = matches[3]
|
||||
version = matches[4]
|
||||
} else {
|
||||
matches := gitSSHRegex.FindStringSubmatch(urlString)
|
||||
matches := gitSSHRegex.FindStringSubmatch(p)
|
||||
host = matches[1]
|
||||
org = matches[2]
|
||||
repo = matches[3]
|
||||
|
|
@ -164,8 +158,8 @@ func parseGitSSHDependency(urlString string) *spec.Dependency {
|
|||
}
|
||||
}
|
||||
|
||||
func parseGithubDependency(urlString string) *spec.Dependency {
|
||||
if !githubSlugRegex.MatchString(urlString) {
|
||||
func parseGithubDependency(p string) *spec.Dependency {
|
||||
if !githubSlugRegex.MatchString(p) {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -175,30 +169,30 @@ func parseGithubDependency(urlString string) *spec.Dependency {
|
|||
subdir := ""
|
||||
version := "master"
|
||||
|
||||
if githubSlugWithPathRegex.MatchString(urlString) {
|
||||
if githubSlugWithPathAndVersionRegex.MatchString(urlString) {
|
||||
matches := githubSlugWithPathAndVersionRegex.FindStringSubmatch(urlString)
|
||||
if githubSlugWithPathRegex.MatchString(p) {
|
||||
if githubSlugWithPathAndVersionRegex.MatchString(p) {
|
||||
matches := githubSlugWithPathAndVersionRegex.FindStringSubmatch(p)
|
||||
user = matches[1]
|
||||
repo = matches[2]
|
||||
subdir = matches[3]
|
||||
version = matches[4]
|
||||
name = path.Base(subdir)
|
||||
} else {
|
||||
matches := githubSlugWithPathRegex.FindStringSubmatch(urlString)
|
||||
matches := githubSlugWithPathRegex.FindStringSubmatch(p)
|
||||
user = matches[1]
|
||||
repo = matches[2]
|
||||
subdir = matches[3]
|
||||
name = path.Base(subdir)
|
||||
}
|
||||
} else {
|
||||
if githubSlugWithVersionRegex.MatchString(urlString) {
|
||||
matches := githubSlugWithVersionRegex.FindStringSubmatch(urlString)
|
||||
if githubSlugWithVersionRegex.MatchString(p) {
|
||||
matches := githubSlugWithVersionRegex.FindStringSubmatch(p)
|
||||
user = matches[1]
|
||||
repo = matches[2]
|
||||
name = repo
|
||||
version = matches[3]
|
||||
} else {
|
||||
matches := githubSlugRegex.FindStringSubmatch(urlString)
|
||||
matches := githubSlugRegex.FindStringSubmatch(p)
|
||||
user = matches[1]
|
||||
repo = matches[2]
|
||||
name = repo
|
||||
|
|
@ -217,41 +211,36 @@ func parseGithubDependency(urlString string) *spec.Dependency {
|
|||
}
|
||||
}
|
||||
|
||||
func updateCommand(jsonnetHome string, urls ...*url.URL) int {
|
||||
jsonnetfile := pkg.JsonnetFile
|
||||
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
|
||||
}
|
||||
|
||||
m, err := pkg.LoadJsonnetfile(jsonnetfile)
|
||||
clean := filepath.Clean(p)
|
||||
abs := filepath.Join(dir, clean)
|
||||
|
||||
info, err := os.Stat(abs)
|
||||
if err != nil {
|
||||
kingpin.Fatalf("failed to load jsonnetfile: %v", err)
|
||||
return 1
|
||||
return nil
|
||||
}
|
||||
|
||||
err = os.MkdirAll(jsonnetHome, os.ModePerm)
|
||||
if err != nil {
|
||||
kingpin.Fatalf("failed to create jsonnet home path: %v", err)
|
||||
return 3
|
||||
if !info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
// When updating, the lockfile is explicitly ignored.
|
||||
isLock := false
|
||||
lock, err := pkg.Install(context.TODO(), isLock, jsonnetfile, m, jsonnetHome)
|
||||
if err != nil {
|
||||
kingpin.Fatalf("failed to install: %v", err)
|
||||
return 3
|
||||
return &spec.Dependency{
|
||||
Name: info.Name(),
|
||||
Source: spec.Source{
|
||||
LocalSource: &spec.LocalSource{
|
||||
Directory: clean,
|
||||
},
|
||||
},
|
||||
Version: "",
|
||||
}
|
||||
|
||||
b, err := json.MarshalIndent(lock, "", " ")
|
||||
if err != nil {
|
||||
kingpin.Fatalf("failed to encode jsonnet file: %v", err)
|
||||
return 3
|
||||
}
|
||||
b = append(b, []byte("\n")...)
|
||||
|
||||
err = ioutil.WriteFile(pkg.JsonnetLockFile, b, 0644)
|
||||
if err != nil {
|
||||
kingpin.Fatalf("failed to write lock file: %v", err)
|
||||
return 3
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
|
|
|||
101
cmd/jb/main_test.go
Normal file
101
cmd/jb/main_test.go
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
// Copyright 2018 jsonnet-bundler authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/spec"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestParseDependency(t *testing.T) {
|
||||
const testFolder = "test/jsonnet/foobar"
|
||||
err := os.MkdirAll(testFolder, os.ModePerm)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll("test")
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
path string
|
||||
want *spec.Dependency
|
||||
}{
|
||||
{
|
||||
name: "Empty",
|
||||
path: "",
|
||||
want: nil,
|
||||
},
|
||||
{
|
||||
name: "Invalid",
|
||||
path: "github.com/foo",
|
||||
want: nil,
|
||||
},
|
||||
{
|
||||
name: "GitHub",
|
||||
path: "github.com/jsonnet-bundler/jsonnet-bundler",
|
||||
want: &spec.Dependency{
|
||||
Name: "jsonnet-bundler",
|
||||
Source: spec.Source{
|
||||
GitSource: &spec.GitSource{
|
||||
Remote: "https://github.com/jsonnet-bundler/jsonnet-bundler",
|
||||
Subdir: "",
|
||||
},
|
||||
},
|
||||
Version: "master",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "SSH",
|
||||
path: "git+ssh://git@github.com:jsonnet-bundler/jsonnet-bundler.git",
|
||||
want: &spec.Dependency{
|
||||
Name: "jsonnet-bundler",
|
||||
Source: spec.Source{
|
||||
GitSource: &spec.GitSource{
|
||||
Remote: "git@github.com:jsonnet-bundler/jsonnet-bundler",
|
||||
Subdir: "",
|
||||
},
|
||||
},
|
||||
Version: "master",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "local",
|
||||
path: testFolder,
|
||||
want: &spec.Dependency{
|
||||
Name: "foobar",
|
||||
Source: spec.Source{
|
||||
LocalSource: &spec.LocalSource{
|
||||
Directory: "test/jsonnet/foobar",
|
||||
},
|
||||
},
|
||||
Version: "",
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
_ = t.Run(tt.name, func(t *testing.T) {
|
||||
dependency := parseDependency("", tt.path)
|
||||
|
||||
if tt.path == "" {
|
||||
assert.Nil(t, dependency)
|
||||
} else {
|
||||
assert.Equal(t, tt.want, dependency)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
64
cmd/jb/update.go
Normal file
64
cmd/jb/update.go
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
// Copyright 2018 jsonnet-bundler authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/pkg"
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/pkg/jsonnetfile"
|
||||
"gopkg.in/alecthomas/kingpin.v2"
|
||||
)
|
||||
|
||||
func updateCommand(jsonnetHome string, urls ...*url.URL) int {
|
||||
m, err := pkg.LoadJsonnetfile(jsonnetfile.File)
|
||||
if err != nil {
|
||||
kingpin.Fatalf("failed to load jsonnetfile: %v", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
err = os.MkdirAll(jsonnetHome, os.ModePerm)
|
||||
if err != nil {
|
||||
kingpin.Fatalf("failed to create jsonnet home path: %v", err)
|
||||
return 3
|
||||
}
|
||||
|
||||
// When updating, the lockfile is explicitly ignored.
|
||||
isLock := false
|
||||
lock, err := pkg.Install(context.TODO(), isLock, jsonnetfile.File, m, jsonnetHome)
|
||||
if err != nil {
|
||||
kingpin.Fatalf("failed to install: %v", err)
|
||||
return 3
|
||||
}
|
||||
|
||||
b, err := json.MarshalIndent(lock, "", " ")
|
||||
if err != nil {
|
||||
kingpin.Fatalf("failed to encode jsonnet file: %v", err)
|
||||
return 3
|
||||
}
|
||||
b = append(b, []byte("\n")...)
|
||||
|
||||
err = ioutil.WriteFile(jsonnetfile.LockFile, b, 0644)
|
||||
if err != nil {
|
||||
kingpin.Fatalf("failed to write lock file: %v", err)
|
||||
return 3
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
48
pkg/git.go
48
pkg/git.go
|
|
@ -33,6 +33,7 @@ import (
|
|||
|
||||
"github.com/fatih/color"
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/spec"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type GitPackage struct {
|
||||
|
|
@ -146,13 +147,21 @@ func gzipUntar(dst string, r io.Reader, subDir string) error {
|
|||
}
|
||||
}
|
||||
|
||||
func (p *GitPackage) Install(ctx context.Context, dir, version string) (lockVersion string, err error) {
|
||||
func (p *GitPackage) Install(ctx context.Context, name, dir, version string) (string, error) {
|
||||
destPath := path.Join(dir, name)
|
||||
|
||||
tmpDir, err := ioutil.TempDir(filepath.Join(dir, ".tmp"), fmt.Sprintf("jsonnetpkg-%s-%s", name, version))
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "failed to create tmp dir")
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
// Optimization for GitHub sources: download a tarball archive of the requested
|
||||
// version instead of cloning the entire repository. Resolves the version to a
|
||||
// commit SHA using the GitHub API.
|
||||
if strings.HasPrefix(p.Source.Remote, "https://github.com/") {
|
||||
archiveUrl := fmt.Sprintf("%s/archive/%s.tar.gz", p.Source.Remote, version)
|
||||
archiveFilepath := fmt.Sprintf("%s.tar.gz", dir)
|
||||
archiveFilepath := fmt.Sprintf("%s.tar.gz", tmpDir)
|
||||
|
||||
defer os.Remove(archiveFilepath)
|
||||
commitSha, err := downloadGitHubArchive(archiveFilepath, archiveUrl)
|
||||
|
|
@ -160,7 +169,7 @@ func (p *GitPackage) Install(ctx context.Context, dir, version string) (lockVers
|
|||
return "", err
|
||||
}
|
||||
r, err := os.Open(archiveFilepath)
|
||||
err = gzipUntar(dir, r, p.Source.Subdir)
|
||||
err = gzipUntar(tmpDir, r, p.Source.Subdir)
|
||||
return commitSha, nil
|
||||
}
|
||||
|
||||
|
|
@ -168,7 +177,7 @@ func (p *GitPackage) Install(ctx context.Context, dir, version string) (lockVers
|
|||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Dir = dir
|
||||
cmd.Dir = tmpDir
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
|
@ -178,7 +187,7 @@ func (p *GitPackage) Install(ctx context.Context, dir, version string) (lockVers
|
|||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Dir = dir
|
||||
cmd.Dir = tmpDir
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
|
@ -189,7 +198,7 @@ func (p *GitPackage) Install(ctx context.Context, dir, version string) (lockVers
|
|||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Dir = dir
|
||||
cmd.Dir = tmpDir
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
// Fall back to normal fetch (all revisions)
|
||||
|
|
@ -197,7 +206,7 @@ func (p *GitPackage) Install(ctx context.Context, dir, version string) (lockVers
|
|||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Dir = dir
|
||||
cmd.Dir = tmpDir
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
|
@ -211,20 +220,20 @@ func (p *GitPackage) Install(ctx context.Context, dir, version string) (lockVers
|
|||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Dir = dir
|
||||
cmd.Dir = tmpDir
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
glob := []byte(p.Source.Subdir + "/*\n")
|
||||
ioutil.WriteFile(dir+"/.git/info/sparse-checkout", glob, 0644)
|
||||
ioutil.WriteFile(tmpDir+"/.git/info/sparse-checkout", glob, 0644)
|
||||
}
|
||||
|
||||
cmd = exec.CommandContext(ctx, "git", "-c", "advice.detachedHead=false", "checkout", version)
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
cmd.Dir = dir
|
||||
cmd.Dir = tmpDir
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
|
@ -233,7 +242,7 @@ func (p *GitPackage) Install(ctx context.Context, dir, version string) (lockVers
|
|||
b := bytes.NewBuffer(nil)
|
||||
cmd = exec.CommandContext(ctx, "git", "rev-parse", "HEAD")
|
||||
cmd.Stdout = b
|
||||
cmd.Dir = dir
|
||||
cmd.Dir = tmpDir
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
|
@ -241,10 +250,25 @@ func (p *GitPackage) Install(ctx context.Context, dir, version string) (lockVers
|
|||
|
||||
commitHash := strings.TrimSpace(b.String())
|
||||
|
||||
err = os.RemoveAll(path.Join(dir, ".git"))
|
||||
err = os.RemoveAll(path.Join(tmpDir, ".git"))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
err = os.MkdirAll(path.Dir(destPath), os.ModePerm)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "failed to create parent path")
|
||||
}
|
||||
|
||||
err = os.RemoveAll(destPath)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "failed to clean previous destination path")
|
||||
}
|
||||
|
||||
err = os.Rename(path.Join(tmpDir, p.Source.Subdir), destPath)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "failed to move package")
|
||||
}
|
||||
|
||||
return commitHash, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,5 +19,5 @@ import (
|
|||
)
|
||||
|
||||
type Interface interface {
|
||||
Install(ctx context.Context, dir, version string) (lockVersion string, err error)
|
||||
Install(ctx context.Context, name, dir, version string) (lockVersion string, err error)
|
||||
}
|
||||
|
|
|
|||
56
pkg/local.go
Normal file
56
pkg/local.go
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
// Copyright 2018 jsonnet-bundler authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/spec"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type LocalPackage struct {
|
||||
Source *spec.LocalSource
|
||||
}
|
||||
|
||||
func NewLocalPackage(source *spec.LocalSource) Interface {
|
||||
return &LocalPackage{
|
||||
Source: source,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *LocalPackage) Install(ctx context.Context, name, dir, version string) (lockVersion string, err error) {
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to get current working directory: %v", err)
|
||||
}
|
||||
|
||||
destPath := filepath.Join(dir, name)
|
||||
|
||||
err = os.RemoveAll(destPath)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "failed to clean previous destination path")
|
||||
}
|
||||
|
||||
err = os.Symlink(filepath.Join(wd, p.Source.Directory), filepath.Join(wd, destPath))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create symlink for local dependency: %v", err)
|
||||
}
|
||||
|
||||
return "", nil
|
||||
}
|
||||
|
|
@ -18,19 +18,17 @@ import (
|
|||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/pkg/jsonnetfile"
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/spec"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var (
|
||||
JsonnetFile = "jsonnetfile.json"
|
||||
JsonnetLockFile = "jsonnetfile.lock.json"
|
||||
VersionMismatch = errors.New("multiple colliding versions specified")
|
||||
)
|
||||
|
||||
|
|
@ -43,20 +41,16 @@ func Install(ctx context.Context, isLock bool, dependencySourceIdentifier string
|
|||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to create general tmp dir")
|
||||
}
|
||||
tmpDir, err := ioutil.TempDir(tmp, fmt.Sprintf("jsonnetpkg-%s-%s", dep.Name, dep.Version))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to create tmp dir")
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
subdir := ""
|
||||
var p Interface
|
||||
if dep.Source.GitSource != nil {
|
||||
p = NewGitPackage(dep.Source.GitSource)
|
||||
subdir = dep.Source.GitSource.Subdir
|
||||
}
|
||||
if dep.Source.LocalSource != nil {
|
||||
p = NewLocalPackage(dep.Source.LocalSource)
|
||||
}
|
||||
|
||||
lockVersion, err := p.Install(ctx, tmpDir, dep.Version)
|
||||
lockVersion, err := p.Install(ctx, dep.Name, dir, dep.Version)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to install package")
|
||||
}
|
||||
|
|
@ -65,20 +59,6 @@ func Install(ctx context.Context, isLock bool, dependencySourceIdentifier string
|
|||
|
||||
destPath := path.Join(dir, dep.Name)
|
||||
|
||||
err = os.MkdirAll(path.Dir(destPath), os.ModePerm)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to create parent path")
|
||||
}
|
||||
|
||||
err = os.RemoveAll(destPath)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to clean previous destination path")
|
||||
}
|
||||
err = os.Rename(path.Join(tmpDir, subdir), destPath)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to move package")
|
||||
}
|
||||
|
||||
lockfile.Dependencies, err = insertDependency(lockfile.Dependencies, spec.Dependency{
|
||||
Name: dep.Name,
|
||||
Source: dep.Source,
|
||||
|
|
@ -101,7 +81,7 @@ func Install(ctx context.Context, isLock bool, dependencySourceIdentifier string
|
|||
return nil, err
|
||||
}
|
||||
depsDeps, err := LoadJsonnetfile(filepath)
|
||||
// It is ok for depedencies not to have a JsonnetFile, it just means
|
||||
// It is ok for dependencies not to have a JsonnetFile, it just means
|
||||
// they do not have transitive dependencies of their own.
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return nil, err
|
||||
|
|
@ -161,18 +141,18 @@ func FileExists(path string) (bool, error) {
|
|||
}
|
||||
|
||||
func ChooseJsonnetFile(dir string) (string, bool, error) {
|
||||
lockfile := path.Join(dir, JsonnetLockFile)
|
||||
jsonnetfile := path.Join(dir, JsonnetFile)
|
||||
filename := lockfile
|
||||
lockfilePath := path.Join(dir, jsonnetfile.LockFile)
|
||||
jsonnetfilePath := path.Join(dir, jsonnetfile.File)
|
||||
filename := lockfilePath
|
||||
isLock := true
|
||||
|
||||
lockExists, err := FileExists(filepath.Join(dir, JsonnetLockFile))
|
||||
lockExists, err := FileExists(filepath.Join(dir, jsonnetfile.LockFile))
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
|
||||
if !lockExists {
|
||||
filename = jsonnetfile
|
||||
filename = jsonnetfilePath
|
||||
isLock = false
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import (
|
|||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/pkg/jsonnetfile"
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/spec"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
|
@ -110,7 +111,7 @@ func TestLoadJsonnetfile(t *testing.T) {
|
|||
assert.Nil(t, err)
|
||||
}()
|
||||
|
||||
tempFile := filepath.Join(tempDir, JsonnetFile)
|
||||
tempFile := filepath.Join(tempDir, jsonnetfile.File)
|
||||
err = ioutil.WriteFile(tempFile, []byte(`{}`), os.ModePerm)
|
||||
assert.Nil(t, err)
|
||||
|
||||
|
|
@ -128,7 +129,7 @@ func TestLoadJsonnetfile(t *testing.T) {
|
|||
assert.Nil(t, err)
|
||||
}()
|
||||
|
||||
tempFile := filepath.Join(tempDir, JsonnetFile)
|
||||
tempFile := filepath.Join(tempDir, jsonnetfile.File)
|
||||
err = ioutil.WriteFile(tempFile, []byte(jsonnetfileContent), os.ModePerm)
|
||||
assert.Nil(t, err)
|
||||
|
||||
|
|
|
|||
17
spec/spec.go
17
spec/spec.go
|
|
@ -18,8 +18,16 @@ type JsonnetFile struct {
|
|||
Dependencies []Dependency `json:"dependencies"`
|
||||
}
|
||||
|
||||
type Dependency struct {
|
||||
Name string `json:"name"`
|
||||
Source Source `json:"source"`
|
||||
Version string `json:"version"`
|
||||
DepSource string `json:"-"`
|
||||
}
|
||||
|
||||
type Source struct {
|
||||
GitSource *GitSource `json:"git"`
|
||||
GitSource *GitSource `json:"git,omitempty"`
|
||||
LocalSource *LocalSource `json:"local,omitempty"`
|
||||
}
|
||||
|
||||
type GitSource struct {
|
||||
|
|
@ -27,9 +35,6 @@ type GitSource struct {
|
|||
Subdir string `json:"subdir"`
|
||||
}
|
||||
|
||||
type Dependency struct {
|
||||
Name string `json:"name"`
|
||||
Source Source `json:"source"`
|
||||
Version string `json:"version"`
|
||||
DepSource string `json:"-"`
|
||||
type LocalSource struct {
|
||||
Directory string `json:"directory"`
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue