Skip to content

Commit 5814483

Browse files
alexng353claude
andcommitted
feat: add --json output to list/get commands
Makes machine-consumable JSON output available on the commands most likely to be piped into scripts or agents: - envx list-projects: new --json flag - envx list-keys (get keys): new --json flag - envx get project: wires the already-declared --json flag to actually emit JSON (it was defined but unused) - envx get variable: new --json flag emitting {\"key\", \"value\"} envx set --json was added in the previous destructive-commands commit. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 3b17286 commit 5814483

4 files changed

Lines changed: 50 additions & 5 deletions

File tree

src/commands/get/keys.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,18 @@ pub struct Args {
66
/// Use full length fingerprints
77
#[arg(short, long)]
88
full: bool,
9+
10+
/// Output as JSON
11+
#[arg(long)]
12+
json: bool,
913
}
1014

1115
pub async fn command(_args: Args, config: &mut Config) -> Result<()> {
16+
if _args.json {
17+
println!("{}", serde_json::to_string(&config.keys)?);
18+
return Ok(());
19+
}
20+
1221
println!("Keys:");
1322
for key in config.keys.iter() {
1423
let fingerprint = match _args.full {

src/commands/get/project.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::*;
22
use crate::utils::config::Config;
33
use crate::{sdk::SDK, utils::choice::Choice};
44

5-
/// Get all environment variables for a project
5+
/// Get project info
66
#[derive(Parser)]
77
pub struct Args {
88
/// Partial fingerprint of key to use
@@ -18,12 +18,24 @@ pub struct Args {
1818
json: bool,
1919
}
2020

21-
// TODO: Pretty print project info (in a table?)
2221
pub async fn command(args: Args, config: &mut Config) -> Result<()> {
2322
let key = config.primary_key()?;
2423
let key = key.unlock(&config.primary_key_password()?);
2524
let project_id = Choice::try_project(args.project_id, &key).await?;
2625
let project_info = SDK::get_project_info(&project_id, &key).await?;
27-
println!("{:?}", project_info);
26+
27+
if args.json {
28+
println!("{}", serde_json::to_string(&project_info)?);
29+
return Ok(());
30+
}
31+
32+
println!("Project Info:\n");
33+
println!("ID: {}", project_info.project_id);
34+
println!("Name: {}", project_info.project_name);
35+
println!("Users:");
36+
for user in project_info.users {
37+
println!(" {} - {}", user.id, user.username);
38+
}
39+
2840
Ok(())
2941
}

src/commands/get/variable.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ pub struct Args {
1313

1414
#[arg(short, long)]
1515
key: String,
16+
17+
/// Output the key/value pair as JSON
18+
#[arg(long)]
19+
json: bool,
1620
}
1721

1822
pub async fn command(args: Args, config: &mut Config) -> Result<()> {
@@ -26,6 +30,17 @@ pub async fn command(args: Args, config: &mut Config) -> Result<()> {
2630

2731
match kvpair {
2832
Some(kvpair) => {
33+
if args.json {
34+
println!(
35+
"{}",
36+
serde_json::json!({
37+
"key": kvpair.key,
38+
"value": kvpair.value,
39+
})
40+
);
41+
return Ok(());
42+
}
43+
2944
let mut stdout = std::io::stdout();
3045
stdout.write_all(kvpair.key.as_bytes())?;
3146
if stdout.is_terminal() {

src/commands/list_projects.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,25 @@ use super::*;
22
use crate::utils::config::Config;
33

44
#[derive(Parser)]
5-
pub struct Args {}
5+
pub struct Args {
6+
/// Output as JSON
7+
#[arg(long)]
8+
json: bool,
9+
}
610

7-
pub async fn command(_args: Args, config: &mut Config) -> Result<()> {
11+
pub async fn command(args: Args, config: &mut Config) -> Result<()> {
812
let key = config.primary_key()?;
913
let password = config.primary_key_password()?;
1014
let key = key.unlock(&password);
1115
let sdk_config = config.sdk_configuration(&key)?;
1216
let projects =
1317
envx_sdk::apis::projects_api::list_projects_v2(&sdk_config).await?;
1418

19+
if args.json {
20+
println!("{}", serde_json::to_string(&projects)?);
21+
return Ok(());
22+
}
23+
1524
println!("Projects:");
1625
for project in projects {
1726
println!("\t{} - {}", project.project_id, project.project_name);

0 commit comments

Comments
 (0)