Skip to content

Commit a26a634

Browse files
committed
feat(cli): add JSON/YAML output format to provider list command
Closes NVIDIA#1745 Add -o/--output flag support (json, yaml, table) to openshell provider list by migrating it to use the existing generic output formatter from NVIDIA#1750. This follows the established pattern used by sandbox list, gateway list, and provider list-profiles commands. Changes: - crates/openshell-cli/src/run.rs: Add provider_to_json() helper that maps Provider proto fields to JSON, including only credential keys (not values) for security. Add output parameter to provider_list() and insert early-return for structured formats. - crates/openshell-cli/src/main.rs: Add output field to ProviderCommands::List with conflicts_with constraints between names and output flags. Update invocation to pass output parameter. - crates/openshell-cli/tests/provider_commands_integration.rs: Update provider_list() call to include output parameter. Security: The provider_to_json() helper only exposes credential keys, never credential values, preventing CWE-532 (insertion of sensitive information into output).
1 parent ec197a4 commit a26a634

3 files changed

Lines changed: 68 additions & 3 deletions

File tree

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,8 +777,12 @@ enum ProviderCommands {
777777
offset: u32,
778778

779779
/// Print only provider names, one per line.
780-
#[arg(long)]
780+
#[arg(long, conflicts_with = "output")]
781781
names: bool,
782+
783+
/// Output format.
784+
#[arg(short = 'o', long = "output", value_enum, default_value_t = OutputFormat::Table, conflicts_with = "names")]
785+
output: OutputFormat,
782786
},
783787

784788
/// List available provider profiles.
@@ -2902,8 +2906,10 @@ async fn main() -> Result<()> {
29022906
limit,
29032907
offset,
29042908
names,
2909+
output,
29052910
} => {
2906-
run::provider_list(endpoint, limit, offset, names, &tls).await?;
2911+
run::provider_list(endpoint, limit, offset, names, output.as_str(), &tls)
2912+
.await?;
29072913
}
29082914
ProviderCommands::ListProfiles { output } => {
29092915
run::provider_list_profiles(endpoint, output.as_str(), &tls).await?;

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4778,11 +4778,65 @@ pub async fn provider_get(server: &str, name: &str, tls: &TlsOptions) -> Result<
47784778
Ok(())
47794779
}
47804780

4781+
fn provider_to_json(provider: &Provider) -> serde_json::Value {
4782+
let mut obj = serde_json::Map::new();
4783+
4784+
// Core fields
4785+
obj.insert("id".to_string(), serde_json::json!(provider.object_id()));
4786+
obj.insert(
4787+
"name".to_string(),
4788+
serde_json::json!(provider.object_name()),
4789+
);
4790+
obj.insert("type".to_string(), serde_json::json!(provider.r#type));
4791+
4792+
// Credential keys (NEVER values - security)
4793+
let credential_keys: Vec<String> = provider.credentials.keys().cloned().collect();
4794+
obj.insert(
4795+
"credential_keys".to_string(),
4796+
serde_json::json!(credential_keys),
4797+
);
4798+
4799+
// Config (non-secret configuration)
4800+
if !provider.config.is_empty() {
4801+
obj.insert("config".to_string(), serde_json::json!(provider.config));
4802+
}
4803+
4804+
// Metadata fields (only if metadata exists)
4805+
if let Some(meta) = &provider.metadata {
4806+
if !meta.labels.is_empty() {
4807+
obj.insert("labels".to_string(), serde_json::json!(meta.labels));
4808+
}
4809+
if meta.resource_version != 0 {
4810+
obj.insert(
4811+
"resource_version".to_string(),
4812+
serde_json::json!(meta.resource_version),
4813+
);
4814+
}
4815+
if meta.created_at_ms != 0 {
4816+
obj.insert(
4817+
"created_at_ms".to_string(),
4818+
serde_json::json!(meta.created_at_ms),
4819+
);
4820+
}
4821+
}
4822+
4823+
// Credential expiration times (only if present)
4824+
if !provider.credential_expires_at_ms.is_empty() {
4825+
obj.insert(
4826+
"credential_expires_at_ms".to_string(),
4827+
serde_json::json!(provider.credential_expires_at_ms),
4828+
);
4829+
}
4830+
4831+
serde_json::Value::Object(obj)
4832+
}
4833+
47814834
pub async fn provider_list(
47824835
server: &str,
47834836
limit: u32,
47844837
offset: u32,
47854838
names_only: bool,
4839+
output: &str,
47864840
tls: &TlsOptions,
47874841
) -> Result<()> {
47884842
let mut client = grpc_client(server, tls).await?;
@@ -4792,6 +4846,11 @@ pub async fn provider_list(
47924846
.into_diagnostic()?;
47934847
let providers = response.into_inner().providers;
47944848

4849+
// Handle structured output formats (json, yaml)
4850+
if crate::output::print_output_collection(output, &providers, provider_to_json)? {
4851+
return Ok(());
4852+
}
4853+
47954854
if providers.is_empty() {
47964855
if !names_only {
47974856
println!("No providers found.");

‎crates/openshell-cli/tests/provider_commands_integration.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ async fn provider_cli_run_functions_support_full_crud_flow() {
997997
run::provider_get(&ts.endpoint, "my-claude", &ts.tls)
998998
.await
999999
.expect("provider get");
1000-
run::provider_list(&ts.endpoint, 100, 0, false, &ts.tls)
1000+
run::provider_list(&ts.endpoint, 100, 0, false, "table", &ts.tls)
10011001
.await
10021002
.expect("provider list");
10031003

0 commit comments

Comments
 (0)