-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd: adding forms validate command to the cli
- Loading branch information
Showing
4 changed files
with
118 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |