Skip to content

Commit

Permalink
support json output (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
DenysVuika authored Nov 19, 2024
1 parent 6152b3a commit d282642
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down Expand Up @@ -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()?;
Expand All @@ -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);
}
}
}

/*
Expand Down

0 comments on commit d282642

Please sign in to comment.