-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcli.rs
More file actions
302 lines (280 loc) · 10.2 KB
/
cli.rs
File metadata and controls
302 lines (280 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
pub mod value;
use crate::command::{
append::AppendCommand, bugreport::BugReportCommand, complete::CompleteCommand,
concat::ConcatCommand, create::CreateCommand, experimental::ExperimentalCommand,
extract::ExtractCommand, list::ListCommand, split::SplitCommand, strip::StripCommand,
xattr::XattrCommand,
};
use clap::{ArgGroup, Parser, Subcommand, ValueEnum, ValueHint, value_parser};
use log::{Level, LevelFilter};
use pna::HashAlgorithm;
use std::{io, path::PathBuf};
pub(crate) use value::*;
#[derive(Parser, Clone, Debug)]
#[command(
name = env!("CARGO_PKG_NAME"),
version,
about,
author,
arg_required_else_help = true,
)]
pub struct Cli {
#[command(subcommand)]
pub(crate) commands: Commands,
#[command(flatten)]
pub(crate) verbosity: VerbosityArgs,
#[arg(
long,
global = true,
help = "Enable experimental options. Required for flags marked as unstable; behavior may change or be removed."
)]
pub(crate) unstable: bool,
}
impl Cli {
pub fn init_logger(&self) -> io::Result<()> {
let level = self.verbosity.log_level_filter();
let base = fern::Dispatch::new();
let stderr = fern::Dispatch::new()
.level(level)
.format(|out, msg, rec| match rec.level() {
Level::Error => out.finish(format_args!("error: {msg}")),
Level::Warn => out.finish(format_args!("warning: {msg}")),
Level::Info | Level::Debug | Level::Trace => out.finish(*msg),
})
.chain(io::stderr());
base.chain(stderr).apply().map_err(io::Error::other)
}
}
#[derive(Parser, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[command(group(ArgGroup::new("verbosity").args(["quiet", "verbose"])))]
pub(crate) struct VerbosityArgs {
#[arg(long, global = true, help = "Make some output more quiet")]
quiet: bool,
#[arg(long, global = true, help = "Make some output more verbose")]
verbose: bool,
}
impl VerbosityArgs {
#[inline]
#[allow(dead_code)]
pub(crate) const fn log_level_filter(&self) -> LevelFilter {
match (self.quiet, self.verbose) {
(true, false) => LevelFilter::Off,
(false, true) => LevelFilter::Debug,
(_, _) => LevelFilter::Info,
}
}
}
#[derive(Subcommand, Clone, Debug)]
pub(crate) enum Commands {
#[command(visible_alias = "c", about = "Create archive")]
Create(CreateCommand),
#[command(visible_alias = "a", about = "Append files to archive")]
Append(AppendCommand),
#[command(visible_alias = "x", about = "Extract files from archive")]
Extract(ExtractCommand),
#[command(visible_aliases = &["l", "ls"], about = "List files in archive")]
List(ListCommand),
#[command(about = "Split archive")]
Split(SplitCommand),
#[command(about = "Concat archives")]
Concat(ConcatCommand),
#[command(about = "Strip entries metadata")]
Strip(StripCommand),
#[command(about = "Manipulate extended attributes")]
Xattr(XattrCommand),
#[command(about = "Generate shell auto complete")]
Complete(CompleteCommand),
#[command(about = "Generate bug report template")]
BugReport(BugReportCommand),
#[command(
about = "Unstable experimental commands; behavior and interface may change or be removed"
)]
Experimental(ExperimentalCommand),
}
#[derive(Parser, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub(crate) struct FileArgs {
#[arg(short = 'f', long = "file", value_hint = ValueHint::FilePath)]
pub(crate) archive: PathBuf,
#[arg(value_hint = ValueHint::FilePath)]
pub(crate) files: Vec<String>,
}
// Archive related args for compatibility with optional and positional arguments.
// This is a temporary measure while the compatibility feature is available,
// and will be removed once the compatibility feature is no longer available.
//
// NOTE: Do NOT use doc comments (///) here as they become `long_about` in clap
// and propagate to commands that flatten this struct, causing incorrect documentation.
#[derive(Parser, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[command(
group(
ArgGroup::new("archive-args")
.args(["file", "archive"])
.multiple(true)
.required(true)
)
)]
pub(crate) struct FileArgsCompat {
#[arg(short, long, value_hint = ValueHint::FilePath)]
file: Option<PathBuf>,
#[arg(value_hint = ValueHint::FilePath)]
archive: Option<PathBuf>,
#[arg(value_hint = ValueHint::FilePath)]
files: Vec<String>,
}
impl FileArgsCompat {
#[inline]
pub(crate) fn archive(&self) -> PathBuf {
if let Some(file) = &self.file {
file.clone()
} else if let Some(archive) = &self.archive {
log::warn!("positional `archive` is deprecated, use `--file` instead");
archive.clone()
} else {
unreachable!()
}
}
#[inline]
pub(crate) fn files(&self) -> Vec<String> {
if self.file.is_none() {
self.files.clone()
} else {
let mut files = self.files.clone();
if let Some(archive) = &self.archive {
files.insert(0, archive.to_string_lossy().to_string());
}
files
}
}
}
#[derive(Parser, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[command(group(ArgGroup::new("password_provider").args(["password", "password_file"])))]
pub(crate) struct PasswordArgs {
#[arg(
long,
visible_alias = "passphrase",
help = "Password of archive. If password is not given it's asked from the tty"
)]
pub(crate) password: Option<Option<String>>,
#[arg(long, help = "Read password from specified file")]
pub(crate) password_file: Option<PathBuf>,
}
#[derive(Parser, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[command(group(ArgGroup::new("transform_strategy").args(["unsolid", "keep_solid"])))]
pub(crate) struct SolidEntriesTransformStrategyArgs {
#[arg(long, help = "Unsolid input solid entries.")]
pub(crate) unsolid: bool,
#[arg(long, help = "Keep input solid entries.")]
pub(crate) keep_solid: bool,
}
impl SolidEntriesTransformStrategyArgs {
#[inline]
pub(crate) const fn strategy(&self) -> SolidEntriesTransformStrategy {
if self.unsolid {
SolidEntriesTransformStrategy::UnSolid
} else {
SolidEntriesTransformStrategy::KeepSolid
}
}
}
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub(crate) enum SolidEntriesTransformStrategy {
UnSolid,
KeepSolid,
}
#[derive(Parser, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[command(group(ArgGroup::new("compression_method").args(["store", "deflate", "zstd", "xz"])))]
pub(crate) struct CompressionAlgorithmArgs {
#[arg(long, help = "No compression")]
pub(crate) store: bool,
#[arg(
long,
value_name = "level",
value_parser = value_parser!(u8).range(1..=9),
help = "Use deflate for compression [possible level: 1-9]"
)]
pub(crate) deflate: Option<Option<u8>>,
#[arg(
long,
value_name = "level",
value_parser = value_parser!(u8).range(1..=21),
help = "Use zstd for compression [possible level: 1-21]"
)]
pub(crate) zstd: Option<Option<u8>>,
#[arg(
long,
value_name = "level",
value_parser = value_parser!(u8).range(0..=9),
help = "Use xz for compression [possible level: 0-9]"
)]
pub(crate) xz: Option<Option<u8>>,
}
impl CompressionAlgorithmArgs {
pub(crate) fn algorithm(&self) -> (pna::Compression, Option<pna::CompressionLevel>) {
if self.store {
(pna::Compression::No, None)
} else if let Some(level) = self.xz {
(pna::Compression::XZ, level.map(Into::into))
} else if let Some(level) = self.zstd {
(pna::Compression::ZStandard, level.map(Into::into))
} else if let Some(level) = self.deflate {
(pna::Compression::Deflate, level.map(Into::into))
} else {
(pna::Compression::ZStandard, None)
}
}
}
#[derive(Parser, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[command(group(ArgGroup::new("cipher_algorithm").args(["aes", "camellia"])))]
pub(crate) struct CipherAlgorithmArgs {
#[arg(long, value_name = "cipher mode", help = "Use aes for encryption")]
pub(crate) aes: Option<Option<CipherMode>>,
#[arg(long, value_name = "cipher mode", help = "Use camellia for encryption")]
pub(crate) camellia: Option<Option<CipherMode>>,
}
impl CipherAlgorithmArgs {
pub(crate) const fn algorithm(&self) -> pna::Encryption {
if self.aes.is_some() {
pna::Encryption::Aes
} else if self.camellia.is_some() {
pna::Encryption::Camellia
} else {
pna::Encryption::Aes
}
}
pub(crate) fn mode(&self) -> pna::CipherMode {
match match (self.aes, self.camellia) {
(Some(mode), _) | (_, Some(mode)) => mode.unwrap_or_default(),
(None, None) => CipherMode::default(),
} {
CipherMode::Cbc => pna::CipherMode::CBC,
CipherMode::Ctr => pna::CipherMode::CTR,
}
}
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default, ValueEnum)]
pub(crate) enum CipherMode {
Cbc,
#[default]
Ctr,
}
#[derive(Parser, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[command(group(ArgGroup::new("hash_algorithm").args(["argon2", "pbkdf2"])))]
pub(crate) struct HashAlgorithmArgs {
#[arg(long, help = "Use argon2 for password hashing")]
pub(crate) argon2: Option<Option<Argon2idParams>>,
#[arg(long, help = "Use pbkdf2 for password hashing")]
pub(crate) pbkdf2: Option<Option<Pbkdf2Sha256Params>>,
}
impl HashAlgorithmArgs {
pub(crate) fn algorithm(&self) -> HashAlgorithm {
if let Some(Some(params)) = &self.pbkdf2 {
HashAlgorithm::pbkdf2_sha256_with(params.rounds)
} else if self.pbkdf2.as_ref().is_some_and(|it| it.is_none()) {
HashAlgorithm::pbkdf2_sha256()
} else if let Some(Some(params)) = &self.argon2 {
HashAlgorithm::argon2id_with(params.time, params.memory, params.parallelism)
} else {
HashAlgorithm::argon2id()
}
}
}