-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathlint_cmd.go
46 lines (40 loc) · 1.14 KB
/
lint_cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package linter
import (
"fmt"
"github.com/spf13/cobra"
)
var lintFlagFormat string
func NewLintCmd() *cobra.Command {
lintCmd := &cobra.Command{
Use: "lint <config_file>",
Short: "🛑 Lint GO Feature Flag configuration file.",
Long: `🛑 Validate GO Feature Flag configuration file, it will return an error if your file is not valid.`,
RunE: func(cmd *cobra.Command, args []string) error {
return runLint(cmd, args, lintFlagFormat)
},
}
lintCmd.Flags().
StringVarP(&lintFlagFormat, "format", "f", "yaml", "Format of your input file (YAML, JSON or TOML)")
return lintCmd
}
func runLint(cmd *cobra.Command, args []string, lintFlagFormat string) error {
l := Linter{
InputFile: getFilePath(args),
InputFormat: lintFlagFormat,
}
if errs := l.Lint(); len(errs) > 0 {
for _, err := range errs {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "%s\n", err)
}
cmd.SilenceUsage = true
return fmt.Errorf("invalid GO Feature Flag configuration")
}
_, err := fmt.Fprint(cmd.OutOrStdout(), "Valid GO Feature Flag configuration")
return err
}
func getFilePath(args []string) string {
if len(args) == 0 {
return ""
}
return args[0]
}