This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
/
patching.go
90 lines (76 loc) · 1.92 KB
/
patching.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package apis
import (
"path"
artifacts "github.com/Microsoft/kunlun/artifacts"
"github.com/Microsoft/kunlun/common/fileio"
"github.com/Microsoft/kunlun/common/storage"
)
type Patching struct {
stateStore storage.Store
fs fileio.Fs
}
func NewPatching(
stateStore storage.Store,
fs fileio.Fs,
) Patching {
return Patching{
stateStore: stateStore,
fs: fs,
}
}
func (p Patching) ProvisionManifest() (*artifacts.Manifest, error) {
mainArtifactFilePath, err := p.stateStore.GetMainArtifactFilePath()
if err != nil {
return nil, err
}
content, err := p.fs.ReadFile(mainArtifactFilePath)
template := NewTemplate(content)
// construct the ops
artifactsPatchDir, err := p.stateStore.GetArtifactsPatchDir()
fileInfos, err := p.fs.ReadDir(artifactsPatchDir)
opsFileArgs := []OpsFileArg{}
for _, fileInfo := range fileInfos {
fileArg := OpsFileArg{
fileReader: p.fs,
}
fileArg.UnmarshalFlag(path.Join(artifactsPatchDir, fileInfo.Name()))
opsFileArgs = append(opsFileArgs, fileArg)
}
opsFlags := OpsFlags{
OpsFiles: opsFileArgs,
}
// build the artifact vars file
artifactVarsFilePath, err := p.stateStore.GetMainArtifactVarsFilePath()
varsFileArg := VarsFileArg{
fs: p.fs,
}
err = varsFileArg.UnmarshalFlag(artifactVarsFilePath)
if err != nil {
return nil, err
}
varsStore := VarsFSStore{
fs: p.fs,
}
varsStoreFilePath, err := p.stateStore.GetMainArtifactVarsStoreFilePath()
if err != nil {
return nil, err
}
err = varsStore.UnmarshalFlag(varsStoreFilePath)
if err != nil {
return nil, err
}
varsFlags := VarFlags{
VarsFiles: []VarsFileArg{varsFileArg},
VarsFSStore: varsStore,
}
evalOpts := EvaluateOpts{
ExpectAllKeys: true,
ExpectAllVarsUsed: false,
}
content, err = template.Evaluate(varsFlags.AsVariables(), opsFlags.AsOp(), evalOpts)
if err != nil {
return nil, err
}
manifest, err := artifacts.NewManifestFromYAML(content)
return manifest, err
}