mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 02:27:26 +01:00
sw-block: the --json mode is read from the arguments as a whole
A flag bound to the same variable let a partial parse overwrite the mode: --json --bogus --json=false answered in JSON and the reverse did not. The flag is now declared for parsing and help only; the mode is the arguments' last --json, read before parsing. Both orders are pinned, with no RPC. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
abf5b6a6b1
commit
ca588f0045
4 files changed
+43
-9
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user