From 0162689fec67b3484e80a82efe2ab3b0e2e44210 Mon Sep 17 00:00:00 2001 From: Caius Durling Date: Wed, 18 Oct 2023 00:12:00 +0100 Subject: [PATCH] Validate pipeline file using `sem validate FILE` When you're authoring files locally it's a pain pushing changes to double-check the YAML syntax is corrected, that you've nested the blocks correctly and that you've put your `run:` clause at the right level. Make it easy to validate the syntax of the file from local machines. --- cmd/validate.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 cmd/validate.go diff --git a/cmd/validate.go b/cmd/validate.go new file mode 100644 index 0000000..93f9fc5 --- /dev/null +++ b/cmd/validate.go @@ -0,0 +1,53 @@ +package cmd + +import ( + "encoding/json" + "fmt" + "os" + + client "github.com/semaphoreci/cli/api/client" + "github.com/semaphoreci/cli/cmd/utils" + "github.com/spf13/cobra" +) + +type YamlBody struct { + YamlDefinition string `json:"yaml_definition"` +} + +var validateCmd = &cobra.Command{ + Use: "validate", + Short: "Validate a pipeline yaml file", + Long: "", + Run: func(cmd *cobra.Command, args []string) { + c := client.NewBaseClientFromConfig() + + if len(args) != 1 { + fmt.Println("Usage: sem validate [PATH_TO_YAML_FILE]") + os.Exit(1) + } + + fileContent, err := os.ReadFile(args[0]) + utils.Check(err) + + body := YamlBody{ + YamlDefinition: string(fileContent), + } + + json_body, err := json.Marshal(body) + utils.Check(err) + + response, status, err := c.Post("yaml", json_body) + utils.Check(err) + + if status == 200 { + fmt.Println("Valid!") + } else { + fmt.Println("Not valid!") + } + fmt.Println(string(response)) + }, +} + +func init() { + RootCmd.AddCommand(validateCmd) +}