Skip to content

Commit baddb67

Browse files
committed
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.
1 parent 9d08943 commit baddb67

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

cmd/validate.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package cmd
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"os"
7+
8+
client "github.com/semaphoreci/cli/api/client"
9+
"github.com/semaphoreci/cli/cmd/utils"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
type YamlBody struct {
14+
YamlDefinition string `json:"yaml_definition"`
15+
}
16+
17+
var validateCmd = &cobra.Command{
18+
Use: "validate",
19+
Short: "Validate a pipeline yaml file",
20+
Long: "",
21+
Run: func(cmd *cobra.Command, args []string) {
22+
c := client.NewBaseClientFromConfig()
23+
24+
if len(args) != 1 {
25+
fmt.Println("Usage: sem validate [PATH_TO_YAML_FILE]")
26+
os.Exit(1)
27+
}
28+
29+
fileContent, err := os.ReadFile(args[0])
30+
utils.Check(err)
31+
32+
body := YamlBody{
33+
YamlDefinition: string(fileContent),
34+
}
35+
36+
json_body, err := json.Marshal(body)
37+
utils.Check(err)
38+
39+
response, status, err := c.Post("yaml", json_body)
40+
utils.Check(err)
41+
42+
if status == 200 {
43+
fmt.Println("Valid!")
44+
} else {
45+
fmt.Println("Not valid!")
46+
}
47+
fmt.Println(string(response))
48+
},
49+
}
50+
51+
func init() {
52+
RootCmd.AddCommand(validateCmd)
53+
}

0 commit comments

Comments
 (0)