Skip to main content

btrfs_cli/property/
list.rs

1use super::{
2    PropertyObjectType, detect_object_types, property_description,
3    property_names,
4};
5use crate::{Format, 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, _format: Format, _dry_run: bool) -> Result<()> {
23        // Detect object type if not specified
24        let detected_types = detect_object_types(&self.object);
25        let target_type = match self.object_type {
26            Some(t) => t,
27            None => {
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: {:?})",
32                        detected_types
33                    );
34                }
35                detected_types
36                    .first()
37                    .copied()
38                    .ok_or_else(|| anyhow!("object is not a btrfs object"))?
39            }
40        };
41
42        for name in property_names(target_type) {
43            println!("{:<20}{}", name, property_description(name));
44        }
45
46        Ok(())
47    }
48}