parseContextInvocation accepts the {cmd, args} envelope, a flat object whose shape implies the subcommand, or ` --flag value` argv.
(args []string)
| 413 | // parseContextInvocation accepts the {cmd, args} envelope, a flat object whose |
| 414 | // shape implies the subcommand, or `<sub> --flag value` argv. |
| 415 | func parseContextInvocation(args []string) (string, map[string]json.RawMessage, error) { |
| 416 | payload := strings.TrimSpace(strings.Join(args, " ")) |
| 417 | if strings.HasPrefix(payload, "{") { |
| 418 | var top map[string]json.RawMessage |
| 419 | if err := json.Unmarshal([]byte(payload), &top); err != nil { |
| 420 | return "", nil, fmt.Errorf("malformed JSON args: %w", err) |
| 421 | } |
| 422 | cmd := canonicalContextCmd(rawJSONString(top, "cmd", "command", "action")) |
| 423 | inner := top |
| 424 | if rargs, ok := top["args"]; ok && len(rargs) > 0 { |
| 425 | var im map[string]json.RawMessage |
| 426 | if err := json.Unmarshal(rargs, &im); err == nil { |
| 427 | inner = im |
| 428 | } |
| 429 | } |
| 430 | if cmd == "" { |
| 431 | cmd = inferContextCmd(inner) |
| 432 | } |
| 433 | if cmd == "" { |
| 434 | return "", nil, fmt.Errorf("missing or unknown cmd (valid: create|update|attach|detach|list|show|inspect|merge|status|export|import|delete|metrics)") |
| 435 | } |
| 436 | return cmd, inner, nil |
| 437 | } |
| 438 | |
| 439 | // argv form: first token is the subcommand. |
| 440 | if len(args) == 0 { |
| 441 | return "", nil, fmt.Errorf("no subcommand") |
| 442 | } |
| 443 | cmd := canonicalContextCmd(strings.TrimSpace(args[0])) |
| 444 | if cmd == "" { |
| 445 | return "", nil, fmt.Errorf("unknown subcommand %q", args[0]) |
| 446 | } |
| 447 | return cmd, contextArgvToMap(args[1:]), nil |
| 448 | } |
| 449 | |
| 450 | // canonicalContextCmd normalizes subcommand aliases. |
| 451 | func canonicalContextCmd(s string) string { |