Skip to content

Commit

Permalink
commands/apps: Add apps spec validate-offline command (Fixes #1449)
Browse files Browse the repository at this point in the history
Implement a new command to validate an app spec without requiring
auth & connection to the API. This is useful for validating app specs
in CI pipelines and untrusted environments.  As there is no currently
published [YAML schema][1] for use with [`redhat.vscode-yaml`][2], this
seems to be the best approach for now.

[1]: https://www.schemastore.org/json/
[2]: https://github.com/redhat-developer/yaml-language-server
  • Loading branch information
trinitronx committed Oct 22, 2023
1 parent ae6c1b6 commit 385ae5f
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions commands/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,11 @@ Optionally, pass a deployment ID to get the spec of that specific deployment.`,
You may pass - as the filename to read from stdin.`, Writer)
AddBoolFlag(validateCmd, doctl.ArgSchemaOnly, "", false, "Only validate the spec schema and not the correctness of the spec.")

cmdBuilderWithInit(cmd, RunAppsSpecValidateOffline, "validate-offline <spec file>", "Validate an application spec offline (schema-only)", `Use this command to check whether a given app spec (YAML or JSON) is valid
without connecting to DigitalOcean API. (schema-only)
You may pass - as the filename to read from stdin.`, Writer, false)

return cmd
}

Expand Down Expand Up @@ -761,6 +766,35 @@ func RunAppsSpecGet(c *CmdConfig) error {
}
}

// ValidateAppSpecSchema validates an app spec (schema-only)
// returns the marshaled yaml spec as a byte array, or error
func ValidateAppSpecSchema(appSpec *godo.AppSpec) ([]byte, error) {
ymlSpec, err := yaml.Marshal(appSpec)
if err != nil {
return []byte{}, fmt.Errorf("marshaling the spec as yaml: %v", err)
}
return ymlSpec, err
}

// RunAppsSpecValidateOffline validates an app spec file without requiring auth & connection to the API
func RunAppsSpecValidateOffline(c *CmdConfig) error {
if len(c.Args) < 1 {
return doctl.NewMissingArgsErr(c.NS)
}

specPath := c.Args[0]
appSpec, err := apps.ReadAppSpec(os.Stdin, specPath)
if err != nil {
return err
}
ymlSpec, err := ValidateAppSpecSchema(appSpec)
if err != nil {
return err
}
_, err = c.Out.Write(ymlSpec)
return err
}

// RunAppsSpecValidate validates an app spec file
func RunAppsSpecValidate(c *CmdConfig) error {
if len(c.Args) < 1 {
Expand Down

0 comments on commit 385ae5f

Please sign in to comment.