Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions internal/cmd/config/unset/unset.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
asyncFlag = globalflags.AsyncFlag
outputFormatFlag = globalflags.OutputFormatFlag
projectIdFlag = globalflags.ProjectIdFlag
verbosityFlag = globalflags.VerbosityFlag

sessionTimeLimitFlag = "session-time-limit"

Expand All @@ -38,11 +39,11 @@ const (
)

type inputModel struct {
AsyncFlag bool
OutputFormat bool
ProjectId bool

Async bool
OutputFormat bool
ProjectId bool
SessionTimeLimit bool
Verbosity bool

ArgusCustomEndpoint bool
AuthorizationCustomEndpoint bool
Expand Down Expand Up @@ -81,7 +82,7 @@ func NewCmd() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
model := parseInput(cmd)

if model.AsyncFlag {
if model.Async {
viper.Set(config.AsyncKey, config.AsyncDefault)
}
if model.OutputFormat {
Expand All @@ -90,10 +91,12 @@ func NewCmd() *cobra.Command {
if model.ProjectId {
viper.Set(config.ProjectIdKey, "")
}

if model.SessionTimeLimit {
viper.Set(config.SessionTimeLimitKey, config.SessionTimeLimitDefault)
}
if model.Verbosity {
viper.Set(config.VerbosityKey, globalflags.VerbosityDefault)
}

if model.ArgusCustomEndpoint {
viper.Set(config.ArgusCustomEndpointKey, "")
Expand Down Expand Up @@ -156,8 +159,8 @@ func configureFlags(cmd *cobra.Command) {
cmd.Flags().Bool(asyncFlag, false, "Configuration option to run commands asynchronously")
cmd.Flags().Bool(projectIdFlag, false, "Project ID")
cmd.Flags().Bool(outputFormatFlag, false, "Output format")

cmd.Flags().Bool(sessionTimeLimitFlag, false, fmt.Sprintf("Maximum time before authentication is required again. If unset, defaults to %s", config.SessionTimeLimitDefault))
cmd.Flags().Bool(verbosityFlag, false, "Verbosity of the CLI")

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

func parseInput(cmd *cobra.Command) *inputModel {
return &inputModel{
AsyncFlag: flags.FlagToBoolValue(cmd, asyncFlag),
OutputFormat: flags.FlagToBoolValue(cmd, outputFormatFlag),
ProjectId: flags.FlagToBoolValue(cmd, projectIdFlag),
Async: flags.FlagToBoolValue(cmd, asyncFlag),
OutputFormat: flags.FlagToBoolValue(cmd, outputFormatFlag),
ProjectId: flags.FlagToBoolValue(cmd, projectIdFlag),
SessionTimeLimit: flags.FlagToBoolValue(cmd, sessionTimeLimitFlag),
Verbosity: flags.FlagToBoolValue(cmd, verbosityFlag),

SessionTimeLimit: flags.FlagToBoolValue(cmd, sessionTimeLimitFlag),
ArgusCustomEndpoint: flags.FlagToBoolValue(cmd, argusCustomEndpointFlag),
AuthorizationCustomEndpoint: flags.FlagToBoolValue(cmd, authorizationCustomEndpointFlag),
DNSCustomEndpoint: flags.FlagToBoolValue(cmd, dnsCustomEndpointFlag),
Expand Down
19 changes: 14 additions & 5 deletions internal/cmd/config/unset/unset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import (

func fixtureFlagValues(mods ...func(flagValues map[string]bool)) map[string]bool {
flagValues := map[string]bool{
projectIdFlag: true,
outputFormatFlag: true,
asyncFlag: true,
outputFormatFlag: true,
projectIdFlag: true,
sessionTimeLimitFlag: true,
verbosityFlag: true,

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

func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
model := &inputModel{
ProjectId: true,
OutputFormat: true,
Async: true,
OutputFormat: true,
ProjectId: true,
SessionTimeLimit: true,
Verbosity: true,

ArgusCustomEndpoint: true,
AuthorizationCustomEndpoint: true,
Expand Down Expand Up @@ -75,8 +81,11 @@ func TestParseInput(t *testing.T) {
flagValues: map[string]bool{},
isValid: true,
expectedModel: fixtureInputModel(func(model *inputModel) {
model.ProjectId = false
model.Async = false
model.OutputFormat = false
model.ProjectId = false
model.SessionTimeLimit = false
model.Verbosity = false

model.ArgusCustomEndpoint = false
model.AuthorizationCustomEndpoint = false
Expand Down
2 changes: 2 additions & 0 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
OutputFormatKey = "output_format"
ProjectIdKey = "project_id"
SessionTimeLimitKey = "session_time_limit"
VerbosityKey = "verbosity"

ArgusCustomEndpointKey = "argus_custom_endpoint"
AuthorizationCustomEndpointKey = "authorization_custom_endpoint"
Expand Down Expand Up @@ -49,6 +50,7 @@ var ConfigKeys = []string{
OutputFormatKey,
ProjectIdKey,
SessionTimeLimitKey,
VerbosityKey,

DNSCustomEndpointKey,
LogMeCustomEndpointKey,
Expand Down
16 changes: 16 additions & 0 deletions internal/pkg/globalflags/global_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,21 @@ const (
AssumeYesFlag = "assume-yes"
OutputFormatFlag = "output-format"
ProjectIdFlag = "project-id"
VerbosityFlag = "verbosity"

JSONOutputFormat = "json"
PrettyOutputFormat = "pretty"

DebugVerbosity = "debug"
InfoVerbosity = "info"
WarningVerbosity = "warning"
ErrorVerbosity = "error"

VerbosityDefault = InfoVerbosity
)

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

type GlobalFlagModel struct {
Async bool
Expand Down Expand Up @@ -50,6 +59,13 @@ func Configure(flagSet *pflag.FlagSet) error {
}

flagSet.BoolP(AssumeYesFlag, "y", false, "If set, skips all confirmation prompts")

flagSet.Var(flags.EnumFlag(true, VerbosityDefault, verbosityFlagOptions...), VerbosityFlag, fmt.Sprintf("Verbosity of the CLI, one of %q", verbosityFlagOptions))
err = viper.BindPFlag(config.VerbosityKey, flagSet.Lookup(VerbosityFlag))
if err != nil {
return fmt.Errorf("bind --%s flag to config: %w", VerbosityFlag, err)
}

return nil
}

Expand Down