only write jsonnnet files if we made changes (#56)

Adding a dep, or updating a dependency version makes writes to the
jsonnet files.

We evaluate the changes on each of the files. An empty jsonnetfile.json
does not create a corresponding lockfile, as a missing lockfile is not
different from its previous (non existent).
This commit is contained in:
David Genest 2019-11-08 09:36:13 -05:00 committed by sh0rez
parent 7fc7c31856
commit f4417ac665
3 changed files with 124 additions and 12 deletions

View file

@ -33,13 +33,21 @@ var ErrNoFile = errors.New("no jsonnetfile")
// Load reads a jsonnetfile.(lock).json from disk
func Load(filepath string) (spec.JsonnetFile, error) {
m := spec.New()
bytes, err := ioutil.ReadFile(filepath)
if err != nil {
return m, err
return spec.New(), err
}
return Unmarshal(bytes)
}
// Unmarshal creates a spec.JsonnetFile from bytes. Empty bytes
// will create an empty spec.
func Unmarshal(bytes []byte) (spec.JsonnetFile, error) {
m := spec.New()
if len(bytes) == 0 {
return m, nil
}
if err := json.Unmarshal(bytes, &m); err != nil {
return m, errors.Wrap(err, "failed to unmarshal file")
}