From 63d5f166cd03c61bd529dccdb07399d41a3b5566 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Ma=C5=82ysa?= Date: Sun, 22 Sep 2024 23:03:29 +0200 Subject: [PATCH] feat(commands): ls: Add option --json (#1251) Co-authored-by: aawsome <37850842+aawsome@users.noreply.github.com> --- src/commands/ls.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/commands/ls.rs b/src/commands/ls.rs index 054ec98..bb5eea0 100644 --- a/src/commands/ls.rs +++ b/src/commands/ls.rs @@ -36,13 +36,17 @@ pub(crate) struct LsCmd { snap: String, /// show summary - #[clap(long, short = 's')] + #[clap(long, short = 's', conflicts_with = "json")] summary: bool, /// show long listing - #[clap(long, short = 'l')] + #[clap(long, short = 'l', conflicts_with = "json")] long: bool, + /// show listing in json + #[clap(long, conflicts_with_all = ["summary", "long"])] + json: bool, + /// show uid/gid instead of user/group #[clap(long, long("numeric-uid-gid"))] numeric_id: bool, @@ -135,14 +139,29 @@ impl LsCmd { let mut summary = Summary::default(); + if self.json { + print!("["); + } + + let mut first_item = true; for item in repo.ls(&node, &ls_opts)? { let (path, node) = item?; summary.update(&node); - if self.long { + if self.json { + if !first_item { + print!(","); + } + print!("{}", serde_json::to_string(&path)?); + } else if self.long { print_node(&node, &path, self.numeric_id); } else { println!("{}", path.display()); } + first_item = false; + } + + if self.json { + println!("]"); } if self.summary {