mirror of
https://github.com/TECHNOFAB11/jsonnet-bundler.git
synced 2025-12-12 08:00:05 +01:00
refactor: switch to pkg/jsonnetfile
So far, `pkg` and `pkg/jsonnetfile` had overlapping functionality when it came to choosing and loading jsonnetfiles. This fully switches to the separate package `pkg/jsonnetfile` that seems to be created for exactly this purpose
This commit is contained in:
parent
a718f48cd8
commit
71938456ae
10 changed files with 55 additions and 180 deletions
|
|
@ -18,13 +18,13 @@ import (
|
|||
"io/ioutil"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/pkg"
|
||||
kingpin "gopkg.in/alecthomas/kingpin.v2"
|
||||
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/pkg/jsonnetfile"
|
||||
"gopkg.in/alecthomas/kingpin.v2"
|
||||
)
|
||||
|
||||
func initCommand(dir string) int {
|
||||
exists, err := pkg.FileExists(jsonnetfile.File)
|
||||
exists, err := jsonnetfile.Exists(jsonnetfile.File)
|
||||
if err != nil {
|
||||
kingpin.Errorf("Failed to check for jsonnetfile.json: %v", err)
|
||||
return 1
|
||||
|
|
|
|||
|
|
@ -21,10 +21,11 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
|
||||
kingpin "gopkg.in/alecthomas/kingpin.v2"
|
||||
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/pkg"
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/pkg/jsonnetfile"
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/spec"
|
||||
"gopkg.in/alecthomas/kingpin.v2"
|
||||
)
|
||||
|
||||
func installCommand(dir, jsonnetHome string, uris ...string) int {
|
||||
|
|
|
|||
|
|
@ -21,8 +21,9 @@ import (
|
|||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/pkg/jsonnetfile"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/pkg/jsonnetfile"
|
||||
)
|
||||
|
||||
func TestInstallCommand(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -18,8 +18,9 @@ import (
|
|||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/spec"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/spec"
|
||||
)
|
||||
|
||||
func TestParseDependency(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -21,13 +21,14 @@ import (
|
|||
"net/url"
|
||||
"os"
|
||||
|
||||
kingpin "gopkg.in/alecthomas/kingpin.v2"
|
||||
|
||||
"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)
|
||||
m, err := jsonnetfile.Load(jsonnetfile.File)
|
||||
if err != nil {
|
||||
kingpin.Fatalf("failed to load jsonnetfile: %v", err)
|
||||
return 1
|
||||
|
|
|
|||
|
|
@ -31,8 +31,9 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/spec"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/spec"
|
||||
)
|
||||
|
||||
type GitPackage struct {
|
||||
|
|
|
|||
|
|
@ -18,22 +18,25 @@ import (
|
|||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/spec"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const File = "jsonnetfile.json"
|
||||
const LockFile = "jsonnetfile.lock.json"
|
||||
const (
|
||||
File = "jsonnetfile.json"
|
||||
LockFile = "jsonnetfile.lock.json"
|
||||
)
|
||||
|
||||
var ErrNoFile = errors.New("no jsonnetfile")
|
||||
|
||||
func Choose(dir string) (string, bool, error) {
|
||||
jsonnetfileLock := path.Join(dir, LockFile)
|
||||
jsonnetfile := path.Join(dir, File)
|
||||
jsonnetfileLock := filepath.Join(dir, LockFile)
|
||||
jsonnetfile := filepath.Join(dir, File)
|
||||
|
||||
lockExists, err := fileExists(jsonnetfileLock)
|
||||
lockExists, err := Exists(jsonnetfileLock)
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
|
|
@ -41,7 +44,7 @@ func Choose(dir string) (string, bool, error) {
|
|||
return jsonnetfileLock, true, nil
|
||||
}
|
||||
|
||||
fileExists, err := fileExists(jsonnetfile)
|
||||
fileExists, err := Exists(jsonnetfile)
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
|
|
@ -67,7 +70,7 @@ func Load(filepath string) (spec.JsonnetFile, error) {
|
|||
return m, nil
|
||||
}
|
||||
|
||||
func fileExists(path string) (bool, error) {
|
||||
func Exists(path string) (bool, error) {
|
||||
_, err := os.Stat(path)
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
|
|
|
|||
|
|
@ -21,9 +21,10 @@ import (
|
|||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/pkg/jsonnetfile"
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/spec"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
const notExist = "/this/does/not/exist"
|
||||
|
|
@ -159,3 +160,26 @@ func TestLoad(t *testing.T) {
|
|||
assert.Equal(t, jsonnetFileExpected, jf)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileExists(t *testing.T) {
|
||||
{
|
||||
exists, err := jsonnetfile.Exists(notExist)
|
||||
assert.False(t, exists)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
{
|
||||
tempFile, err := ioutil.TempFile("", "jb-exists")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
err := os.Remove(tempFile.Name())
|
||||
assert.Nil(t, err)
|
||||
}()
|
||||
|
||||
exists, err := jsonnetfile.Exists(tempFile.Name())
|
||||
assert.True(t, exists)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,16 +16,16 @@ package pkg
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/pkg/jsonnetfile"
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/spec"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
@ -76,11 +76,11 @@ func Install(ctx context.Context, isLock bool, dependencySourceIdentifier string
|
|||
continue
|
||||
}
|
||||
|
||||
filepath, isLock, err := ChooseJsonnetFile(destPath)
|
||||
filepath, isLock, err := jsonnetfile.Choose(destPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
depsDeps, err := LoadJsonnetfile(filepath)
|
||||
depsDeps, err := jsonnetfile.Load(filepath)
|
||||
// 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) {
|
||||
|
|
@ -127,55 +127,3 @@ func insertDependency(deps []spec.Dependency, newDep spec.Dependency) ([]spec.De
|
|||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func FileExists(path string) (bool, error) {
|
||||
_, err := os.Stat(path)
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func ChooseJsonnetFile(dir string) (string, bool, error) {
|
||||
lockfilePath := path.Join(dir, jsonnetfile.LockFile)
|
||||
jsonnetfilePath := path.Join(dir, jsonnetfile.File)
|
||||
filename := lockfilePath
|
||||
isLock := true
|
||||
|
||||
lockExists, err := FileExists(filepath.Join(dir, jsonnetfile.LockFile))
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
|
||||
if !lockExists {
|
||||
filename = jsonnetfilePath
|
||||
isLock = false
|
||||
}
|
||||
|
||||
return filename, isLock, err
|
||||
}
|
||||
|
||||
func LoadJsonnetfile(filepath string) (spec.JsonnetFile, error) {
|
||||
m := spec.JsonnetFile{}
|
||||
|
||||
if _, err := os.Stat(filepath); err != nil {
|
||||
return m, err
|
||||
}
|
||||
|
||||
f, err := os.Open(filepath)
|
||||
if err != nil {
|
||||
return m, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
err = json.NewDecoder(f).Decode(&m)
|
||||
if err != nil {
|
||||
return m, err
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,18 +15,11 @@
|
|||
package pkg
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/pkg/jsonnetfile"
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/spec"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
const NotExist = "/this/does/not/exist"
|
||||
|
||||
func TestInsertDependency(t *testing.T) {
|
||||
deps := []spec.Dependency{{Name: "test1", Version: "latest"}}
|
||||
dep := spec.Dependency{Name: "test2", Version: "latest"}
|
||||
|
|
@ -40,101 +33,3 @@ func TestInsertDependency(t *testing.T) {
|
|||
t.Fatal("Incorrectly inserted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileExists(t *testing.T) {
|
||||
{
|
||||
exists, err := FileExists(NotExist)
|
||||
assert.False(t, exists)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
{
|
||||
tempFile, err := ioutil.TempFile("", "jb-exists")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
err := os.Remove(tempFile.Name())
|
||||
assert.Nil(t, err)
|
||||
}()
|
||||
|
||||
exists, err := FileExists(tempFile.Name())
|
||||
assert.True(t, exists)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadJsonnetfile(t *testing.T) {
|
||||
empty := spec.JsonnetFile{}
|
||||
|
||||
jsonnetfileContent := `{
|
||||
"dependencies": [
|
||||
{
|
||||
"name": "foobar",
|
||||
"source": {
|
||||
"git": {
|
||||
"remote": "https://github.com/foobar/foobar",
|
||||
"subdir": ""
|
||||
}
|
||||
},
|
||||
"version": "master"
|
||||
}
|
||||
]
|
||||
}
|
||||
`
|
||||
jsonnetFileExpected := spec.JsonnetFile{
|
||||
Dependencies: []spec.Dependency{{
|
||||
Name: "foobar",
|
||||
Source: spec.Source{
|
||||
GitSource: &spec.GitSource{
|
||||
Remote: "https://github.com/foobar/foobar",
|
||||
Subdir: "",
|
||||
},
|
||||
},
|
||||
Version: "master",
|
||||
DepSource: "",
|
||||
}},
|
||||
}
|
||||
|
||||
{
|
||||
jf, err := LoadJsonnetfile(NotExist)
|
||||
assert.Equal(t, empty, jf)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
{
|
||||
tempDir, err := ioutil.TempDir("", "jb-load-jsonnetfile")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
err := os.RemoveAll(tempDir)
|
||||
assert.Nil(t, err)
|
||||
}()
|
||||
|
||||
tempFile := filepath.Join(tempDir, jsonnetfile.File)
|
||||
err = ioutil.WriteFile(tempFile, []byte(`{}`), os.ModePerm)
|
||||
assert.Nil(t, err)
|
||||
|
||||
jf, err := LoadJsonnetfile(tempFile)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, empty, jf)
|
||||
}
|
||||
{
|
||||
tempDir, err := ioutil.TempDir("", "jb-load-jsonnetfile")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
err := os.RemoveAll(tempDir)
|
||||
assert.Nil(t, err)
|
||||
}()
|
||||
|
||||
tempFile := filepath.Join(tempDir, jsonnetfile.File)
|
||||
err = ioutil.WriteFile(tempFile, []byte(jsonnetfileContent), os.ModePerm)
|
||||
assert.Nil(t, err)
|
||||
|
||||
jf, err := LoadJsonnetfile(tempFile)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, jsonnetFileExpected, jf)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue