bundle change-password cli with peach-web
This commit is contained in:
parent
eaca10954e
commit
bc190051db
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -2575,6 +2575,7 @@ dependencies = [
|
|||||||
"peach-stats",
|
"peach-stats",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"rouille",
|
"rouille",
|
||||||
|
"rpassword",
|
||||||
"temporary",
|
"temporary",
|
||||||
"urlencoding",
|
"urlencoding",
|
||||||
"vnstat_parse",
|
"vnstat_parse",
|
||||||
|
@ -52,3 +52,4 @@ xdg = "2.2"
|
|||||||
jsonrpc_client = { version = "0.7", features = ["macros", "reqwest"] }
|
jsonrpc_client = { version = "0.7", features = ["macros", "reqwest"] }
|
||||||
reqwest = "0.11.24"
|
reqwest = "0.11.24"
|
||||||
urlencoding = "2.1.3"
|
urlencoding = "2.1.3"
|
||||||
|
rpassword = "5.0"
|
||||||
|
34
peach-web/src/cli/change_password.rs
Normal file
34
peach-web/src/cli/change_password.rs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
use peach_lib::password_utils::set_new_password;
|
||||||
|
use crate::error::PeachWebError;
|
||||||
|
|
||||||
|
/// Utility function to set the admin password for peach-web from the command-line.
|
||||||
|
pub fn set_peach_web_password(password: Option<String>) -> Result<(), PeachWebError> {
|
||||||
|
match password {
|
||||||
|
// read password from CLI arg
|
||||||
|
Some(password) => {
|
||||||
|
set_new_password(&password)
|
||||||
|
.map_err(|err| PeachWebError::PeachLib { source: err, msg: "Error setting password via cli".to_string() })?;
|
||||||
|
println!(
|
||||||
|
"Your new password has been set for peach-web. You can login through the \
|
||||||
|
web interface with username admin."
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
// read password from tty
|
||||||
|
None => {
|
||||||
|
let pass1 = rpassword::read_password_from_tty(Some("New password: "))?;
|
||||||
|
let pass2 = rpassword::read_password_from_tty(Some("Confirm password: "))?;
|
||||||
|
if pass1 != pass2 {
|
||||||
|
Err(PeachWebError::InvalidPassword)
|
||||||
|
} else {
|
||||||
|
set_new_password(&pass1)
|
||||||
|
.map_err(|err| PeachWebError::PeachLib { source: err, msg: "Passwords did not match".to_string() })?;
|
||||||
|
println!(
|
||||||
|
"Your new password has been set for peach-web. You can login through the \
|
||||||
|
web interface with username admin."
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
1
peach-web/src/cli/mod.rs
Normal file
1
peach-web/src/cli/mod.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub mod change_password;
|
@ -19,6 +19,7 @@ pub enum PeachWebError {
|
|||||||
PeachLib { source: PeachError, msg: String },
|
PeachLib { source: PeachError, msg: String },
|
||||||
Yaml(YamlError),
|
Yaml(YamlError),
|
||||||
Tilde(TildeError),
|
Tilde(TildeError),
|
||||||
|
InvalidPassword,
|
||||||
NotYetImplemented,
|
NotYetImplemented,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,6 +28,7 @@ impl std::error::Error for PeachWebError {
|
|||||||
match *self {
|
match *self {
|
||||||
PeachWebError::FailedToRegisterDynDomain(_) => None,
|
PeachWebError::FailedToRegisterDynDomain(_) => None,
|
||||||
PeachWebError::HomeDir => None,
|
PeachWebError::HomeDir => None,
|
||||||
|
PeachWebError::InvalidPassword => None,
|
||||||
PeachWebError::Io(ref source) => Some(source),
|
PeachWebError::Io(ref source) => Some(source),
|
||||||
PeachWebError::Json(ref source) => Some(source),
|
PeachWebError::Json(ref source) => Some(source),
|
||||||
PeachWebError::OsString => None,
|
PeachWebError::OsString => None,
|
||||||
@ -48,6 +50,10 @@ impl std::fmt::Display for PeachWebError {
|
|||||||
f,
|
f,
|
||||||
"Filesystem error: failed to determine home directory path"
|
"Filesystem error: failed to determine home directory path"
|
||||||
),
|
),
|
||||||
|
PeachWebError::InvalidPassword => write!(
|
||||||
|
f,
|
||||||
|
"Failed to change password via CLI"
|
||||||
|
),
|
||||||
PeachWebError::Io(ref source) => write!(f, "IO error: {}", source),
|
PeachWebError::Io(ref source) => write!(f, "IO error: {}", source),
|
||||||
PeachWebError::Json(ref source) => write!(f, "Serde JSON error: {}", source),
|
PeachWebError::Json(ref source) => write!(f, "Serde JSON error: {}", source),
|
||||||
PeachWebError::OsString => write!(
|
PeachWebError::OsString => write!(
|
||||||
|
@ -19,11 +19,9 @@ mod public_router;
|
|||||||
mod routes;
|
mod routes;
|
||||||
mod templates;
|
mod templates;
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
|
mod cli;
|
||||||
|
|
||||||
use std::{
|
use std::{collections::HashMap, env, sync::{Mutex, RwLock}};
|
||||||
collections::HashMap,
|
|
||||||
sync::{Mutex, RwLock},
|
|
||||||
};
|
|
||||||
|
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use log::info;
|
use log::info;
|
||||||
@ -52,9 +50,7 @@ pub struct SessionData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Launch the peach-web server.
|
/// Launch the peach-web server.
|
||||||
fn main() {
|
fn run_webserver() {
|
||||||
// initialize logger
|
|
||||||
env_logger::init();
|
|
||||||
|
|
||||||
// set ip address / hostname and port for the webserver
|
// set ip address / hostname and port for the webserver
|
||||||
// defaults to "127.0.0.1:8000"
|
// defaults to "127.0.0.1:8000"
|
||||||
@ -121,3 +117,22 @@ fn main() {
|
|||||||
})
|
})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// cli entry point
|
||||||
|
fn main() {
|
||||||
|
env_logger::init();
|
||||||
|
|
||||||
|
let mut args = env::args();
|
||||||
|
let _program = args.next(); // skip program name
|
||||||
|
|
||||||
|
match args.next().as_deref() {
|
||||||
|
Some("run") => run_webserver(),
|
||||||
|
Some("change-password") => {
|
||||||
|
cli::change_password::set_peach_web_password(args.next());
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
eprintln!("Usage: peach-web <run|change-password>");
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -38,6 +38,7 @@ impl TildeClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let result = String::from_utf8_lossy(&output.stdout).to_string();
|
let result = String::from_utf8_lossy(&output.stdout).to_string();
|
||||||
|
println!("Command: {:?}", command);
|
||||||
println!("Command output: {}", result);
|
println!("Command output: {}", result);
|
||||||
|
|
||||||
Ok(result)
|
Ok(result)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user