mirror of
https://github.com/TECHNOFAB11/jsonnet-bundler.git
synced 2026-02-03 01:45:09 +01:00
Create pkg/jsonnetfile to better encapsulate jsonnetfile things
This commit is contained in:
parent
27c36f6160
commit
f71ef86d48
2 changed files with 213 additions and 0 deletions
66
pkg/jsonnetfile/jsonnetfile.go
Normal file
66
pkg/jsonnetfile/jsonnetfile.go
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
package jsonnetfile
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/jsonnet-bundler/jsonnet-bundler/spec"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const File = "jsonnetfile.json"
|
||||
const 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)
|
||||
|
||||
lockExists, err := fileExists(jsonnetfileLock)
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
if lockExists {
|
||||
return jsonnetfileLock, true, nil
|
||||
}
|
||||
|
||||
fileExists, err := fileExists(jsonnetfile)
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
if fileExists {
|
||||
return jsonnetfile, false, nil
|
||||
}
|
||||
|
||||
return "", false, ErrNoFile
|
||||
}
|
||||
|
||||
func Load(filepath string) (spec.JsonnetFile, error) {
|
||||
m := spec.JsonnetFile{}
|
||||
|
||||
bytes, err := ioutil.ReadFile(filepath)
|
||||
if err != nil {
|
||||
return m, errors.Wrap(err, "failed to read file")
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(bytes, &m); err != nil {
|
||||
return m, errors.Wrap(err, "failed to unmarshal file")
|
||||
}
|
||||
|
||||
return m, 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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue