Skip to main content

portable_network_archive/
command.rs

1mod acl;
2pub mod append;
3pub(crate) mod bsdtar;
4pub mod bugreport;
5mod chmod;
6mod chown;
7mod chunk;
8pub(super) mod compat;
9pub mod complete;
10pub(crate) mod concat;
11pub(crate) mod core;
12pub mod create;
13pub mod delete;
14pub mod diff;
15pub(super) mod experimental;
16pub mod extract;
17pub mod list;
18mod migrate;
19pub mod sort;
20pub mod split;
21pub(crate) mod strip;
22pub mod update;
23pub mod xattr;
24
25use crate::cli::{CipherAlgorithmArgs, Cli, Commands, GlobalContext, PasswordArgs};
26use std::{fs, io};
27
28fn ask_password(args: PasswordArgs) -> io::Result<Option<Vec<u8>>> {
29    if let Some(path) = args.password_file {
30        return Ok(Some(fs::read(path)?));
31    };
32    Ok(match args.password {
33        Some(Some(password)) => {
34            log::warn!("Using a password on the command line interface can be insecure.");
35            Some(password.into_bytes())
36        }
37        Some(None) => Some(
38            gix_prompt::securely("Enter password: ")
39                .map_err(io::Error::other)?
40                .into_bytes(),
41        ),
42        None => None,
43    })
44}
45
46fn check_password(password: &Option<Vec<u8>>, cipher_args: &CipherAlgorithmArgs) {
47    if password.is_some() {
48        return;
49    }
50    if cipher_args.aes.is_some() {
51        log::warn!("Using `--aes` option but, `--password` was not provided. It will not encrypt.");
52    } else if cipher_args.camellia.is_some() {
53        log::warn!(
54            "Using `--camellia` option but, `--password` was not provided. It will not encrypt."
55        );
56    }
57}
58
59pub(crate) trait Command {
60    fn execute(self, ctx: &GlobalContext) -> anyhow::Result<()>;
61}
62
63impl Cli {
64    #[inline]
65    pub fn execute(self) -> anyhow::Result<()> {
66        let ctx = &GlobalContext::new(self.global);
67        match self.commands {
68            Commands::Create(cmd) => cmd.execute(ctx),
69            Commands::Append(cmd) => cmd.execute(ctx),
70            Commands::Extract(cmd) => cmd.execute(ctx),
71            Commands::List(cmd) => cmd.execute(ctx),
72            Commands::Delete(cmd) => cmd.execute(ctx),
73            Commands::Split(cmd) => cmd.execute(ctx),
74            Commands::Concat(cmd) => cmd.execute(ctx),
75            Commands::Strip(cmd) => cmd.execute(ctx),
76            Commands::Sort(cmd) => cmd.execute(ctx),
77            Commands::Xattr(cmd) => cmd.execute(ctx),
78            Commands::Complete(cmd) => cmd.execute(ctx),
79            Commands::BugReport(cmd) => cmd.execute(ctx),
80            Commands::Compat(cmd) => cmd.execute(ctx),
81            Commands::Experimental(cmd) => cmd.execute(ctx),
82        }
83    }
84}