feat: refactored main()
This commit is contained in:
parent
c6fd190660
commit
bcb17136d9
1 changed files with 34 additions and 26 deletions
60
src/main.rs
60
src/main.rs
|
|
@ -1,5 +1,5 @@
|
||||||
use anyhow::*;
|
use anyhow::*;
|
||||||
use std::{io::Write, process::Command, };
|
use std::{io::Write, mem::align_of_val, process::Command};
|
||||||
use wifi_connector::network::Network;
|
use wifi_connector::network::Network;
|
||||||
|
|
||||||
fn get_available_wifis() -> Result<String> {
|
fn get_available_wifis() -> Result<String> {
|
||||||
|
|
@ -78,48 +78,56 @@ fn try_connect_to_network_without_password(network: &Network) -> Result<()> {
|
||||||
.output()
|
.output()
|
||||||
.expect("Failed to execute command");
|
.expect("Failed to execute command");
|
||||||
|
|
||||||
|
|
||||||
if output.status.success() {
|
if output.status.success() {
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
dbg!(output.stderr);
|
|
||||||
Err(anyhow!("Couldn't connect to wifi without password"))
|
Err(anyhow!("Couldn't connect to wifi without password"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn print_networks(networks: &Vec<Network>) {
|
||||||
let all_networks = get_all_networks();
|
for (idx, network) in networks.iter().enumerate() {
|
||||||
|
|
||||||
for (idx, network) in all_networks.iter().enumerate() {
|
|
||||||
println!("{} - {}", idx, network);
|
println!("{} - {}", idx, network);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
println!(
|
fn password_prompt_for_network(network: &Network) -> String {
|
||||||
"Which network do you choose? (0-{})",
|
let mut password = String::new();
|
||||||
all_networks.len() - 1
|
print!(
|
||||||
|
"Now enter the password for the selected wifi '{}': ",
|
||||||
|
network.ssid
|
||||||
);
|
);
|
||||||
|
std::io::stdout().flush().unwrap();
|
||||||
|
std::io::stdin().read_line(&mut password).unwrap();
|
||||||
|
password
|
||||||
|
}
|
||||||
|
|
||||||
|
fn choose_network(max: usize) -> String {
|
||||||
|
println!("Which network do you choose? (0-{})", max);
|
||||||
let mut input = String::new();
|
let mut input = String::new();
|
||||||
std::io::stdin().read_line(&mut input).unwrap();
|
std::io::stdin().read_line(&mut input).unwrap();
|
||||||
|
input
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let all_networks = get_all_networks();
|
||||||
|
print_networks(&all_networks);
|
||||||
|
|
||||||
|
let chosen_network_index = choose_network(all_networks.len() - 1);
|
||||||
|
|
||||||
let network = all_networks
|
let network = all_networks
|
||||||
.get(input.trim().parse::<usize>().unwrap())
|
.get(chosen_network_index.trim().parse::<usize>().unwrap())
|
||||||
.expect("Given number does not reference a network");
|
.expect("Given number does not reference a network");
|
||||||
let connected = try_connect_to_network_without_password(network);
|
let mut connected = try_connect_to_network_without_password(network);
|
||||||
|
|
||||||
if !connected.is_ok() {
|
if !connected.is_ok() {
|
||||||
let mut password = String::new();
|
let password = password_prompt_for_network(network);
|
||||||
print!(
|
connected = connect_to_network(&network, password.trim());
|
||||||
"Now enter the password for the selected wifi '{}': ",
|
}
|
||||||
network.ssid
|
|
||||||
);
|
if connected.is_ok() {
|
||||||
std::io::stdout().flush().unwrap();
|
|
||||||
std::io::stdin().read_line(&mut password).unwrap();
|
|
||||||
let result = connect_to_network(&network, password.trim());
|
|
||||||
if result.is_ok() {
|
|
||||||
println!("Successfully connected to network '{}'", network.ssid);
|
|
||||||
} else {
|
|
||||||
println!("Connection failed");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
println!("Successfully connected to network '{}'", network.ssid);
|
println!("Successfully connected to network '{}'", network.ssid);
|
||||||
|
} else {
|
||||||
|
println!("Connection failed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue