-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnowsecure.go
More file actions
94 lines (81 loc) · 3.3 KB
/
Copy pathnowsecure.go
File metadata and controls
94 lines (81 loc) · 3.3 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package cmd
import (
"context"
"errors"
"os"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/nowsecure/nowsecure-ci/cmd/ns/run"
"github.com/nowsecure/nowsecure-ci/cmd/ns/version"
"github.com/nowsecure/nowsecure-ci/internal"
)
func RootCommand(ctx context.Context, v *viper.Viper, config *internal.BaseConfig) *cobra.Command {
rootCmd := &cobra.Command{
Use: "ns",
Short: "NowSecure command line tool to interact with NowSecure Platform",
Version: version.Version(),
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
v.SetEnvPrefix("NS")
v.AutomaticEnv()
v.SetConfigType("yaml")
v.SetConfigName(".ns-ci")
if err := readConfigFile(v); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
zerolog.Ctx(ctx).Debug().Msg("No config file found")
} else {
zerolog.Ctx(ctx).Debug().Err(err).Msg("Error reading from config file")
return err
}
}
baseConfig, err := internal.NewBaseConfig(v)
if err != nil {
return err
}
*config = *baseConfig
return nil
},
}
rootCmd.PersistentFlags().StringP("config", "c", "", "config file path")
rootCmd.PersistentFlags().String("api-host", "https://api.nowsecure.com", "REST API base url")
rootCmd.PersistentFlags().String("ui-host", "https://app.nowsecure.com", "UI base url")
rootCmd.PersistentFlags().String("token", "", "auth token for REST API")
rootCmd.PersistentFlags().String("group-ref", "", "group uuid with which to run assessments")
rootCmd.PersistentFlags().String("log-level", "info", "logging level")
rootCmd.PersistentFlags().StringP("output", "o", "", "write output to <file> instead of stdout.")
rootCmd.PersistentFlags().String("output-format", "json", "write output in specified format.")
rootCmd.PersistentFlags().String("ci-environment", "", "appended to the user_agent header")
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "enable verbose logging (same as --log-level debug)")
bindingErrors := []error{
v.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config")),
v.BindPFlag("api_host", rootCmd.PersistentFlags().Lookup("api-host")),
v.BindPFlag("ui_host", rootCmd.PersistentFlags().Lookup("ui-host")),
v.BindPFlag("token", rootCmd.PersistentFlags().Lookup("token")),
v.BindPFlag("group_ref", rootCmd.PersistentFlags().Lookup("group-ref")),
v.BindPFlag("output", rootCmd.PersistentFlags().Lookup("output")),
v.BindPFlag("output_format", rootCmd.PersistentFlags().Lookup("output-format")),
v.BindPFlag("log_level", rootCmd.PersistentFlags().Lookup("log-level")),
v.BindPFlag("ci_environment", rootCmd.PersistentFlags().Lookup("ci-environment")),
v.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose")),
}
if errs := errors.Join(bindingErrors...); errs != nil {
zerolog.Ctx(ctx).Panic().Err(errs).Msg("Failed binding run level flags")
}
rootCmd.MarkFlagsMutuallyExclusive("log-level", "verbose")
rootCmd.AddCommand(run.RunCommand(ctx, v, config))
return rootCmd
}
func readConfigFile(v *viper.Viper) error {
if v.IsSet("config") {
v.SetConfigFile(v.GetString("config"))
return v.ReadInConfig()
}
home, err := os.UserHomeDir()
if err == nil {
v.AddConfigPath(home)
}
v.AddConfigPath(".")
return v.ReadInConfig()
}