diff --git a/seaweed-block/cmd/sw-block/cluster_format.go b/seaweed-block/cmd/sw-block/cluster_format.go index 6f937676ef..fcbb74b547 100644 --- a/seaweed-block/cmd/sw-block/cluster_format.go +++ b/seaweed-block/cmd/sw-block/cluster_format.go @@ -80,10 +80,12 @@ func runClusterFinalizeFormat(args []string, stdout, stderr io.Writer) int { fs.SetOutput(stderr) master := fs.String("master", "", "blockmaster gRPC address (required)") timeout := fs.Duration("timeout", 30*time.Second, "how long to wait for the master's answer") - jsonOut := fs.Bool("json", false, "print the typed answer as JSON: result, reason, hosts, format_level, message") - // The mode is read before parsing, so a bad option in any position still - // answers in JSON when --json was asked for. - *jsonOut = jsonRequested(args) + // The flag is declared for parsing and help only: the mode comes from the + // arguments as a whole, read before parsing, so a bad option in any + // position cannot leave it half-parsed. + fs.Bool("json", false, "print the typed answer as JSON: result, reason, hosts, format_level, message") + jsonMode := jsonRequested(args) + jsonOut := &jsonMode answer := finalizeAnswer{Hosts: []finalizeHost{}} defer func() { if *jsonOut { diff --git a/seaweed-block/cmd/sw-block/cluster_format_json_test.go b/seaweed-block/cmd/sw-block/cluster_format_json_test.go index 1f51139ce2..a3ae03f470 100644 --- a/seaweed-block/cmd/sw-block/cluster_format_json_test.go +++ b/seaweed-block/cmd/sw-block/cluster_format_json_test.go @@ -120,8 +120,25 @@ func TestClusterFinalizeFormatBadOptionAnswersJSONWithoutAnRPC(t *testing.T) { if calls != 0 { t.Fatalf("a malformed command sent %d finalize RPCs", calls) } - var out, errOut bytes.Buffer - if code := run([]string{"cluster", "finalize-format", "--bogus", "--json", "--json=false"}, &out, &errOut); code != 1 || out.Len() != 0 { - t.Fatalf("--json=false last: exit %d, stdout %q; want no JSON", code, out.String()) + // The mode is the arguments' last --json, whatever a partial parse saw. + for _, tc := range []struct { + args []string + json bool + }{ + {[]string{"cluster", "finalize-format", "--bogus", "--json", "--json=false"}, false}, + {[]string{"cluster", "finalize-format", "--json", "--bogus", "--json=false"}, false}, + {[]string{"cluster", "finalize-format", "--json=false", "--bogus", "--json"}, true}, + {[]string{"cluster", "finalize-format", "--json=false", "--json", "--bogus"}, true}, + } { + var out, errOut bytes.Buffer + code := run(tc.args, &out, &errOut) + var a finalizeAnswer + gotJSON := json.Unmarshal(out.Bytes(), &a) == nil && a.Result == "failed" + if code != 1 || gotJSON != tc.json || (!tc.json && out.Len() != 0) { + t.Fatalf("%v: exit %d, stdout %q; want JSON %v", tc.args, code, out.String(), tc.json) + } + } + if calls != 0 { + t.Fatalf("a malformed command sent %d finalize RPCs", calls) } } diff --git a/seaweed-block/cmd/sw-block/workspace_clean.go b/seaweed-block/cmd/sw-block/workspace_clean.go index ba69e17e8c..03e8f09569 100644 --- a/seaweed-block/cmd/sw-block/workspace_clean.go +++ b/seaweed-block/cmd/sw-block/workspace_clean.go @@ -39,8 +39,10 @@ func runWorkspaceCleanMove(args []string, stdout, stderr io.Writer) int { fs.StringVar(&q.TargetCtrlAddr, "target-ctrl", "", "target control address host:port") fs.BoolVar(&clean, "clean", false, "required: move using the detached HEAD and matching recorded host ACK") fs.DurationVar(&timeout, "timeout", wsDefaultTimeout, "bound for the RPC") - fs.BoolVar(&jsonOutput, "json", false, "print the typed answer: issued, refusal code and reason, issued move") - jsonOutput = jsonRequested(args) // a bad option before --json still answers in JSON + // Declared for parsing and help only; the mode is read from the arguments + // as a whole, so a bad option in any position cannot half-parse it. + fs.Bool("json", false, "print the typed answer: issued, refusal code and reason, issued move") + jsonOutput = jsonRequested(args) if err := fs.Parse(args); err != nil { return wsExitRefusedLocally } diff --git a/seaweed-block/cmd/sw-block/workspace_clean_test.go b/seaweed-block/cmd/sw-block/workspace_clean_test.go index 3f78758bfd..faa8c15ea8 100644 --- a/seaweed-block/cmd/sw-block/workspace_clean_test.go +++ b/seaweed-block/cmd/sw-block/workspace_clean_test.go @@ -135,6 +135,19 @@ func TestWorkspaceCleanMoveBadOptionAnswersJSONWithoutAnRPC(t *testing.T) { if code != wsExitRefusedLocally || json.Unmarshal([]byte(out), &a) != nil || a.Issued || a.Code != "InvalidArgument" || *dials != 0 || f.total() != 0 { t.Fatalf("exit=%d out=%q answer=%+v dials=%d calls=%d", code, out, a, *dials, f.total()) } + // The mode is the arguments' last --json, whatever a partial parse saw. + for _, tc := range []struct { + extra []string + json bool + }{ + {[]string{"--json", "--bogus", "--json=false"}, false}, + {[]string{"--json=false", "--bogus", "--json"}, true}, + } { + code, out, _ := runCLI(append([]string{"ops", "workspace", "move"}, append(tc.extra, cleanMoveArgs(addr)[3:]...)...)) + if code != wsExitRefusedLocally || (out != "") != tc.json || *dials != 0 || f.total() != 0 { + t.Fatalf("%v: exit=%d out=%q want JSON %v; dials=%d calls=%d", tc.extra, code, out, tc.json, *dials, f.total()) + } + } } // force-move --json carries the master's typed reason from the refusal's