-
Notifications
You must be signed in to change notification settings - Fork 28
/
pull.go
194 lines (169 loc) · 4.71 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
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
package config
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"github.com/nhost/be/services/mimir/model"
"github.com/nhost/cli/clienv"
"github.com/nhost/cli/nhostclient/graphql"
"github.com/nhost/cli/project/env"
"github.com/nhost/cli/system"
"github.com/pelletier/go-toml/v2"
"github.com/urfave/cli/v2"
)
const (
DefaultHasuraGraphqlAdminSecret = "nhost-admin-secret" //nolint:gosec
DefaultGraphqlJWTSecret = "0f987876650b4a085e64594fae9219e7781b17506bec02489ad061fba8cb22db"
DefaultNhostWebhookSecret = "nhost-webhook-secret" //nolint:gosec
)
const (
flagYes = "yes"
)
func CommandPull() *cli.Command {
return &cli.Command{ //nolint:exhaustruct
Name: "pull",
Aliases: []string{},
Usage: "Get cloud configuration",
Action: commandPull,
Flags: []cli.Flag{
&cli.StringFlag{ //nolint:exhaustruct
Name: flagSubdomain,
Usage: "Pull this subdomain's configuration. Defaults to linked project",
EnvVars: []string{"NHOST_SUBDOMAIN"},
},
&cli.BoolFlag{ //nolint:exhaustruct
Name: flagYes,
Usage: "Skip confirmation",
EnvVars: []string{"NHOST_YES"},
},
},
}
}
func commandPull(cCtx *cli.Context) error {
ce := clienv.FromCLI(cCtx)
skipConfirmation := cCtx.Bool(flagYes)
if !skipConfirmation {
if err := verifyFile(ce, ce.Path.NhostToml()); err != nil {
return err
}
}
writeSecrets := true
if !skipConfirmation {
if err := verifyFile(ce, ce.Path.Secrets()); err != nil {
writeSecrets = false
}
}
proj, err := ce.GetAppInfo(cCtx.Context, cCtx.String(flagSubdomain))
if err != nil {
return fmt.Errorf("failed to get app info: %w", err)
}
_, err = Pull(cCtx.Context, ce, proj, writeSecrets)
return err
}
func verifyFile(ce *clienv.CliEnv, name string) error {
if clienv.PathExists(name) {
ce.PromptMessage(
name + " already exists. Do you want to overwrite it? [y/N] ",
)
resp, err := ce.PromptInput(false)
if err != nil {
return fmt.Errorf("failed to read input: %w", err)
}
if resp != "y" && resp != "Y" {
return errors.New("aborting") //nolint:goerr113
}
}
return nil
}
func respToSecrets(env []*graphql.GetSecrets_AppSecrets, anonymize bool) model.Secrets {
secrets := make(model.Secrets, len(env))
for i, s := range env {
if anonymize {
switch s.Name {
case "HASURA_GRAPHQL_ADMIN_SECRET":
s.Value = DefaultHasuraGraphqlAdminSecret
case "HASURA_GRAPHQL_JWT_SECRET":
s.Value = DefaultGraphqlJWTSecret
case "NHOST_WEBHOOK_SECRET":
s.Value = DefaultNhostWebhookSecret
default:
s.Value = "FIXME"
}
}
secrets[i] = &model.ConfigEnvironmentVariable{
Name: s.Name,
Value: s.Value,
}
}
return secrets
}
func pullSecrets(
ctx context.Context,
ce *clienv.CliEnv,
proj *graphql.GetWorkspacesApps_Workspaces_Apps,
) error {
ce.Infoln("Getting secrets list from Nhost...")
cl, err := ce.GetNhostClient(ctx)
if err != nil {
return fmt.Errorf("failed to get nhost client: %w", err)
}
resp, err := cl.GetSecrets(
ctx,
proj.ID,
)
if err != nil {
return fmt.Errorf("failed to get secrets: %w", err)
}
secrets := respToSecrets(resp.GetAppSecrets(), true)
if err := clienv.MarshalFile(&secrets, ce.Path.Secrets(), env.Marshal); err != nil {
return fmt.Errorf("failed to save nhost.toml: %w", err)
}
ce.Infoln("Adding .secrets to .gitignore...")
if err := system.AddToGitignore("\n.secrets\n"); err != nil {
return fmt.Errorf("failed to add .secrets to .gitignore: %w", err)
}
return nil
}
func Pull(
ctx context.Context,
ce *clienv.CliEnv,
proj *graphql.GetWorkspacesApps_Workspaces_Apps,
writeSecrts bool,
) (*model.ConfigConfig, error) {
ce.Infoln("Pulling config from Nhost...")
cl, err := ce.GetNhostClient(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get nhost client: %w", err)
}
cfg, err := cl.GetConfigRawJSON(
ctx,
proj.ID,
)
if err != nil {
return nil, fmt.Errorf("failed to get config: %w", err)
}
var v model.ConfigConfig
if err := json.Unmarshal([]byte(cfg.ConfigRawJSON), &v); err != nil {
return nil, fmt.Errorf("failed to unmarshal config: %w", err)
}
if err := os.MkdirAll(ce.Path.NhostFolder(), 0o755); err != nil { //nolint:mnd
return nil, fmt.Errorf("failed to create nhost directory: %w", err)
}
if err := clienv.MarshalFile(v, ce.Path.NhostToml(), toml.Marshal); err != nil {
return nil, fmt.Errorf("failed to save nhost.toml: %w", err)
}
if writeSecrts {
if err := pullSecrets(ctx, ce, proj); err != nil {
return nil, err
}
}
ce.Infoln("Success!")
ce.Warnln(
"- Review `nhost/nhost.toml` and make sure there are no secrets before you commit it to git.",
)
ce.Warnln("- Review `.secrets` file and set your development secrets")
ce.Warnln("- Review `.secrets` was added to .gitignore")
return &v, nil
}