/
export.go
261 lines (215 loc) · 6.74 KB
/
export.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"sort"
"strings"
"github.com/manifoldco/go-manifold"
"github.com/urfave/cli"
"github.com/manifoldco/manifold-cli/api"
"github.com/manifoldco/manifold-cli/clients"
"github.com/manifoldco/manifold-cli/middleware"
"github.com/manifoldco/manifold-cli/prompts"
mClient "github.com/manifoldco/manifold-cli/generated/marketplace/client"
"github.com/manifoldco/manifold-cli/generated/marketplace/client/credential"
"github.com/manifoldco/manifold-cli/generated/marketplace/models"
)
var formats = []string{"env", "bash", "powershell", "fish", "cmd", "json"}
func init() {
formatFlagStr := fmt.Sprintf("Export format of the secrets (%s)", strings.Join(formats, ", "))
exportCmd := cli.Command{
Name: "export",
Usage: "Export all environment variables from all resources",
Category: "CONFIGURATION",
Action: middleware.Chain(middleware.LoadDirPrefs, middleware.EnsureSession,
middleware.LoadTeamPrefs, export),
Flags: append(teamFlags, []cli.Flag{
formatFlag(formats[0], formatFlagStr),
projectFlag(),
}...),
}
cmds = append(cmds, exportCmd)
}
func export(cliCtx *cli.Context) error {
ctx := context.Background()
projectName, err := validateName(cliCtx, "project")
if err != nil {
return err
}
format := cliCtx.String("format")
if !validFormat(format) {
return cli.NewExitError("You provided an invalid format!", -1)
}
teamID, err := validateTeamID(cliCtx)
if err != nil {
return err
}
client, err := api.New(api.Analytics, api.Marketplace)
if err != nil {
return err
}
// we need to fetch all the credentials for the rMap for resource naming, etc
prompts.SpinStart("Fetching Resources")
resources, err := clients.FetchResources(ctx, client.Marketplace, teamID, projectName)
prompts.SpinStop()
if err != nil {
return cli.NewExitError("Could not retrieve resources: "+err.Error(), -1)
}
sort.Slice(resources, func(i, j int) bool {
return resources[i].Body.Name < resources[j].Body.Name
})
cMap := make(map[manifold.ID][]*models.Credential)
if projectName != "" {
p, err := clients.FetchProjectByLabel(ctx, client.Marketplace, teamID, projectName)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Could not retrieve project: %s", err), -1)
}
cMap, err = fetchProjectCredentials(ctx, client.Marketplace, p, true)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Could not retrieve credentials: %s", err), -1)
}
} else {
resources = filterResourcesWithoutProjects(resources)
cMap, err = fetchResourceCredentials(ctx, client.Marketplace, resources, true)
if err != nil {
return cli.NewExitError("Could not retrieve credentials: "+err.Error(), -1)
}
}
params := map[string]string{
format: format,
}
if projectName != "" {
params["project"] = projectName
}
client.Analytics.Track(ctx, "Fetch Credentials", ¶ms)
rMap := indexResources(resources)
w := os.Stdout
switch format {
case "env":
err = writeFormat(w, rMap, cMap, "%s=%s\n")
case "bash":
err = writeFormat(w, rMap, cMap, "export %s=%s\n")
case "powershell":
err = writeFormat(w, rMap, cMap, "$Env:%s = \"%s\"\n")
case "cmd":
err = writeFormat(w, rMap, cMap, "set %s=%s\n")
case "fish":
err = writeFormat(w, rMap, cMap, "set -x %s %s;\n")
case "json":
err = writeJSON(w, cMap)
default:
return cli.NewExitError("Unrecognized format value: "+format, -1)
}
if err != nil {
cli.NewExitError("Could not output to format: "+err.Error(), -1)
}
return nil
}
func validFormat(format string) bool {
for _, f := range formats {
if f == format {
return true
}
}
return false
}
func writeFormat(w io.Writer, rMap map[manifold.ID]*models.Resource,
cMap map[manifold.ID][]*models.Credential, format string) error {
for rID, credentials := range cMap {
resource := rMap[rID]
fmt.Fprintf(w, "# %s\n", resource.Body.Name)
for _, c := range credentials {
for name, value := range c.Body.Values {
fmt.Fprintf(w, format, name, value)
}
}
fmt.Fprintf(w, "\n")
}
return nil
}
func writeJSON(w io.Writer, cMap map[manifold.ID][]*models.Credential) error {
credentials, err := flattenCMap(cMap)
if err != nil {
return err
}
b, err := json.MarshalIndent(credentials, "", " ")
if err != nil {
return err
}
fmt.Fprintf(w, "%s\n", b)
return nil
}
func flattenCMap(cMap map[manifold.ID][]*models.Credential) (map[string]string, error) {
out := make(map[string]string)
for _, credentials := range cMap {
for _, c := range credentials {
for name, value := range c.Body.Values {
out[name] = value
}
}
}
return out, nil
}
func indexResources(resources []*models.Resource) map[manifold.ID]*models.Resource {
index := make(map[manifold.ID]*models.Resource)
for _, resource := range resources {
index[resource.ID] = resource
}
return index
}
func fetchResourceCredentials(ctx context.Context, m *mClient.Marketplace, resources []*models.Resource, customNames bool) (map[manifold.ID][]*models.Credential, error) {
p := credential.NewGetCredentialsParamsWithContext(ctx)
if customNames == false {
noCustomNames := "false"
p.SetCustomNames(&noCustomNames)
}
// Prep the credential map with the known resources, and build a list
// of IDs to use in the resource_id query param
var resourceIDs []string
for _, r := range resources {
resourceIDs = append(resourceIDs, r.ID.String())
}
p.SetResourceID(resourceIDs)
return fetchCredentials(m, p)
}
func fetchProjectCredentials(ctx context.Context, m *mClient.Marketplace, project *models.Project, customNames bool) (map[manifold.ID][]*models.Credential, error) {
_ = make(map[manifold.ID][]*models.Credential)
p := credential.NewGetCredentialsParamsWithContext(ctx)
if customNames == false {
noCustomNames := "false"
p.SetCustomNames(&noCustomNames)
}
pid := project.ID.String()
p.SetProjectID(&pid)
return fetchCredentials(m, p)
}
func fetchCredentials(m *mClient.Marketplace, params *credential.GetCredentialsParams) (map[manifold.ID][]*models.Credential, error) {
cMap := make(map[manifold.ID][]*models.Credential)
// Get credentials for all the defined resources withring(call
c, err := m.Credential.GetCredentials(params, nil)
if err != nil {
return nil, err
}
// Append credential results to the map
for _, credential := range c.Payload {
rid := credential.Body.ResourceID
if _, ok := cMap[rid]; !ok {
cMap[rid] = []*models.Credential{}
}
cMap[credential.Body.ResourceID] = append(cMap[credential.Body.ResourceID], credential)
}
return cMap, nil
}
// filterResourcesWithoutProjects returns resources without a project id
func filterResourcesWithoutProjects(resources []*models.Resource) []*models.Resource {
var results []*models.Resource
for _, r := range resources {
if r.Body.ProjectID == nil {
results = append(results, r)
}
}
return results
}