this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

cmd/cue: reject login --token=""

Don't go straight into the OAuth device flow as if the flag had not
been given at all, because that can be rather confusing in the case
where a developer used --token=${UNSET_VAR}.

An alternative would have been a custom pflag.Value implementaion
that recorded whether the string value was set via a boolean field.
However, that feels unnecessary given that looping over the set flags
is pretty easy and works with any flag of any type already.

The linear looping is fine given that we don't expect CLI users
to set more than a dozen or so flags at any time.

Fixes #3665.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I7de34e3b3503d6fd495e10ec9f64851f2a6ddd11
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1207558
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.io>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

+22
+13
cmd/cue/cmd/flags.go
··· 130 130 } 131 131 } 132 132 133 + // IsSet reports whether f was provided by the user, which is useful to tell 134 + // `cue` apart from `cue --flag=` when the flag's default value is the zero value. 135 + func (f flagName) IsSet(cmd *Command) bool { 136 + f.ensureAdded(cmd) 137 + found := false 138 + cmd.Flags().Visit(func(pf *pflag.Flag) { 139 + if pf.Name == string(f) { 140 + found = true 141 + } 142 + }) 143 + return found 144 + } 145 + 133 146 func (f flagName) Bool(cmd *Command) bool { 134 147 f.ensureAdded(cmd) 135 148 v, _ := cmd.Flags().GetBool(string(f))
+5
cmd/cue/cmd/login.go
··· 91 91 switch tokenStr := flagToken.String(cmd); { 92 92 case tokenStr == "": 93 93 // By default, we perform the OAuth 2.0 device flow to obtain a new token. 94 + // Note that we refuse to continue if the user set --token="", 95 + // because that can be an easy mistake to make via --token=${UNSET_VAR}. 96 + if flagToken.IsSet(cmd) { 97 + return fmt.Errorf("the --token flag needs a non-empty string") 98 + } 94 99 95 100 ctx := backgroundContext() 96 101 // Cause the oauth2 logic to log HTTP requests when logging is enabled.
+4
cmd/cue/cmd/testdata/script/login_token.txtar
··· 7 7 ! exec cue login --token=unrecognized_token_format_1234 8 8 stderr -count=1 'unknown token format, expected an appv1_ prefix' 9 9 10 + # An empty token is rejected, to prevent issues like --token=${UNSET_VAR}. 11 + ! exec cue login --token= 12 + stderr -count=1 'the --token flag needs a non-empty string' 13 + 10 14 # Ensure that only one token is stored when starting from an empty logins.json file. 11 15 exec cue login --token=appv1_validtoken1234 12 16 grep -count=1 '"registries": {' cueconfig/logins.json