-
Notifications
You must be signed in to change notification settings - Fork 28
/
validate.go
183 lines (156 loc) · 4.36 KB
/
validate.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
package config
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"github.com/nhost/be/services/mimir/model"
"github.com/nhost/be/services/mimir/schema"
"github.com/nhost/be/services/mimir/schema/appconfig"
"github.com/nhost/cli/clienv"
"github.com/nhost/cli/project/env"
"github.com/pelletier/go-toml/v2"
"github.com/urfave/cli/v2"
jsonpatch "gopkg.in/evanphx/json-patch.v5"
)
func CommandValidate() *cli.Command {
return &cli.Command{ //nolint:exhaustruct
Name: "validate",
Aliases: []string{},
Usage: "Validate configuration",
Action: commandValidate,
Flags: []cli.Flag{
&cli.StringFlag{ //nolint:exhaustruct
Name: flagSubdomain,
Usage: "Validate this subdomain's configuration. Defaults to linked project",
EnvVars: []string{"NHOST_SUBDOMAIN"},
},
},
}
}
func commandValidate(cCtx *cli.Context) error {
ce := clienv.FromCLI(cCtx)
subdomain := cCtx.String(flagSubdomain)
if subdomain != "" && subdomain != "local" {
return ValidateRemote(
cCtx.Context,
ce,
cCtx.String(flagSubdomain),
)
}
var secrets model.Secrets
if err := clienv.UnmarshalFile(ce.Path.Secrets(), &secrets, env.Unmarshal); err != nil {
return fmt.Errorf(
"failed to parse secrets, make sure secret values are between quotes: %w",
err,
)
}
ce.Infoln("Verifying configuration...")
if _, err := Validate(ce, "local", secrets); err != nil {
return err
}
ce.Infoln("Configuration is valid!")
return nil
}
func ApplyJSONPatches[T any](
cfg T,
overlayPath string,
) (*T, error) {
f, err := os.Open(overlayPath)
if err != nil {
return nil, fmt.Errorf("failed to open json patches file: %w", err)
}
defer f.Close()
patchesb, err := io.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("failed to read json patches file: %w", err)
}
cfgb, err := json.Marshal(cfg)
if err != nil {
return nil, fmt.Errorf("failed to marshal config: %w", err)
}
patch, err := jsonpatch.DecodePatch(patchesb)
if err != nil {
return nil, fmt.Errorf("failed to apply json patches: %w", err)
}
cfgb, err = patch.Apply(cfgb)
if err != nil {
return nil, fmt.Errorf("failed to apply json patches: %w", err)
}
var r T
if err := json.Unmarshal(cfgb, &r); err != nil {
return nil, fmt.Errorf("failed to unmarshal config: %w", err)
}
return &r, nil
}
func Validate(
ce *clienv.CliEnv,
subdomain string,
secrets model.Secrets,
) (*model.ConfigConfig, error) {
cfg := &model.ConfigConfig{} //nolint:exhaustruct
if err := clienv.UnmarshalFile(ce.Path.NhostToml(), cfg, toml.Unmarshal); err != nil {
return nil, fmt.Errorf("failed to parse config: %w", err)
}
if clienv.PathExists(ce.Path.Overlay(subdomain)) {
var err error
cfg, err = ApplyJSONPatches(*cfg, ce.Path.Overlay(subdomain))
if err != nil {
return nil, fmt.Errorf("failed to apply json patches: %w", err)
}
}
schema, err := schema.New()
if err != nil {
return nil, fmt.Errorf("failed to create schema: %w", err)
}
cfg, err = appconfig.SecretsResolver[model.ConfigConfig](cfg, secrets, schema.Fill)
if err != nil {
return nil, fmt.Errorf("failed to validate config: %w", err)
}
return cfg, nil
}
func ValidateRemote(
ctx context.Context,
ce *clienv.CliEnv,
subdomain string,
) error {
cfg := &model.ConfigConfig{} //nolint:exhaustruct
if err := clienv.UnmarshalFile(ce.Path.NhostToml(), cfg, toml.Unmarshal); err != nil {
return fmt.Errorf("failed to parse config: %w", err)
}
schema, err := schema.New()
if err != nil {
return fmt.Errorf("failed to create schema: %w", err)
}
proj, err := ce.GetAppInfo(ctx, subdomain)
if err != nil {
return fmt.Errorf("failed to get app info: %w", err)
}
ce.Infoln("Getting secrets...")
cl, err := ce.GetNhostClient(ctx)
if err != nil {
return fmt.Errorf("failed to get nhost client: %w", err)
}
secretsResp, err := cl.GetSecrets(
ctx,
proj.ID,
)
if err != nil {
return fmt.Errorf("failed to get secrets: %w", err)
}
if clienv.PathExists(ce.Path.Overlay(proj.GetSubdomain())) {
var err error
cfg, err = ApplyJSONPatches(*cfg, ce.Path.Overlay(proj.GetSubdomain()))
if err != nil {
return fmt.Errorf("failed to apply json patches: %w", err)
}
}
secrets := respToSecrets(secretsResp.GetAppSecrets(), false)
_, err = appconfig.SecretsResolver[model.ConfigConfig](cfg, secrets, schema.Fill)
if err != nil {
return fmt.Errorf("failed to validate config: %w", err)
}
ce.Infoln("Config is valid!")
return nil
}