Skip to content

Commit

Permalink
Add password pass
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Jun 10, 2024
1 parent 343dd78 commit 9572432
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
34 changes: 25 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ async fn start_server(prop: Properties, port: u16, working_dir: &str, password:
let _ = server.start().await;
}

/// Set up the session password.
fn get_password() -> Option<String> {
/// Ask the password.
fn ask_password() -> Option<String> {
print!("Set the password: ");
io::stdout().flush().unwrap();
let password = rpassword::read_password().unwrap();
Expand All @@ -96,6 +96,28 @@ fn get_password() -> Option<String> {
}
}

/// Find out what's the password.
///
/// # Arguments
///
/// * `matches` - Argument matches.
/// * `prop` - Properties object.
fn get_password(matches: &ArgMatches, prop: &Properties) -> Option<String> {
let no_password = matches.get_flag("no_password");
let prop_password = prop.get("cogru.Password");

let password = if no_password {
None
} else if !prop_password.is_none() {
// if password is set, use that one!
prop_password
} else {
Some(ask_password().expect("Confirm password doesn't match"))
};

password
}

/// Return the workspace path.
///
/// This is the directory we want to watch and sync.
Expand Down Expand Up @@ -163,13 +185,7 @@ async fn main() {
// Convert to u16
let port = port.parse::<u16>().unwrap();

let no_password = matches.get_flag("no_password");

let password = if no_password {
None
} else {
Some(get_password().expect("Confirm password doesn't match"))
};
let password = get_password(&matches, &prop);

// Setup logger
let _guard = setup_logger(&prop);
Expand Down
10 changes: 7 additions & 3 deletions src/server/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ impl Properties {
/// # Arguments
///
/// * `key` - The key used to find value.
pub fn get(&self, key: &str) -> Option<&String> {
self.data.get(key)
pub fn get(&self, key: &str) -> Option<String> {
let value = self.data.get(key);
if value.is_none() {
return None;
}
Some(value.unwrap().clone())
}

/// Return property value or the default value when null.
Expand All @@ -78,6 +82,6 @@ impl Properties {
if data.is_none() {
return default_value.to_string();
}
data.unwrap().clone()
data.unwrap()
}
}

0 comments on commit 9572432

Please sign in to comment.