Skip to main content

btrfs_cli/property/
list.rs

1use super::{
2    PropertyObjectType, detect_object_types, property_description,
3    property_names,
4};
5use crate::{RunContext, Runnable};
6use anyhow::{Result, anyhow, bail};
7use clap::Parser;
8use std::path::PathBuf;
9
10/// List available properties with their descriptions for the given object
11#[derive(Parser, Debug)]
12pub struct PropertyListCommand {
13    /// Btrfs object path to list properties for
14    pub object: PathBuf,
15
16    /// Object type (inode, subvol, filesystem, device)
17    #[clap(short = 't', long = "type")]
18    pub object_type: Option<PropertyObjectType>,
19}
20
21impl Runnable for PropertyListCommand {
22    fn run(&self, _ctx: &RunContext) -> Result<()> {
23        // Detect object type if not specified
24        let detected_types = detect_object_types(&self.object);
25        let target_type = if let Some(t) = self.object_type {
26            t
27        } else {
28            // If ambiguous, require the user to specify
29            if detected_types.len() > 1 {
30                bail!(
31                    "object type is ambiguous, please use option -t (detected: {detected_types:?})"
32                );
33            }
34            detected_types
35                .first()
36                .copied()
37                .ok_or_else(|| anyhow!("object is not a btrfs object"))?
38        };
39
40        for name in property_names(target_type) {
41            println!("{:<20}{}", name, property_description(name));
42        }
43
44        Ok(())
45    }
46}