Use CreateFlags
to parse flags for archive creation
This commit is contained in:
parent
5420684061
commit
dd8daa26ed
@ -33,16 +33,17 @@ struct ListingFlags {
|
||||
}
|
||||
|
||||
impl From<&ArgMatches> for ListingFlags {
|
||||
fn from(value: &ArgMatches) -> Self {
|
||||
fn from(matches: &ArgMatches) -> Self {
|
||||
Self {
|
||||
files: value.get_flag("files"),
|
||||
color: value.get_flag("color"),
|
||||
long: value.get_flag("long"),
|
||||
human: value.get_flag("human"),
|
||||
files: matches.get_flag("files"),
|
||||
color: matches.get_flag("color"),
|
||||
long: matches.get_flag("long"),
|
||||
human: matches.get_flag("human"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
enum Verbosity {
|
||||
Quiet,
|
||||
Normal,
|
||||
@ -50,7 +51,7 @@ enum Verbosity {
|
||||
}
|
||||
|
||||
impl From<&ArgMatches> for Verbosity {
|
||||
fn from(value: &ArgMatches) -> Self {
|
||||
fn from(matches: &ArgMatches) -> Self {
|
||||
if matches.get_flag("quiet") {
|
||||
Self::Quiet
|
||||
} else if matches.get_flag("verbose") && !matches.get_flag("stdout") {
|
||||
@ -70,9 +71,22 @@ struct CreateFlags {
|
||||
gid: Option<u32>,
|
||||
}
|
||||
|
||||
impl From<&ArgMatches> for CreateFlags {
|
||||
fn from(value: &ArgMatches) -> Self {
|
||||
todo!()
|
||||
impl TryFrom<&ArgMatches> for CreateFlags {
|
||||
type Error = haggis::Error;
|
||||
|
||||
fn try_from(matches: &ArgMatches) -> Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
algorithm: matches.get_one::<String>("algorithm").unwrap().parse()?,
|
||||
zstd_compression: if matches.get_flag("zstd") {
|
||||
Some(matches.get_one::<i32>("level").copied().unwrap_or(3))
|
||||
} else {
|
||||
None
|
||||
},
|
||||
verbosity: Verbosity::from(matches),
|
||||
stdout: matches.get_flag("stdout"),
|
||||
uid: matches.get_one::<u32>("uid").copied(),
|
||||
gid: matches.get_one::<u32>("gid").copied(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -108,10 +122,7 @@ fn main() {
|
||||
|
||||
#[allow(clippy::similar_names)]
|
||||
fn create(matches: &ArgMatches) -> Result<(), haggis::Error> {
|
||||
let verbose = !matches.get_flag("stdout") || matches.get_flag("quiet");
|
||||
let algorithm: Algorithm = matches.get_one::<String>("algorithm").unwrap().parse()?;
|
||||
let uid = matches.get_one::<u32>("uid").copied();
|
||||
let gid = matches.get_one::<u32>("gid").copied();
|
||||
let flags = CreateFlags::try_from(matches)?;
|
||||
let mut files = vec![];
|
||||
if let Some(f) = matches.get_many::<String>("files") {
|
||||
for f in f {
|
||||
@ -136,7 +147,7 @@ fn create(matches: &ArgMatches) -> Result<(), haggis::Error> {
|
||||
let (sender, receiver) = mpsc::channel();
|
||||
let len = files.len();
|
||||
let mut handle = None;
|
||||
if verbose {
|
||||
if flags.verbosity == Verbosity::Verbose {
|
||||
let pb = ProgressBar::new(len as u64);
|
||||
pb.set_style(ProgressStyle::with_template(TEMPLATE).unwrap());
|
||||
pb.set_prefix("Adding files");
|
||||
@ -172,34 +183,33 @@ fn create(matches: &ArgMatches) -> Result<(), haggis::Error> {
|
||||
}
|
||||
}));
|
||||
}
|
||||
if matches.get_flag("zstd") {
|
||||
let level = matches.get_one::<i32>("level").copied().unwrap_or(3);
|
||||
if matches.get_flag("stdout") {
|
||||
if let Some(level) = flags.zstd_compression {
|
||||
if flags.stdout {
|
||||
let stdout = io::stdout();
|
||||
let mut writer = Encoder::new(stdout, level)?;
|
||||
haggis::par_stream_archive(&mut writer, &files, algorithm, &sender, uid, gid)?;
|
||||
haggis::par_stream_archive(&mut writer, &files, flags.algorithm, &sender, flags.uid, flags.gid)?;
|
||||
let _fd = writer.finish();
|
||||
} else if let Some(o) = output {
|
||||
let fd = File::create(o)?;
|
||||
let mut writer = Encoder::new(fd, level)?;
|
||||
haggis::par_stream_archive(&mut writer, &files, algorithm, &sender, uid, gid)?;
|
||||
haggis::par_stream_archive(&mut writer, &files, flags.algorithm, &sender, flags.uid, flags.gid)?;
|
||||
let _fd = writer.finish()?;
|
||||
} else {
|
||||
unreachable!();
|
||||
}
|
||||
} else if matches.get_flag("stdout") {
|
||||
} else if flags.stdout {
|
||||
let stdout = io::stdout();
|
||||
let mut writer = BufWriter::new(stdout);
|
||||
haggis::par_stream_archive(&mut writer, &files, algorithm, &sender, uid, gid)?;
|
||||
haggis::par_stream_archive(&mut writer, &files, flags.algorithm, &sender, flags.uid, flags.gid)?;
|
||||
} else if let Some(o) = output {
|
||||
haggis::par_create_archive(o, &files, algorithm, &sender, uid, gid)?;
|
||||
haggis::par_create_archive(o, &files, flags.algorithm, &sender, flags.uid, flags.gid)?;
|
||||
} else {
|
||||
unreachable!();
|
||||
}
|
||||
if let Some(handle) = handle {
|
||||
match handle.join() {
|
||||
Ok(()) => {
|
||||
if verbose {
|
||||
if flags.verbosity == Verbosity::Verbose {
|
||||
println!("Archive created successfully");
|
||||
}
|
||||
Ok(())
|
||||
|
Loading…
Reference in New Issue
Block a user