feat(commands): ls: Add option --json (#1251)

Co-authored-by: aawsome <37850842+aawsome@users.noreply.github.com>
This commit is contained in:
Krzysztof Małysa 2024-09-22 23:03:29 +02:00 committed by GitHub
parent a21c991b33
commit 63d5f166cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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 {