diff --git a/crates/openshell-cli/src/main.rs b/crates/openshell-cli/src/main.rs index 5f20fc7f3c..908c066b22 100644 --- a/crates/openshell-cli/src/main.rs +++ b/crates/openshell-cli/src/main.rs @@ -2484,6 +2484,10 @@ enum WorkspaceCommands { /// Labels to attach to the workspace (KEY=VALUE). #[arg(long = "label", value_name = "KEY=VALUE")] labels: Vec, + + /// Output format. + #[arg(short = 'o', long = "output", value_enum, default_value_t = OutputFormat::Table)] + output: OutputFormat, }, /// Fetch a workspace by name. @@ -2492,6 +2496,10 @@ enum WorkspaceCommands { /// Workspace name. #[arg(add = ArgValueCompleter::new(completers::complete_workspace_names))] name: String, + + /// Output format. + #[arg(short = 'o', long = "output", value_enum, default_value_t = OutputFormat::Table)] + output: OutputFormat, }, /// List workspaces. @@ -3830,11 +3838,15 @@ async fn run_async() -> Result<()> { apply_auth(&mut tls, &ctx.name)?; match command { - WorkspaceCommands::Create { name, labels } => { - run::workspace_create(endpoint, &name, &labels, &tls).await?; + WorkspaceCommands::Create { + name, + labels, + output, + } => { + run::workspace_create(endpoint, &name, &labels, output.as_str(), &tls).await?; } - WorkspaceCommands::Get { name } => { - run::workspace_get(endpoint, &name, &tls).await?; + WorkspaceCommands::Get { name, output } => { + run::workspace_get(endpoint, &name, output.as_str(), &tls).await?; } WorkspaceCommands::List { page_size, @@ -6640,6 +6652,35 @@ mod tests { } } + #[test] + fn workspace_create_and_get_accept_output_format() { + let cli = Cli::try_parse_from([ + "openshell", + "workspace", + "create", + "--name", + "team-ml", + "--output", + "json", + ]) + .expect("workspace create --output should parse"); + match cli.command { + Some(Commands::Workspace { + command: Some(WorkspaceCommands::Create { output, .. }), + }) => assert_eq!(output.as_str(), "json"), + other => panic!("expected workspace create, got {other:?}"), + } + + let cli = Cli::try_parse_from(["openshell", "workspace", "get", "team-ml", "-o", "yaml"]) + .expect("workspace get -o should parse"); + match cli.command { + Some(Commands::Workspace { + command: Some(WorkspaceCommands::Get { output, .. }), + }) => assert_eq!(output.as_str(), "yaml"), + other => panic!("expected workspace get, got {other:?}"), + } + } + #[test] fn sandbox_template_list_names_conflicts_with_output() { let result = Cli::try_parse_from([ diff --git a/crates/openshell-cli/src/run.rs b/crates/openshell-cli/src/run.rs index 807a79cb72..62175a15f1 100644 --- a/crates/openshell-cli/src/run.rs +++ b/crates/openshell-cli/src/run.rs @@ -4515,6 +4515,7 @@ pub async fn workspace_create( server: &str, name: &str, label_args: &[String], + output: &str, tls: &TlsOptions, ) -> Result<()> { use openshell_core::proto::CreateWorkspaceRequest; @@ -4542,6 +4543,10 @@ pub async fn workspace_create( .workspace .ok_or_else(|| miette!("workspace missing from response"))?; + if crate::output::print_output_single(output, &workspace, workspace_to_json)? { + return Ok(()); + } + println!( "{} Created workspace {}", "✓".green().bold(), @@ -4551,7 +4556,7 @@ pub async fn workspace_create( Ok(()) } -pub async fn workspace_get(server: &str, name: &str, tls: &TlsOptions) -> Result<()> { +pub async fn workspace_get(server: &str, name: &str, output: &str, tls: &TlsOptions) -> Result<()> { use openshell_core::proto::GetWorkspaceRequest; let mut client = grpc_client(server, tls).await?; @@ -4567,6 +4572,10 @@ pub async fn workspace_get(server: &str, name: &str, tls: &TlsOptions) -> Result .workspace .ok_or_else(|| miette!("workspace missing from response"))?; + if crate::output::print_output_single(output, &workspace, workspace_to_json)? { + return Ok(()); + } + println!("{}", "Workspace:".cyan().bold()); println!(); println!(" {} {}", "Name:".dimmed(), workspace.object_name()); diff --git a/docs/how-it-works/workspaces.mdx b/docs/how-it-works/workspaces.mdx index d39a35c962..7774ca1064 100644 --- a/docs/how-it-works/workspaces.mdx +++ b/docs/how-it-works/workspaces.mdx @@ -149,11 +149,13 @@ Filtering only returns workspaces you are authorized to view. openshell workspace list --label-selector env=dev,team=ml ``` -Inspect a workspace to see its labels, or use structured list output for -automation. +Inspect a workspace to see its labels. Pass `--output json` or `--output yaml` +to `workspace create`, `workspace get`, or `workspace list` for structured +output in automation. ```shell openshell workspace get team-research +openshell workspace get team-research --output json openshell workspace list --label-selector env=dev --output json ```