mirror of
https://github.com/TECHNOFAB11/jsonnet-bundler.git
synced 2025-12-11 23:50: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,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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue