/
default.go
67 lines (53 loc) · 1.66 KB
/
default.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package commands
import (
"context"
"errors"
"fmt"
"log"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// These values are set at build time
var (
version = ""
commit = ""
date = ""
)
// NewDefaultCommand creates the default command
func NewDefaultCommand() *cobra.Command {
cmd := cobra.Command{
Use: "conftest <subcommand>",
Short: "Test your configuration files using Open Policy Agent",
Version: fmt.Sprintf("Version: %s\nCommit: %s\nDate: %s\n", version, commit, date),
}
cmd.SetVersionTemplate(`{{.Version}}`)
cmd.PersistentFlags().StringP("policy", "p", "policy", "path to the Rego policy files directory. For the test command, specifying a specific .rego file is allowed.")
cmd.PersistentFlags().Bool("no-color", false, "disable color when printing")
viper.BindPFlag("policy", cmd.PersistentFlags().Lookup("policy"))
viper.BindPFlag("no-color", cmd.PersistentFlags().Lookup("no-color"))
viper.SetEnvPrefix("CONFTEST")
viper.SetConfigName("conftest")
viper.AddConfigPath(".")
viper.AutomaticEnv()
logger := log.New(os.Stdout, "", log.LstdFlags)
ctx := context.Background()
if err := viper.ReadInConfig(); err != nil {
var e viper.ConfigFileNotFoundError
if !errors.As(err, &e) {
logger.Fatalf("error reading config: %s", err)
}
}
cmd.AddCommand(NewTestCommand(ctx))
cmd.AddCommand(NewParseCommand(ctx))
cmd.AddCommand(NewPushCommand(ctx, logger))
cmd.AddCommand(NewPullCommand(ctx))
cmd.AddCommand(NewVerifyCommand(ctx))
cmd.AddCommand(NewPluginCommand(ctx))
pluginCmds, err := loadPlugins(ctx)
if err != nil {
logger.Fatalf("error loading plugins: %v", err)
}
cmd.AddCommand(pluginCmds...)
return &cmd
}