From 37baf8dfc5f380ef33de8e0d2fb39bb0d9bc3be0 Mon Sep 17 00:00:00 2001 From: Denys Vuika Date: Tue, 19 Nov 2024 16:03:28 -0500 Subject: [PATCH] support json output --- src/main.rs | 45 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index fabd121..48b5a1f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,8 +43,16 @@ enum Commands { #[derive(Subcommand)] enum ViewCommands { - Files, - Projects, + Files { + /// Output format: json or text + #[arg(long, default_value = "text")] + format: String, + }, + Projects { + /// Output format: json or text + #[arg(long, default_value = "text")] + format: String, + }, Tasks, } @@ -86,15 +94,28 @@ async fn main() -> Result<()> { } Commands::View(subcommand) => match subcommand { - ViewCommands::Files => { + ViewCommands::Files { format } => { workspace.load().await?; let files = workspace.affected_files()?; - for file in files { - println!("{}", file); + if files.is_empty() { + println!("No files affected"); + return Ok(()); + } + + match format.as_str() { + "json" => { + let json_output = serde_json::to_string_pretty(&files)?; + println!("{}", json_output); + } + _ => { + for file in files { + println!("{}", file); + } + } } } - ViewCommands::Projects => { + ViewCommands::Projects { format } => { workspace.load().await?; let projects = workspace.affected_projects()?; @@ -104,8 +125,16 @@ async fn main() -> Result<()> { return Ok(()); } - for project in projects { - println!("{}", project); + match format.as_str() { + "json" => { + let json_output = serde_json::to_string_pretty(&projects)?; + println!("{}", json_output); + } + _ => { + for project in projects { + println!("{}", project); + } + } } /*