Skip to content

Commit

Permalink
prompt for password if none is given
Browse files Browse the repository at this point in the history
This shows a password prompt, if neither the `--password-file` or
`--password-command` option is given **and** the `RESTIC_PASSWORD` env
variable is unset.

There's no way to hand over the password to restic as a CLI parameter,
so we use the `RESTIC_PASSWORD` env variable under the hood to set it
instead. The env variable is only valid for the restic process we call
internally, so the next time redu is run, it'll prompt for the password
again.
  • Loading branch information
rluetzner committed Aug 1, 2024
1 parent e85ccf7 commit 7f29d52
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ ratatui = { version = "0.27", features = [
"unstable-rendered-line-info",
"unstable-widget-ref",
] }
rpassword = "7.3.1"
rusqlite = { version = "0.32", features = ["bundled", "functions", "trace"] }
scopeguard = "1"
serde = { version = "1", features = ["derive"] }
Expand Down
21 changes: 15 additions & 6 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{env, io::Write};

use clap::{ArgGroup, Parser};
use log::LevelFilter;
use redu::restic::Repository;
use rpassword::read_password;

use crate::restic::Password;

Expand Down Expand Up @@ -33,7 +36,11 @@ impl Args {
} else if let Some(str) = cli.restic_password {
Password::Plain(str)
} else {
unreachable!("Error in Config: neither password_command nor password_file found. Please open an issue if you see this.")
let pwd = Self::read_password_from_stdin();
// There's no way to hand over the password to restic directly, so we use the env
// variable instead.
env::set_var("RESTIC_PASSWORD", &pwd);
Password::Plain(pwd)
},
parallelism: cli.parallelism,
log_level: match cli.verbose {
Expand All @@ -44,6 +51,13 @@ impl Args {
no_cache: cli.no_cache,
}
}

fn read_password_from_stdin() -> String {
print!("enter password for repository: ");
// Console output is line buffered, so we need to flush to show the message.
_ = std::io::stdout().flush();
read_password().unwrap()
}
}

/// This is like ncdu for a restic respository.
Expand Down Expand Up @@ -83,11 +97,6 @@ impl Args {
.required(true)
.args(["repo", "repository_file"]),
))]
#[command(group(
ArgGroup::new("password")
.required(true)
.args(["password_command", "password_file", "restic_password"]),
))]
struct Cli {
#[arg(short = 'r', long, env = "RESTIC_REPOSITORY")]
repo: Option<String>,
Expand Down

0 comments on commit 7f29d52

Please sign in to comment.