Skip to content

Commit

Permalink
cmd: adding forms validate command to the cli
Browse files Browse the repository at this point in the history
  • Loading branch information
gaelL committed Oct 5, 2020
1 parent b324f8a commit 63459f2
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/cycloid.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func AttachCommands(cmd *cobra.Command) {
// Root
root.NewStatusCmd(),
root.NewVersionCmd(),

root.NewValidateFormCmd(),
organizations.NewCommands(),
catalogRepositories.NewCommands(),
configRepositories.NewCommands(),
Expand Down
2 changes: 2 additions & 0 deletions cmd/cycloid/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ type Middleware interface {
ListStacks(org string) ([]*models.ServiceCatalog, error)
GetStack(org, ref string) (*models.ServiceCatalog, error)

ValidateForm(org string, rawForms []byte) (*models.FormsValidationResult, error)

// Login methods

// Login is the method used to log the user into the Cycloid console
Expand Down
54 changes: 54 additions & 0 deletions cmd/cycloid/middleware/stacks.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package middleware

import (
"github.com/cycloidio/cycloid-cli/client/client/organization_forms"
"github.com/cycloidio/cycloid-cli/client/client/service_catalogs"

"github.com/cycloidio/cycloid-cli/client/models"
"github.com/cycloidio/cycloid-cli/cmd/cycloid/common"
strfmt "github.com/go-openapi/strfmt"
"gopkg.in/yaml.v2"
)

func (m *middleware) ListStacks(org string) ([]*models.ServiceCatalog, error) {
Expand Down Expand Up @@ -50,3 +54,53 @@ func (m *middleware) GetStack(org, ref string) (*models.ServiceCatalog, error) {

return d, err
}

func (m *middleware) ValidateForm(org string, rawForms []byte) (*models.FormsValidationResult, error) {
var body *models.FormsValidation
var formsfile models.FormsFile

err := yaml.Unmarshal(rawForms, &formsfile)
if err != nil {
// return nil, err
// Convert swagger validation error as FormsValidationResult
// to keep the same display on validation error for the end user
ve := &models.FormsValidationResult{
Errors: []string{err.Error()},
}
return ve, nil
}

params := organization_forms.NewValidateFormsFileParams()
params.SetOrganizationCanonical(org)

body = &models.FormsValidation{
FormFile: formsfile,
}
err = body.Validate(strfmt.Default)
if err != nil {
// return nil, err
// Convert swagger validation error as FormsValidationResult
// to keep the same display on validation error for the end user
ve := &models.FormsValidationResult{
Errors: []string{err.Error()},
}
return ve, nil
}

params.SetBody(body)

resp, err := m.api.OrganizationForms.ValidateFormsFile(params, common.ClientCredentials(&org))
if err != nil {
return nil, err
}

p := resp.GetPayload()
// TODO this validate have been removed https://github.com/cycloidio/youdeploy-http-api/issues/2262
// err = p.Validate(strfmt.Default)
// if err != nil {
// return err
// }

d := p.Data
return d, err
}
73 changes: 61 additions & 12 deletions cmd/cycloid/validate-form.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,76 @@
package root

import (
"fmt"
"io/ioutil"
"os"

"github.com/cycloidio/cycloid-cli/cmd/cycloid/common"
"github.com/cycloidio/cycloid-cli/cmd/cycloid/internal"
"github.com/cycloidio/cycloid-cli/cmd/cycloid/middleware"
"github.com/cycloidio/cycloid-cli/printer"
"github.com/cycloidio/cycloid-cli/printer/factory"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

var formsFlag string

func NewValidateFormCmd() *cobra.Command {
var cmd = &cobra.Command{
Use: "validate-form",
Hidden: true,
Short: "not implemented yet",
Long: `not implemented yet`,
Use: "validate-form",
Short: "validate a .forms.yml file",
Example: `
# validate a stackforms file
cy --org my-org project get --project my-project -o yaml
`,
PreRunE: internal.CheckAPIAndCLIVersion,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("not implemented yet")
return nil
},
RunE: validateForm,
}

common.RequiredPersistentFlag(common.WithFlagOrg, cmd)

cmd.Flags().StringVar(&formsFlag, "forms", ".forms.yml", "Path to your stackform file, default .forms.yml")

return cmd
}

// /organizations/{organization_canonical}/forms/validate
// post: validateFormsFile
// Validate a forms file definition
func validateForm(cmd *cobra.Command, args []string) error {
api := common.NewAPI()
m := middleware.NewMiddleware(api)

org, err := cmd.Flags().GetString("org")
if err != nil {
return err
}
formPath, err := cmd.Flags().GetString("forms")
if err != nil {
return err
}

output, err := cmd.Flags().GetString("output")
if err != nil {
return errors.Wrap(err, "unable to get output flag")
}

rawForms, err := ioutil.ReadFile(formPath)
if err != nil {
return errors.Wrap(err, "unable to read the form file")
}

vf, err := m.ValidateForm(org, rawForms)
if err != nil {
return errors.Wrap(err, "unable validate form")
}

// fetch the printer from the factory
p, err := factory.GetPrinter(output)
if err != nil {
return errors.Wrap(err, "unable to get printer")
}

// print the result on the standard output
if err := p.Print(vf, printer.Options{}, os.Stdout); err != nil {
return errors.Wrap(err, "unable to print result")
}
return nil
}

0 comments on commit 63459f2

Please sign in to comment.