Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 45 additions & 4 deletions crates/openshell-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2484,6 +2484,10 @@ enum WorkspaceCommands {
/// Labels to attach to the workspace (KEY=VALUE).
#[arg(long = "label", value_name = "KEY=VALUE")]
labels: Vec<String>,

/// Output format.
#[arg(short = 'o', long = "output", value_enum, default_value_t = OutputFormat::Table)]
output: OutputFormat,
},

/// Fetch a workspace by name.
Expand All @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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([
Expand Down
11 changes: 10 additions & 1 deletion crates/openshell-cli/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand All @@ -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?;
Expand All @@ -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());
Expand Down
6 changes: 4 additions & 2 deletions docs/how-it-works/workspaces.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
Loading