feat/sg/msp: add 'sg msp validate' for validating service specifications (#62973)

This commit is contained in:
Robert Lin 2024-05-30 09:11:36 -07:00 committed by GitHub
parent 27f0d725ac
commit 9e4a8e8033
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 2 deletions

View File

@ -61,7 +61,7 @@ func Open(specPath string) (*Spec, error) {
}
spec, err := parse(specData)
if err != nil {
return nil, errors.Wrap(err, "spec.parse")
return spec, errors.Wrap(err, "spec.parse")
}
// Load extraneous resources
@ -134,7 +134,7 @@ func parse(data []byte) (*Spec, error) {
}
if validationErrs := s.Validate(); len(validationErrs) > 0 {
return nil, errors.Append(nil, validationErrs...)
return &s, errors.Append(nil, validationErrs...)
}
return &s, nil
}

View File

@ -1043,6 +1043,44 @@ This command supports completions on services and environments.
},
},
},
{
Name: "validate",
Usage: "Validate MSP configurations",
Before: msprepo.UseManagedServicesRepo,
Action: func(c *cli.Context) error {
services, err := msprepo.ListServices()
if err != nil {
return err
}
for _, svc := range services {
s, err := spec.Open(msprepo.ServiceYAMLPath(svc))
// HACK: Check nil instead of error so that we can get the
// itemized list of errors instead with s.Validate() for
// validation errors.
if s == nil {
std.Out.WriteFailuref("[%s] Could not open spec: %s", svc, err.Error())
continue
}
errs := s.Validate()
if len(errs) == 0 {
std.Out.WriteSuccessf("[%s] Validated", svc)
continue
}
std.Out.WriteFailuref("[%s] Found valdiation errors", svc)
var messages []string
for _, err := range errs {
messages = append(messages, fmt.Sprintf("- %s", err.Error()))
}
if err := std.Out.WriteMarkdown(strings.Join(messages, "\n")); err != nil {
return err
}
}
std.Out.Writef("Checked %d service specifications", len(services))
return nil
},
},
{
Name: "fleet",
Usage: "Summarize aspects of the MSP fleet",