Skip to content

Commit

Permalink
🚧 use anyhow::Result instead of io::Result in cli
Browse files Browse the repository at this point in the history
  • Loading branch information
ChanTsune committed Nov 18, 2024
1 parent 0ca2bec commit 373fb29
Show file tree
Hide file tree
Showing 19 changed files with 82 additions and 51 deletions.
4 changes: 2 additions & 2 deletions cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod xattr;
use crate::cli::{CipherAlgorithmArgs, Cli, Commands, PasswordArgs};
use std::{fs, io};

pub fn entry(cli: Cli) -> io::Result<()> {
pub fn entry(cli: Cli) -> anyhow::Result<()> {
match cli.commands {
Commands::Create(cmd) => cmd.execute(),
Commands::Append(cmd) => cmd.execute(),
Expand Down Expand Up @@ -62,5 +62,5 @@ fn check_password(password: &Option<String>, cipher_args: &CipherAlgorithmArgs)
}

trait Command {
fn execute(self) -> io::Result<()>;
fn execute(self) -> anyhow::Result<()>;
}
13 changes: 7 additions & 6 deletions cli/src/command/acl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub(crate) struct AclCommand {

impl Command for AclCommand {
#[inline]
fn execute(self) -> io::Result<()> {
fn execute(self) -> anyhow::Result<()> {
match self.command {
XattrCommands::Get(cmd) => cmd.execute(),
XattrCommands::Set(cmd) => cmd.execute(),
Expand Down Expand Up @@ -61,7 +61,7 @@ pub(crate) struct GetAclCommand {

impl Command for GetAclCommand {
#[inline]
fn execute(self) -> io::Result<()> {
fn execute(self) -> anyhow::Result<()> {
archive_get_acl(self)
}
}
Expand All @@ -84,7 +84,7 @@ pub(crate) struct SetAclCommand {

impl Command for SetAclCommand {
#[inline]
fn execute(self) -> io::Result<()> {
fn execute(self) -> anyhow::Result<()> {
archive_set_acl(self)
}
}
Expand Down Expand Up @@ -207,7 +207,7 @@ impl FromStr for AclEntries {
}
}

fn archive_get_acl(args: GetAclCommand) -> io::Result<()> {
fn archive_get_acl(args: GetAclCommand) -> anyhow::Result<()> {
let password = ask_password(args.password)?;
if args.files.is_empty() {
return Ok(());
Expand Down Expand Up @@ -245,7 +245,7 @@ fn archive_get_acl(args: GetAclCommand) -> io::Result<()> {
Ok(())
}

fn archive_set_acl(args: SetAclCommand) -> io::Result<()> {
fn archive_set_acl(args: SetAclCommand) -> anyhow::Result<()> {
let password = ask_password(args.password)?;
if args.files.is_empty() {
return Ok(());
Expand Down Expand Up @@ -290,7 +290,8 @@ fn archive_set_acl(args: SetAclCommand) -> io::Result<()> {
},
TransformStrategyKeepSolid,
),
}
}?;
Ok(())
}

#[inline]
Expand Down
8 changes: 5 additions & 3 deletions cli/src/command/append.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
},
utils::{self, PathPartExt},
};
use anyhow::Context;
use clap::{ArgGroup, Parser, ValueHint};
use pna::Archive;
use rayon::ThreadPoolBuilder;
Expand Down Expand Up @@ -92,20 +93,21 @@ pub(crate) struct AppendCommand {

impl Command for AppendCommand {
#[inline]
fn execute(self) -> io::Result<()> {
fn execute(self) -> anyhow::Result<()> {
append_to_archive(self)
}
}

fn append_to_archive(args: AppendCommand) -> io::Result<()> {
fn append_to_archive(args: AppendCommand) -> anyhow::Result<()> {
let password = ask_password(args.password)?;
check_password(&password, &args.cipher);
let archive_path = args.file.archive;
if !archive_path.exists() {
return Err(io::Error::new(
io::ErrorKind::NotFound,
format!("{} is not exists", archive_path.display()),
));
))
.with_context(|| format!("{} does not exists", archive_path.display()));
}
let mut num = 1;
let file = File::options().write(true).read(true).open(&archive_path)?;
Expand Down
6 changes: 4 additions & 2 deletions cli/src/command/chmod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
},
utils::{GlobPatterns, PathPartExt},
};
use anyhow::Context;
use bitflags::bitflags;
use clap::{Parser, ValueHint};
use pna::NormalEntry;
Expand All @@ -28,12 +29,12 @@ pub(crate) struct ChmodCommand {

impl Command for ChmodCommand {
#[inline]
fn execute(self) -> io::Result<()> {
fn execute(self) -> anyhow::Result<()> {
archive_chmod(self)
}
}

fn archive_chmod(args: ChmodCommand) -> io::Result<()> {
fn archive_chmod(args: ChmodCommand) -> anyhow::Result<()> {
let password = ask_password(args.password)?;
if args.files.is_empty() {
return Ok(());
Expand Down Expand Up @@ -70,6 +71,7 @@ fn archive_chmod(args: ChmodCommand) -> io::Result<()> {
TransformStrategyKeepSolid,
),
}
.with_context(|| "While processing a chmod file")
}

#[inline]
Expand Down
6 changes: 4 additions & 2 deletions cli/src/command/chown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
},
utils::{GlobPatterns, PathPartExt},
};
use anyhow::Context;
use clap::{Parser, ValueHint};
use pna::NormalEntry;
use std::ops::Not;
Expand All @@ -30,12 +31,12 @@ pub(crate) struct ChownCommand {

impl Command for ChownCommand {
#[inline]
fn execute(self) -> io::Result<()> {
fn execute(self) -> anyhow::Result<()> {
archive_chown(self)
}
}

fn archive_chown(args: ChownCommand) -> io::Result<()> {
fn archive_chown(args: ChownCommand) -> anyhow::Result<()> {
let password = ask_password(args.password)?;
if args.files.is_empty() {
return Ok(());
Expand Down Expand Up @@ -72,6 +73,7 @@ fn archive_chown(args: ChownCommand) -> io::Result<()> {
TransformStrategyKeepSolid,
),
}
.with_context(|| "failed to combine transforms")
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion cli/src/command/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub(crate) struct CompleteCommand {

impl Command for CompleteCommand {
#[inline]
fn execute(self) -> io::Result<()> {
fn execute(self) -> anyhow::Result<()> {
let cmd = &mut Cli::command();
print_completions(self.shell, cmd);
Ok(())
Expand Down
11 changes: 7 additions & 4 deletions cli/src/command/concat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::command::commons::run_across_archive_mem;
#[cfg(not(feature = "memmap"))]
use crate::command::commons::{run_across_archive, PathArchiveProvider};
use crate::{cli::FileArgs, command::Command, utils};
use anyhow::Context;
use clap::Parser;
use pna::Archive;
use std::{fs, io};
Expand All @@ -17,24 +18,26 @@ pub(crate) struct ConcatCommand {

impl Command for ConcatCommand {
#[inline]
fn execute(self) -> io::Result<()> {
fn execute(self) -> anyhow::Result<()> {
concat_entry(self)
}
}

fn concat_entry(args: ConcatCommand) -> io::Result<()> {
fn concat_entry(args: ConcatCommand) -> anyhow::Result<()> {
if !args.overwrite && args.files.archive.exists() {
return Err(io::Error::new(
io::ErrorKind::AlreadyExists,
format!("{} is already exists", args.files.archive.display()),
));
))
.with_context(|| "");
}
for item in &args.files.files {
if !utils::fs::is_pna(item)? {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("{} is not a pna file", item),
));
))
.with_context(|| "");
}
}
let file = fs::File::create(&args.files.archive)?;
Expand Down
8 changes: 5 additions & 3 deletions cli/src/command/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
},
utils::{self, fmt::DurationDisplay},
};
use anyhow::Context;
use bytesize::ByteSize;
use clap::{ArgGroup, Parser, ValueHint};
use pna::{Archive, SolidEntryBuilder, WriteOptions};
Expand Down Expand Up @@ -105,12 +106,12 @@ pub(crate) struct CreateCommand {

impl Command for CreateCommand {
#[inline]
fn execute(self) -> io::Result<()> {
fn execute(self) -> anyhow::Result<()> {
create_archive(self)
}
}

fn create_archive(args: CreateCommand) -> io::Result<()> {
fn create_archive(args: CreateCommand) -> anyhow::Result<()> {
let password = ask_password(args.password.clone())?;
check_password(&password, &args.cipher);
let start = Instant::now();
Expand All @@ -119,7 +120,8 @@ fn create_archive(args: CreateCommand) -> io::Result<()> {
return Err(io::Error::new(
io::ErrorKind::AlreadyExists,
format!("{} is already exists", archive.display()),
));
))
.with_context(|| "");
}
log::info!("Create an archive: {}", archive.display());
let mut files = args.file.files;
Expand Down
6 changes: 4 additions & 2 deletions cli/src/command/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
},
utils::{GlobPatterns, PathPartExt},
};
use anyhow::Context;
use clap::{ArgGroup, Parser, ValueHint};
use std::{io, path::PathBuf};

Expand All @@ -29,12 +30,12 @@ pub(crate) struct DeleteCommand {

impl Command for DeleteCommand {
#[inline]
fn execute(self) -> io::Result<()> {
fn execute(self) -> anyhow::Result<()> {
delete_file_from_archive(self)
}
}

fn delete_file_from_archive(args: DeleteCommand) -> io::Result<()> {
fn delete_file_from_archive(args: DeleteCommand) -> anyhow::Result<()> {
let password = ask_password(args.password)?;
let globs = GlobPatterns::new(args.file.files)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;
Expand Down Expand Up @@ -72,4 +73,5 @@ fn delete_file_from_archive(args: DeleteCommand) -> io::Result<()> {
TransformStrategyKeepSolid,
),
}
.with_context(|| "While deleting a file")
}
3 changes: 1 addition & 2 deletions cli/src/command/experimental.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{command, command::Command};
use clap::{Parser, Subcommand};
use std::io;

#[derive(Parser, Clone, Eq, PartialEq, Hash, Debug)]
#[command(args_conflicts_with_subcommands = true, arg_required_else_help = true)]
Expand All @@ -11,7 +10,7 @@ pub(crate) struct ExperimentalCommand {

impl Command for ExperimentalCommand {
#[inline]
fn execute(self) -> io::Result<()> {
fn execute(self) -> anyhow::Result<()> {
match self.command {
ExperimentalCommands::Stdio(cmd) => cmd.execute(),
ExperimentalCommands::Delete(cmd) => cmd.execute(),
Expand Down
4 changes: 2 additions & 2 deletions cli/src/command/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ pub(crate) struct ExtractCommand {

impl Command for ExtractCommand {
#[inline]
fn execute(self) -> io::Result<()> {
fn execute(self) -> anyhow::Result<()> {
extract_archive(self)
}
}
fn extract_archive(args: ExtractCommand) -> io::Result<()> {
fn extract_archive(args: ExtractCommand) -> anyhow::Result<()> {
let password = ask_password(args.password)?;
let start = Instant::now();
log::info!("Extract archive {}", args.file.archive.display());
Expand Down
7 changes: 5 additions & 2 deletions cli/src/command/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
ext::*,
utils::GlobPatterns,
};
use anyhow::Context;
use chrono::{DateTime, Local};
use clap::{
builder::styling::{AnsiColor, Color as Colour, Style},
Expand Down Expand Up @@ -76,7 +77,7 @@ pub(crate) struct ListCommand {

impl Command for ListCommand {
#[inline]
fn execute(self) -> io::Result<()> {
fn execute(self) -> anyhow::Result<()> {
list_archive(self)
}
}
Expand Down Expand Up @@ -200,7 +201,7 @@ where
}
}

fn list_archive(args: ListCommand) -> io::Result<()> {
fn list_archive(args: ListCommand) -> anyhow::Result<()> {
let password = ask_password(args.password)?;
let options = ListOptions {
long: args.long,
Expand All @@ -219,6 +220,7 @@ fn list_archive(args: ListCommand) -> io::Result<()> {
&args.file.files,
options,
)
.with_context(|| "")
}
#[cfg(feature = "memmap")]
{
Expand All @@ -228,6 +230,7 @@ fn list_archive(args: ListCommand) -> io::Result<()> {
&args.file.files,
options,
)
.with_context(|| "")
}
}

Expand Down
7 changes: 4 additions & 3 deletions cli/src/command/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ pub(crate) struct MigrateCommand {

impl Command for MigrateCommand {
#[inline]
fn execute(self) -> io::Result<()> {
fn execute(self) -> anyhow::Result<()> {
migrate_metadata(self)
}
}

fn migrate_metadata(args: MigrateCommand) -> io::Result<()> {
fn migrate_metadata(args: MigrateCommand) -> anyhow::Result<()> {
let password = ask_password(args.password)?;
match args.transform_strategy.strategy() {
SolidEntriesTransformStrategy::UnSolid => run_transform_entry(
Expand All @@ -47,7 +47,8 @@ fn migrate_metadata(args: MigrateCommand) -> io::Result<()> {
|entry| Ok(Some(strip_entry_metadata(entry?)?)),
TransformStrategyKeepSolid,
),
}
}?;
Ok(())
}

#[inline]
Expand Down
Loading

0 comments on commit 373fb29

Please sign in to comment.