test login credentials before entering main program
closes #58 this is a bit messy, as librespot panics, when authentication fails. thus, we test the credentials in a separate thread to handle the panic. see also: https://github.com/librespot-org/librespot/issues/108
This commit is contained in:
47
src/main.rs
47
src/main.rs
@@ -28,6 +28,7 @@ extern crate fern;
|
||||
|
||||
extern crate rand;
|
||||
|
||||
use std::fs;
|
||||
use std::process;
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
@@ -83,6 +84,31 @@ fn setup_logging(filename: &str) -> Result<(), fern::InitError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_credentials(reset: bool) -> Credentials {
|
||||
let path = config::config_path("credentials.toml");
|
||||
if reset {
|
||||
if fs::remove_file(&path).is_err() {
|
||||
error!("could not delete credential file");
|
||||
}
|
||||
}
|
||||
|
||||
let creds =
|
||||
::config::load_or_generate_default(&path, authentication::create_credentials, true)
|
||||
.unwrap_or_else(|e| {
|
||||
eprintln!("{}", e);
|
||||
process::exit(1);
|
||||
});
|
||||
|
||||
#[cfg(target_family = "unix")]
|
||||
std::fs::set_permissions(path, std::os::unix::fs::PermissionsExt::from_mode(0o600))
|
||||
.unwrap_or_else(|e| {
|
||||
eprintln!("{}", e);
|
||||
process::exit(1);
|
||||
});
|
||||
|
||||
creds
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let matches = App::new("ncspot")
|
||||
.version("0.1.0")
|
||||
@@ -113,24 +139,11 @@ fn main() {
|
||||
})
|
||||
};
|
||||
|
||||
let credentials: Credentials = {
|
||||
let path = config::config_path("credentials.toml");
|
||||
let creds =
|
||||
::config::load_or_generate_default(&path, authentication::create_credentials, true)
|
||||
.unwrap_or_else(|e| {
|
||||
eprintln!("{}", e);
|
||||
process::exit(1);
|
||||
});
|
||||
let mut credentials = get_credentials(false);
|
||||
|
||||
#[cfg(target_family = "unix")]
|
||||
std::fs::set_permissions(path, std::os::unix::fs::PermissionsExt::from_mode(0o600))
|
||||
.unwrap_or_else(|e| {
|
||||
eprintln!("{}", e);
|
||||
process::exit(1);
|
||||
});
|
||||
|
||||
creds
|
||||
};
|
||||
while !spotify::Spotify::test_credentials(credentials.clone()) {
|
||||
credentials = get_credentials(true);
|
||||
}
|
||||
|
||||
let theme = theme::load(&cfg);
|
||||
|
||||
|
||||
@@ -220,6 +220,17 @@ impl Spotify {
|
||||
spotify
|
||||
}
|
||||
|
||||
pub fn test_credentials(credentials: Credentials) -> bool {
|
||||
let th = thread::spawn(|| {
|
||||
let mut core = Core::new().unwrap();
|
||||
let config = SessionConfig::default();
|
||||
let handle = core.handle();
|
||||
|
||||
core.run(Session::connect(config, credentials, None, handle)).unwrap();
|
||||
});
|
||||
th.join().is_ok()
|
||||
}
|
||||
|
||||
fn create_session(core: &mut Core, credentials: Credentials) -> Session {
|
||||
let session_config = SessionConfig::default();
|
||||
let cache = Cache::new(config::cache_path("librespot"), true);
|
||||
|
||||
Reference in New Issue
Block a user