GeezerVison is showing github.com — fix(cli): recognize --flag=value form in startup arg pre-scan · ucloud/ucloud-cli@dd6febd
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: cmd/root.go +60-30Lines changed: 60 additions & 30 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -308,37 +308,39 @@ func init() { | 308 | 308 |
| 309 | 309 | //-1表示不覆盖配置文件中的MaxRetryTimes参数 | 310 | 310 | global.MaxRetryTimes = -1 | 311 | | - for idx, arg := range os.Args { | 312 | | - if arg == "--profile" && len(os.Args) > idx+1 && os.Args[idx+1] != "" { | 313 | | - global.Profile = os.Args[idx+1] | 314 | | - } | 315 | | - if arg == "--public-key" && len(os.Args) > idx+1 && os.Args[idx+1] != "" { | 316 | | - global.PublicKey = os.Args[idx+1] | 317 | | - } | 318 | | - if arg == "--private-key" && len(os.Args) > idx+1 && os.Args[idx+1] != "" { | 319 | | - global.PrivateKey = os.Args[idx+1] | 320 | | - } | 321 | | - if arg == "--base-url" && len(os.Args) > idx+1 && os.Args[idx+1] != "" { | 322 | | - global.BaseURL = os.Args[idx+1] | 323 | | - } | 324 | | - if arg == "--channel-key" && len(os.Args) > idx+1 && os.Args[idx+1] != "" { | 325 | | - global.ChannelKey = os.Args[idx+1] | 326 | | - } | 327 | | - if arg == "--timeout-sec" && len(os.Args) > idx+1 && os.Args[idx+1] != "" { | 328 | | - sec, err := strconv.Atoi(os.Args[idx+1]) | 329 | | - if err != nil { | 330 | | - fmt.Printf("parse timeout-sec failed: %v\n", err) | 331 | | - } else { | 332 | | - global.Timeout = sec | 333 | | - } | | 311 | + // 启动期预扫描:在 cobra 解析前把连接类参数落到 global.*,供 InitClientRuntime 用。 | | 312 | + // 每个 flag 都识别 `--flag value` 与 `--flag=value` 两形式(scanFlagValue)。 | | 313 | + // --profile 额外识别短选项 -p:漏识别会让 ConfigIns 指向错误 profile, | | 314 | + // OAuth 刷新可能把别人的 Bearer 重放到当前请求(见 platform/client.go 注释)。 | | 315 | + if v, ok := scanFlagValue(os.Args, "--profile", "-p"); ok { | | 316 | + global.Profile = v | | 317 | + } | | 318 | + if v, ok := scanFlagValue(os.Args, "--public-key"); ok { | | 319 | + global.PublicKey = v | | 320 | + } | | 321 | + if v, ok := scanFlagValue(os.Args, "--private-key"); ok { | | 322 | + global.PrivateKey = v | | 323 | + } | | 324 | + if v, ok := scanFlagValue(os.Args, "--base-url"); ok { | | 325 | + global.BaseURL = v | | 326 | + } | | 327 | + if v, ok := scanFlagValue(os.Args, "--channel-key"); ok { | | 328 | + global.ChannelKey = v | | 329 | + } | | 330 | + if v, ok := scanFlagValue(os.Args, "--timeout-sec"); ok { | | 331 | + sec, err := strconv.Atoi(v) | | 332 | + if err != nil { | | 333 | + fmt.Printf("parse timeout-sec failed: %v\n", err) | | 334 | + } else { | | 335 | + global.Timeout = sec | 334 | 336 | } | 335 | | - if arg == "--max-retry-times" && len(os.Args) > idx+1 && os.Args[idx+1] != "" { | 336 | | - times, err := strconv.Atoi(os.Args[idx+1]) | 337 | | - if err != nil { | 338 | | - fmt.Printf("parse max-retry-times failed: %v\n", err) | 339 | | - } else { | 340 | | - global.MaxRetryTimes = times | 341 | | - } | | 337 | + } | | 338 | + if v, ok := scanFlagValue(os.Args, "--max-retry-times"); ok { | | 339 | + times, err := strconv.Atoi(v) | | 340 | + if err != nil { | | 341 | + fmt.Printf("parse max-retry-times failed: %v\n", err) | | 342 | + } else { | | 343 | + global.MaxRetryTimes = times | 342 | 344 | } | 343 | 345 | } | 344 | 346 | if sec, found, err := parseWaitTimeoutSec(os.Args); found { | @@ -351,6 +353,34 @@ func init() { | 351 | 353 | cobra.EnableCommandSorting = false | 352 | 354 | } | 353 | 355 |
| | 356 | +// scanFlagValue finds the value of any of names in args, recognizing both the | | 357 | +// space form (`--profile foo`) and the equals form (`--profile=foo`). Multiple | | 358 | +// names allow aliases, e.g. scanFlagValue(args, "--profile", "-p"). The first | | 359 | +// (leftmost) hit wins; an empty value (`--profile=` or a trailing `--profile` | | 360 | +// with no following arg) counts as no hit and scanning continues. | | 361 | +// | | 362 | +// This is the startup pre-scan that must run before cobra parses flags, because | | 363 | +// connection-class params (base-url/channel-key/profile/keys) must be settled | | 364 | +// before InitClientRuntime builds sdk.Config. Exact-comparison-only scans missed | | 365 | +// the equals form — the same #119 bug parseWaitTimeoutSec already fixed for | | 366 | +// --wait-timeout-sec. Attached/combined shorthand (`-pfoo`, `-dpfoo`) is out of | | 367 | +// scope: hand-parsing combined shorthand is error-prone and its residual risk | | 368 | +// matches today's behavior (the pre-scan never recognized `-p` at all). | | 369 | +func scanFlagValue(args []string, names ...string) (string, bool) { | | 370 | + for i, arg := range args { | | 371 | + for _, name := range names { | | 372 | + if arg == name { | | 373 | + if i+1 < len(args) && args[i+1] != "" { | | 374 | + return args[i+1], true | | 375 | + } | | 376 | + } else if v, ok := strings.CutPrefix(arg, name+"="); ok && v != "" { | | 377 | + return v, true | | 378 | + } | | 379 | + } | | 380 | + } | | 381 | + return "", false | | 382 | +} | | 383 | + | 354 | 384 | // parseWaitTimeoutSec scans args for --wait-timeout-sec in either | 355 | 385 | // `--wait-timeout-sec N` or `--wait-timeout-sec=N` form. found=true when | 356 | 386 | // the flag is present with a value; err is set when that value isn't an int. | |
Collapse file Copy file name to clipboardExpand all lines: cmd/root_test.go +99Lines changed: 99 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -81,3 +81,102 @@ func TestParseWaitTimeoutSec(t *testing.T) { | 81 | 81 | }) | 82 | 82 | } | 83 | 83 | } | | 84 | + | | 85 | +// TestScanFlagValue covers the generalized startup pre-scan helper. Same #119 | | 86 | +// bug as parseWaitTimeoutSec (equals form silently ignored), now for the | | 87 | +// connection-class flags. The critical -pfoo case pins the R3 boundary: | | 88 | +// attached/combined shorthand is intentionally NOT recognized. | | 89 | +func TestScanFlagValue(t *testing.T) { | | 90 | + tests := []struct { | | 91 | + name string | | 92 | + args []string | | 93 | + names []string | | 94 | + wantVal string | | 95 | + wantFound bool | | 96 | + }{ | | 97 | + { | | 98 | + name: "long space form", | | 99 | + args: []string{"ucloud", "config", "--profile", "foo"}, | | 100 | + names: []string{"--profile", "-p"}, | | 101 | + wantVal: "foo", | | 102 | + wantFound: true, | | 103 | + }, | | 104 | + { | | 105 | + name: "long equals form", | | 106 | + args: []string{"ucloud", "config", "--profile=foo"}, | | 107 | + names: []string{"--profile", "-p"}, | | 108 | + wantVal: "foo", | | 109 | + wantFound: true, | | 110 | + }, | | 111 | + { | | 112 | + name: "short space form", | | 113 | + args: []string{"ucloud", "config", "-p", "foo"}, | | 114 | + names: []string{"--profile", "-p"}, | | 115 | + wantVal: "foo", | | 116 | + wantFound: true, | | 117 | + }, | | 118 | + { | | 119 | + name: "short equals form", | | 120 | + args: []string{"ucloud", "config", "-p=foo"}, | | 121 | + names: []string{"--profile", "-p"}, | | 122 | + wantVal: "foo", | | 123 | + wantFound: true, | | 124 | + }, | | 125 | + { | | 126 | + // R3 boundary: attached shorthand is out of scope, must NOT match. | | 127 | + name: "attached shorthand not recognized", | | 128 | + args: []string{"ucloud", "config", "-pfoo"}, | | 129 | + names: []string{"--profile", "-p"}, | | 130 | + wantVal: "", | | 131 | + wantFound: false, | | 132 | + }, | | 133 | + { | | 134 | + name: "empty equals is no hit", | | 135 | + args: []string{"ucloud", "config", "--profile="}, | | 136 | + names: []string{"--profile", "-p"}, | | 137 | + wantVal: "", | | 138 | + wantFound: false, | | 139 | + }, | | 140 | + { | | 141 | + name: "trailing flag no value", | | 142 | + args: []string{"ucloud", "config", "--profile"}, | | 143 | + names: []string{"--profile", "-p"}, | | 144 | + wantVal: "", | | 145 | + wantFound: false, | | 146 | + }, | | 147 | + { | | 148 | + name: "absent", | | 149 | + args: []string{"ucloud", "config"}, | | 150 | + names: []string{"--profile", "-p"}, | | 151 | + wantVal: "", | | 152 | + wantFound: false, | | 153 | + }, | | 154 | + { | | 155 | + name: "base-url equals form single name", | | 156 | + args: []string{"ucloud", "uhost", "list", "--base-url=http://x/"}, | | 157 | + names: []string{"--base-url"}, | | 158 | + wantVal: "http://x/", | | 159 | + wantFound: true, | | 160 | + }, | | 161 | + { | | 162 | + // leftmost hit wins (pre-scan only needs one early value, no override) | | 163 | + name: "leftmost wins", | | 164 | + args: []string{"ucloud", "--profile", "a", "--profile", "b"}, | | 165 | + names: []string{"--profile", "-p"}, | | 166 | + wantVal: "a", | | 167 | + wantFound: true, | | 168 | + }, | | 169 | + } | | 170 | + | | 171 | + for _, tt := range tests { | | 172 | + t.Run(tt.name, func(t *testing.T) { | | 173 | + val, found := scanFlagValue(tt.args, tt.names...) | | 174 | + if val != tt.wantVal { | | 175 | + t.Errorf("val = %q, want %q", val, tt.wantVal) | | 176 | + } | | 177 | + if found != tt.wantFound { | | 178 | + t.Errorf("found = %v, want %v", found, tt.wantFound) | | 179 | + } | | 180 | + }) | | 181 | + } | | 182 | +} |
|