Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(cli): add -o json/yaml output format to sandbox list#1422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -3045,13 +3045,15 @@ fn print_sandbox_policy(policy: &SandboxPolicy) { | ||
| } | ||
| /// List sandboxes. | ||
| #[allow(clippy::too_many_arguments)] | ||
| pub async fn sandbox_list( | ||
| server: &str, | ||
| limit: u32, | ||
| offset: u32, | ||
| ids_only: bool, | ||
| names_only: bool, | ||
| label_selector: Option<&str>, | ||
| output: &str, | ||
| tls: &TlsOptions, | ||
| ) -> Result<()> { | ||
| let mut client = grpc_client(server, tls).await?; | ||
| @@ -3066,6 +3068,25 @@ pub async fn sandbox_list( | ||
| .into_diagnostic()?; | ||
| let sandboxes = response.into_inner().sandboxes; | ||
| match output { | ||
| "json" => { | ||
| let items: Vec<serde_json::Value> = sandboxes.iter().map(sandbox_to_json).collect(); | ||
| println!( | ||
| "{}", | ||
| serde_json::to_string_pretty(&items).into_diagnostic()? | ||
| ); | ||
| return Ok(()); | ||
| } | ||
| "yaml" => { | ||
| let items: Vec<serde_json::Value> = sandboxes.iter().map(sandbox_to_json).collect(); | ||
| print!("{}", serde_yml::to_string(&items).into_diagnostic()?); | ||
| return Ok(()); | ||
| } | ||
| "table" => {} | ||
| _ => return Err(miette!("unsupported output format: {output}")), | ||
| } | ||
| if sandboxes.is_empty() { | ||
| if !ids_only && !names_only { | ||
| println!("No sandboxes found."); | ||
| @@ -3126,6 +3147,19 @@ pub async fn sandbox_list( | ||
| Ok(()) | ||
| } | ||
| fn sandbox_to_json(sandbox: &Sandbox) -> serde_json::Value { | ||
| let meta = sandbox.metadata.as_ref(); | ||
| let labels = meta.map_or_else(|| serde_json::json!({}), |m| serde_json::json!(m.labels)); | ||
| serde_json::json!({ | ||
Comment on lines
+3150
to
+3153
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would have checked to see if this can be done via | ||
| "id": sandbox.object_id(), | ||
| "name": sandbox.object_name(), | ||
| "labels": labels, | ||
| "created_at": format_epoch_ms(meta.map_or(0, |m| m.created_at_ms)), | ||
| "phase": phase_name(sandbox.phase), | ||
| "current_policy_version": sandbox.current_policy_version, | ||
| }) | ||
| } | ||
| pub async fn sandbox_provider_list(server: &str, name: &str, tls: &TlsOptions) -> Result<()> { | ||
| let mut client = grpc_client(server, tls).await?; | ||
| let response = client | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would avoid YAML in new projects in general, but also specifically in Rust since serde-yaml has gone through some maintenance issues yaml/yaml-serde#2
Especially for this use case, there's no reason not to just use JSON which is about machine readability.