-
Notifications
You must be signed in to change notification settings - Fork 4
/
pull.go
169 lines (129 loc) · 4.53 KB
/
pull.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package cstore
import (
"bytes"
"fmt"
"github.com/subosito/gotenv"
"github.com/turnerlabs/cstore/v4/components/models"
"github.com/turnerlabs/cstore/v4/components/remote"
"github.com/turnerlabs/cstore/v4/components/catalog"
"github.com/turnerlabs/cstore/v4/components/cfg"
"github.com/turnerlabs/cstore/v4/components/path"
"github.com/turnerlabs/cstore/v4/components/token"
)
// Pull retrieves configuration fron a remote store using cstore.yml
func Pull(catalogPath string, o Options) ([]byte, error) {
opt := o.ToUserOptions()
data := []byte{}
//-------------------------------------------------
//- Get the local catalog for reference.
//-------------------------------------------------
clog, err := catalog.Get(catalogPath)
if err != nil {
return data, err
}
root := path.RemoveFileName(catalogPath)
files := clog.FilesBy(opt.GetPaths(clog.CWD), opt.TagList, opt.AllTags, opt.Version)
if len(opt.Version) > 0 && len(files) == 0 {
files = clog.FilesBy(opt.GetPaths(clog.CWD), opt.TagList, opt.AllTags, "")
}
if len(files) == 0 {
return data, fmt.Errorf("FileNotFoundError: file not found in %s", catalogPath)
}
for _, fileEntry := range files {
// //-----------------------------------------------------
// //- Override saved file settings with user preferences.
// //-----------------------------------------------------
fileEntry = remote.OverrideFileSettings(fileEntry, opt)
//----------------------------------------------------
//- Check for a linked catalog with child files.
//----------------------------------------------------
if fileEntry.IsRef {
tempConfig, err := Pull(path.BuildPath(root, fileEntry.Path), o)
if err != nil {
return tempConfig, err
}
data = append(data, tempConfig...)
continue
}
//----------------------------------------------------
//- Get the remote store and vaults components ready.
//----------------------------------------------------
fileEntryTemp := fileEntry
remoteComp, err := remote.InitComponents(&fileEntryTemp, clog, opt, models.IO{})
if err != nil {
p := path.BuildPath(root, fileEntry.Path)
if len(opt.Version) > 0 {
p = fmt.Sprintf("%s (%s)", p, opt.Version)
}
return data, fmt.Errorf("PullFailedError1: %s (%s)", p, err)
}
//----------------------------------------------------
//- Pull remote file from store.
//----------------------------------------------------
file, _, err := remoteComp.Store.Pull(&fileEntry, opt.Version)
if err != nil {
p := path.BuildPath(root, fileEntry.Path)
if len(opt.Version) > 0 {
p = fmt.Sprintf("%s (%s)", p, opt.Version)
}
return data, fmt.Errorf("PullFailedError2: %s (%s)", p, err)
}
//-------------------------------------------------
//- If user specifies, inject secrets into file.
//-------------------------------------------------
fileWithSecrets := file
if opt.InjectSecrets {
if !fileEntry.SupportsSecrets() {
return data, fmt.Errorf("IncompatibleFileError: %s secrets not supported", fileEntry.Path)
}
tokens, err := token.Find(fileWithSecrets, fileEntry.Type, false)
if err != nil {
return data, fmt.Errorf("MissingTokensError: failed to find tokens in file %s (%s)", fileEntry.Path, err)
}
for k, t := range tokens {
value, err := remoteComp.Secrets.Get(clog.Context, t.Secret(), t.Prop)
if err != nil {
return data, fmt.Errorf("GetSecretValueError: failed to get value for %s/%s for %s (%s)", t.Secret(), t.Prop, path.BuildPath(root, fileEntry.Path), err)
}
t.Value = value
tokens[k] = t
}
fileWithSecrets, err = token.Replace(fileWithSecrets, fileEntry.Type, tokens, false)
if err != nil {
return data, fmt.Errorf("TokenReplacementError: failed to replace tokens in file %s (%s)", fileEntry.Path, err)
}
}
data = fileWithSecrets
}
return data, nil
}
// PullEnv retrieves configuration stored in .env format as a map
func PullEnv(catalogPath string, o Options) (map[string]string, error) {
config := map[string]string{}
b, err := Pull(catalogPath, o)
if err != nil {
return config, err
}
envvars := gotenv.Parse(bytes.NewReader(b))
for k, v := range envvars {
config[k] = v
}
return config, nil
}
type Options struct {
AllTags bool
Tags []string
Paths []string
Version string
InjectSecrets bool
}
func (o Options) ToUserOptions() cfg.UserOptions {
return cfg.UserOptions{
Paths: o.Paths,
Version: o.Version,
TagList: o.Tags,
AllTags: o.AllTags,
InjectSecrets: o.InjectSecrets,
Silent: true,
}
}