-
Notifications
You must be signed in to change notification settings - Fork 51
/
files.go
59 lines (52 loc) · 1.63 KB
/
files.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package kustomizes
import (
"io/ioutil"
"path/filepath"
"github.com/jenkins-x/jx-helpers/v3/pkg/files"
"github.com/pkg/errors"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
// LazyCreate lazily creates the kustomization configuration
func LazyCreate(k *types.Kustomization) *types.Kustomization {
if k == nil {
k = &types.Kustomization{}
}
k.FixKustomizationPostUnmarshalling()
return k
}
// LoadKustomization loads the kustomization yaml file from the given directory
func LoadKustomization(dir string) (*types.Kustomization, error) {
fileName := filepath.Join(dir, "kustomization.yaml")
exists, err := files.FileExists(fileName)
if err != nil {
return nil, errors.Wrapf(err, "failed to check if file exists %s", fileName)
}
answer := &types.Kustomization{}
answer.FixKustomizationPostUnmarshalling()
if !exists {
return answer, nil
}
data, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, errors.Wrapf(err, "failed to load file %s", fileName)
}
err = yaml.Unmarshal(data, answer)
if err != nil {
return nil, errors.Wrapf(err, "failed parse YAML file %s", fileName)
}
return answer, nil
}
// SaveKustomization saves the kustomisation file in the given directory
func SaveKustomization(kustomization *types.Kustomization, dir string) error {
data, err := yaml.Marshal(kustomization)
if err != nil {
return errors.Wrapf(err, "failed to marshal Kustomization")
}
fileName := filepath.Join(dir, "kustomization.yaml")
err = ioutil.WriteFile(fileName, data, files.DefaultFileWritePermissions)
if err != nil {
return errors.Wrapf(err, "failed write file %s", fileName)
}
return nil
}