1use crate::{Format, Runnable, util::SizeFormat};
2use anyhow::Result;
3use clap::{Args, Parser};
4
5mod commit_stats;
6mod defrag;
7mod df;
8mod du;
9mod label;
10mod mkswapfile;
11mod resize;
12mod show;
13mod sync;
14mod usage;
15
16pub use self::{
17 commit_stats::*, defrag::*, df::*, du::*, label::*, mkswapfile::*,
18 resize::*, show::*, sync::*, usage::*,
19};
20
21#[derive(Parser, Debug)]
28pub struct FilesystemCommand {
29 #[clap(subcommand)]
30 pub subcommand: FilesystemSubcommand,
31}
32
33impl Runnable for FilesystemCommand {
34 fn run(&self, format: Format, dry_run: bool) -> Result<()> {
35 match &self.subcommand {
36 FilesystemSubcommand::Df(cmd) => cmd.run(format, dry_run),
37 FilesystemSubcommand::Du(cmd) => cmd.run(format, dry_run),
38 FilesystemSubcommand::Show(cmd) => cmd.run(format, dry_run),
39 FilesystemSubcommand::Sync(cmd) => cmd.run(format, dry_run),
40 FilesystemSubcommand::Defragment(cmd) => cmd.run(format, dry_run),
41 FilesystemSubcommand::Resize(cmd) => cmd.run(format, dry_run),
42 FilesystemSubcommand::Label(cmd) => cmd.run(format, dry_run),
43 FilesystemSubcommand::Usage(cmd) => cmd.run(format, dry_run),
44 FilesystemSubcommand::Mkswapfile(cmd) => cmd.run(format, dry_run),
45 FilesystemSubcommand::CommitStats(cmd) => cmd.run(format, dry_run),
46 }
47 }
48}
49
50#[derive(Parser, Debug)]
51pub enum FilesystemSubcommand {
52 Df(FilesystemDfCommand),
53 Du(FilesystemDuCommand),
54 Show(FilesystemShowCommand),
55 Sync(FilesystemSyncCommand),
56 #[clap(alias = "defrag")]
57 Defragment(FilesystemDefragCommand),
58 Resize(FilesystemResizeCommand),
59 Label(FilesystemLabelCommand),
60 Usage(FilesystemUsageCommand),
61 Mkswapfile(FilesystemMkswapfileCommand),
62 CommitStats(FilesystemCommitStatsCommand),
63}
64
65#[derive(Args, Debug)]
71#[allow(clippy::struct_excessive_bools)]
72pub struct UnitMode {
73 #[clap(long, overrides_with_all = ["human_readable", "iec", "si", "kbytes", "mbytes", "gbytes", "tbytes"])]
75 pub raw: bool,
76
77 #[clap(long, overrides_with_all = ["raw", "iec", "si", "kbytes", "mbytes", "gbytes", "tbytes"])]
79 pub human_readable: bool,
80
81 #[clap(long, overrides_with_all = ["raw", "human_readable", "si", "kbytes", "mbytes", "gbytes", "tbytes"])]
83 pub iec: bool,
84
85 #[clap(long, overrides_with_all = ["raw", "human_readable", "iec", "kbytes", "mbytes", "gbytes", "tbytes"])]
87 pub si: bool,
88
89 #[clap(long, overrides_with_all = ["raw", "human_readable", "iec", "si", "mbytes", "gbytes", "tbytes"])]
91 pub kbytes: bool,
92
93 #[clap(long, overrides_with_all = ["raw", "human_readable", "iec", "si", "kbytes", "gbytes", "tbytes"])]
95 pub mbytes: bool,
96
97 #[clap(long, overrides_with_all = ["raw", "human_readable", "iec", "si", "kbytes", "mbytes", "tbytes"])]
99 pub gbytes: bool,
100
101 #[clap(long, overrides_with_all = ["raw", "human_readable", "iec", "si", "kbytes", "mbytes", "gbytes"])]
103 pub tbytes: bool,
104}
105
106impl UnitMode {
107 #[must_use]
109 pub fn resolve(&self) -> SizeFormat {
110 let si = self.si;
111 if self.raw {
112 SizeFormat::Raw
113 } else if self.kbytes {
114 SizeFormat::Fixed(if si { 1000 } else { 1024 })
115 } else if self.mbytes {
116 SizeFormat::Fixed(if si { 1_000_000 } else { 1024 * 1024 })
117 } else if self.gbytes {
118 SizeFormat::Fixed(if si {
119 1_000_000_000
120 } else {
121 1024 * 1024 * 1024
122 })
123 } else if self.tbytes {
124 SizeFormat::Fixed(if si {
125 1_000_000_000_000
126 } else {
127 1024u64.pow(4)
128 })
129 } else if si {
130 SizeFormat::HumanSi
131 } else {
132 SizeFormat::HumanIec
133 }
134 }
135}