Skip to content

Commit d287542

Browse files
committed
feat(cli): add --output json/yaml to sandbox get, status, and sandbox create
Add structured output support to three commands that previously only supported human-readable table output: - `sandbox get --output json/yaml`: emits sandbox detail including policy source, revision, and active policy content via a new `sandbox_detail_to_json` converter. - `status --output json/yaml`: emits gateway connection state via a new `status_to_json` converter with conditional fields (auth, version, error, http_status) matching the human output. - `sandbox create --output json/yaml`: emits sandbox metadata after the sandbox reaches Ready phase, suppressing spinners and ANSI chrome from stdout. Uploads and port forwarding still execute before output. Clap rejects `--output` combined with `--editor` or trailing commands. All three commands reuse the existing `OutputFormat` enum and `print_output_single` helper. Default output (no --output flag) is byte-identical to current behavior. Unit tests cover both converters. Closes NVIDIA#1964 Assisted-By: 🤖 Claude Code Signed-off-by: Roland Huß <[email protected]>
1 parent 7bce122 commit d287542

6 files changed

Lines changed: 382 additions & 67 deletions

File tree

‎crates/openshell-cli/src/main.rs‎

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,11 @@ enum Commands {
533533

534534
/// Show gateway status and information.
535535
#[command(help_template = LEAF_HELP_TEMPLATE, next_help_heading = "FLAGS")]
536-
Status,
536+
Status {
537+
/// Output format.
538+
#[arg(short = 'o', long = "output", value_enum, default_value_t = OutputFormat::Table)]
539+
output: OutputFormat,
540+
},
537541

538542
/// Manage inference configuration.
539543
#[command(after_help = INFERENCE_EXAMPLES, help_template = SUBCOMMAND_HELP_TEMPLATE)]
@@ -1336,6 +1340,10 @@ enum SandboxCommands {
13361340
#[arg(long, value_parser = ["manual", "auto"], default_value = "manual")]
13371341
approval_mode: String,
13381342

1343+
/// Output format.
1344+
#[arg(short = 'o', long = "output", value_enum, default_value_t = OutputFormat::Table, conflicts_with_all = ["editor", "command", "no_keep"])]
1345+
output: OutputFormat,
1346+
13391347
/// Command to run after "--" (defaults to an interactive shell).
13401348
#[arg(last = true, allow_hyphen_values = true)]
13411349
command: Vec<String>,
@@ -1349,8 +1357,12 @@ enum SandboxCommands {
13491357
name: Option<String>,
13501358

13511359
/// Print only the active policy YAML (same policy as the default view; stdout only).
1352-
#[arg(long)]
1360+
#[arg(long, conflicts_with = "output")]
13531361
policy_only: bool,
1362+
1363+
/// Output format.
1364+
#[arg(short = 'o', long = "output", value_enum, default_value_t = OutputFormat::Table)]
1365+
output: OutputFormat,
13541366
},
13551367

13561368
/// List sandboxes.
@@ -2083,11 +2095,17 @@ async fn main() -> Result<()> {
20832095
// -----------------------------------------------------------
20842096
// Top-level status
20852097
// -----------------------------------------------------------
2086-
Some(Commands::Status) => {
2098+
Some(Commands::Status { output }) => {
20872099
if let Ok(ctx) = resolve_gateway(&cli.gateway, &cli.gateway_endpoint) {
20882100
let mut tls = tls.with_gateway_name(&ctx.name);
20892101
apply_auth(&mut tls, &ctx.name);
2090-
run::gateway_status(&ctx.name, &ctx.endpoint, &tls).await?;
2102+
run::gateway_status(&ctx.name, &ctx.endpoint, output.as_str(), &tls).await?;
2103+
} else if openshell_cli::output::print_output_single(
2104+
output.as_str(),
2105+
&(),
2106+
|()| serde_json::json!({"status": "not_configured"}),
2107+
)? {
2108+
// Structured output handled.
20912109
} else {
20922110
println!("{}", "Gateway Status".cyan().bold());
20932111
println!();
@@ -2618,6 +2636,7 @@ async fn main() -> Result<()> {
26182636
labels,
26192637
envs,
26202638
approval_mode,
2639+
output,
26212640
command,
26222641
} => {
26232642
// Resolve --tty / --no-tty into an Option<bool> override.
@@ -2704,6 +2723,7 @@ async fn main() -> Result<()> {
27042723
labels: labels_map,
27052724
environment: env_map,
27062725
approval_mode: &approval_mode,
2726+
output: output.as_str(),
27072727
},
27082728
&tls,
27092729
))
@@ -2777,9 +2797,14 @@ async fn main() -> Result<()> {
27772797
| SandboxCommands::Download { .. } => {
27782798
unreachable!()
27792799
}
2780-
SandboxCommands::Get { name, policy_only } => {
2800+
SandboxCommands::Get {
2801+
name,
2802+
policy_only,
2803+
output,
2804+
} => {
27812805
let name = resolve_sandbox_name(name, &ctx.name)?;
2782-
run::sandbox_get(endpoint, &name, policy_only, &tls).await?;
2806+
run::sandbox_get(endpoint, &name, policy_only, output.as_str(), &tls)
2807+
.await?;
27832808
}
27842809
SandboxCommands::List {
27852810
limit,
@@ -3463,7 +3488,7 @@ mod tests {
34633488
.expect("global gateway flag should parse with subcommands");
34643489

34653490
assert_eq!(cli.gateway.as_deref(), Some("demo"));
3466-
assert!(matches!(cli.command, Some(Commands::Status)));
3491+
assert!(matches!(cli.command, Some(Commands::Status { .. })));
34673492
}
34683493

34693494
#[test]

0 commit comments

Comments
 (0)