Skip to content

Commit 71719b5

Browse files
joaopaletDiogoFerrao
authored andcommitted
Add global verbosity flag (#178)
1 parent cb2d132 commit 71719b5

File tree

4 files changed

+47
-16
lines changed

4 files changed

+47
-16
lines changed

internal/cmd/config/unset/unset.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const (
1717
asyncFlag = globalflags.AsyncFlag
1818
outputFormatFlag = globalflags.OutputFormatFlag
1919
projectIdFlag = globalflags.ProjectIdFlag
20+
verbosityFlag = globalflags.VerbosityFlag
2021

2122
sessionTimeLimitFlag = "session-time-limit"
2223

@@ -38,11 +39,11 @@ const (
3839
)
3940

4041
type inputModel struct {
41-
AsyncFlag bool
42-
OutputFormat bool
43-
ProjectId bool
44-
42+
Async bool
43+
OutputFormat bool
44+
ProjectId bool
4545
SessionTimeLimit bool
46+
Verbosity bool
4647

4748
ArgusCustomEndpoint bool
4849
AuthorizationCustomEndpoint bool
@@ -81,7 +82,7 @@ func NewCmd() *cobra.Command {
8182
RunE: func(cmd *cobra.Command, args []string) error {
8283
model := parseInput(cmd)
8384

84-
if model.AsyncFlag {
85+
if model.Async {
8586
viper.Set(config.AsyncKey, config.AsyncDefault)
8687
}
8788
if model.OutputFormat {
@@ -90,10 +91,12 @@ func NewCmd() *cobra.Command {
9091
if model.ProjectId {
9192
viper.Set(config.ProjectIdKey, "")
9293
}
93-
9494
if model.SessionTimeLimit {
9595
viper.Set(config.SessionTimeLimitKey, config.SessionTimeLimitDefault)
9696
}
97+
if model.Verbosity {
98+
viper.Set(config.VerbosityKey, globalflags.VerbosityDefault)
99+
}
97100

98101
if model.ArgusCustomEndpoint {
99102
viper.Set(config.ArgusCustomEndpointKey, "")
@@ -156,8 +159,8 @@ func configureFlags(cmd *cobra.Command) {
156159
cmd.Flags().Bool(asyncFlag, false, "Configuration option to run commands asynchronously")
157160
cmd.Flags().Bool(projectIdFlag, false, "Project ID")
158161
cmd.Flags().Bool(outputFormatFlag, false, "Output format")
159-
160162
cmd.Flags().Bool(sessionTimeLimitFlag, false, fmt.Sprintf("Maximum time before authentication is required again. If unset, defaults to %s", config.SessionTimeLimitDefault))
163+
cmd.Flags().Bool(verbosityFlag, false, "Verbosity of the CLI")
161164

162165
cmd.Flags().Bool(argusCustomEndpointFlag, false, "Argus API base URL. If unset, uses the default base URL")
163166
cmd.Flags().Bool(authorizationCustomEndpointFlag, false, "Authorization API base URL. If unset, uses the default base URL")
@@ -178,11 +181,12 @@ func configureFlags(cmd *cobra.Command) {
178181

179182
func parseInput(cmd *cobra.Command) *inputModel {
180183
return &inputModel{
181-
AsyncFlag: flags.FlagToBoolValue(cmd, asyncFlag),
182-
OutputFormat: flags.FlagToBoolValue(cmd, outputFormatFlag),
183-
ProjectId: flags.FlagToBoolValue(cmd, projectIdFlag),
184+
Async: flags.FlagToBoolValue(cmd, asyncFlag),
185+
OutputFormat: flags.FlagToBoolValue(cmd, outputFormatFlag),
186+
ProjectId: flags.FlagToBoolValue(cmd, projectIdFlag),
187+
SessionTimeLimit: flags.FlagToBoolValue(cmd, sessionTimeLimitFlag),
188+
Verbosity: flags.FlagToBoolValue(cmd, verbosityFlag),
184189

185-
SessionTimeLimit: flags.FlagToBoolValue(cmd, sessionTimeLimitFlag),
186190
ArgusCustomEndpoint: flags.FlagToBoolValue(cmd, argusCustomEndpointFlag),
187191
AuthorizationCustomEndpoint: flags.FlagToBoolValue(cmd, authorizationCustomEndpointFlag),
188192
DNSCustomEndpoint: flags.FlagToBoolValue(cmd, dnsCustomEndpointFlag),

internal/cmd/config/unset/unset_test.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ import (
99

1010
func fixtureFlagValues(mods ...func(flagValues map[string]bool)) map[string]bool {
1111
flagValues := map[string]bool{
12-
projectIdFlag: true,
13-
outputFormatFlag: true,
12+
asyncFlag: true,
13+
outputFormatFlag: true,
14+
projectIdFlag: true,
15+
sessionTimeLimitFlag: true,
16+
verbosityFlag: true,
1417

1518
argusCustomEndpointFlag: true,
1619
authorizationCustomEndpointFlag: true,
@@ -34,8 +37,11 @@ func fixtureFlagValues(mods ...func(flagValues map[string]bool)) map[string]bool
3437

3538
func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
3639
model := &inputModel{
37-
ProjectId: true,
38-
OutputFormat: true,
40+
Async: true,
41+
OutputFormat: true,
42+
ProjectId: true,
43+
SessionTimeLimit: true,
44+
Verbosity: true,
3945

4046
ArgusCustomEndpoint: true,
4147
AuthorizationCustomEndpoint: true,
@@ -75,8 +81,11 @@ func TestParseInput(t *testing.T) {
7581
flagValues: map[string]bool{},
7682
isValid: true,
7783
expectedModel: fixtureInputModel(func(model *inputModel) {
78-
model.ProjectId = false
84+
model.Async = false
7985
model.OutputFormat = false
86+
model.ProjectId = false
87+
model.SessionTimeLimit = false
88+
model.Verbosity = false
8089

8190
model.ArgusCustomEndpoint = false
8291
model.AuthorizationCustomEndpoint = false

internal/pkg/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const (
1515
OutputFormatKey = "output_format"
1616
ProjectIdKey = "project_id"
1717
SessionTimeLimitKey = "session_time_limit"
18+
VerbosityKey = "verbosity"
1819

1920
ArgusCustomEndpointKey = "argus_custom_endpoint"
2021
AuthorizationCustomEndpointKey = "authorization_custom_endpoint"
@@ -49,6 +50,7 @@ var ConfigKeys = []string{
4950
OutputFormatKey,
5051
ProjectIdKey,
5152
SessionTimeLimitKey,
53+
VerbosityKey,
5254

5355
DNSCustomEndpointKey,
5456
LogMeCustomEndpointKey,

internal/pkg/globalflags/global_flags.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,21 @@ const (
1616
AssumeYesFlag = "assume-yes"
1717
OutputFormatFlag = "output-format"
1818
ProjectIdFlag = "project-id"
19+
VerbosityFlag = "verbosity"
1920

2021
JSONOutputFormat = "json"
2122
PrettyOutputFormat = "pretty"
23+
24+
DebugVerbosity = "debug"
25+
InfoVerbosity = "info"
26+
WarningVerbosity = "warning"
27+
ErrorVerbosity = "error"
28+
29+
VerbosityDefault = InfoVerbosity
2230
)
2331

2432
var outputFormatFlagOptions = []string{JSONOutputFormat, PrettyOutputFormat}
33+
var verbosityFlagOptions = []string{DebugVerbosity, InfoVerbosity, WarningVerbosity, ErrorVerbosity}
2534

2635
type GlobalFlagModel struct {
2736
Async bool
@@ -50,6 +59,13 @@ func Configure(flagSet *pflag.FlagSet) error {
5059
}
5160

5261
flagSet.BoolP(AssumeYesFlag, "y", false, "If set, skips all confirmation prompts")
62+
63+
flagSet.Var(flags.EnumFlag(true, VerbosityDefault, verbosityFlagOptions...), VerbosityFlag, fmt.Sprintf("Verbosity of the CLI, one of %q", verbosityFlagOptions))
64+
err = viper.BindPFlag(config.VerbosityKey, flagSet.Lookup(VerbosityFlag))
65+
if err != nil {
66+
return fmt.Errorf("bind --%s flag to config: %w", VerbosityFlag, err)
67+
}
68+
5369
return nil
5470
}
5571

0 commit comments

Comments
 (0)