Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add app spec validate-offline (Resolves #1449) #1450

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 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 All @@ -779,9 +813,9 @@ func RunAppsSpecValidate(c *CmdConfig) error {
}

if schemaOnly {
ymlSpec, err := yaml.Marshal(appSpec)
ymlSpec, err := ValidateAppSpecSchema(appSpec)
if err != nil {
return fmt.Errorf("marshaling the spec as yaml: %v", err)
return err
}
_, err = c.Out.Write(ymlSpec)
return err
Expand Down
47 changes: 47 additions & 0 deletions integration/apps_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,50 @@ services:
expect.Equal(expectedOutput, strings.TrimSpace(string(output)))
})
})

var _ = suite("apps/spec/validate-offline", func(t *testing.T, when spec.G, it spec.S) {
var (
expect *require.Assertions
)

it.Before(func() {
expect = require.New(t)
})

it("accepts a valid spec", func() {
cmd := exec.Command(builtBinaryPath,
"apps", "spec", "validate-offline",
"-",
)
byt, err := json.Marshal(testAppSpec)
expect.NoError(err)

cmd.Stdin = bytes.NewReader(byt)

output, err := cmd.CombinedOutput()
expect.NoError(err)

expectedOutput := "name: test\nservices:\n- github:\n branch: main\n repo: digitalocean/doctl\n name: service"
expect.Equal(expectedOutput, strings.TrimSpace(string(output)))
})

it("fails on invalid specs", func() {
cmd := exec.Command(builtBinaryPath,
"apps", "spec", "validate-offline",
"-",
)
testSpec := `name: test
services:
name: service
github:
repo: digitalocean/doctl
`
cmd.Stdin = strings.NewReader(testSpec)

output, err := cmd.CombinedOutput()
expect.Equal("exit status 1", err.Error())

expectedOutput := "Error: parsing app spec: json: cannot unmarshal object into Go struct field AppSpec.services of type []*godo.AppServiceSpec"
expect.Equal(expectedOutput, strings.TrimSpace(string(output)))
})
})