27
src/main.rs
27
src/main.rs
@@ -98,12 +98,23 @@ fn setup_logging(filename: &str) -> Result<(), fern::InitError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn credentials_prompt(reset: bool) -> Credentials {
|
||||
fn credentials_prompt(reset: bool, error_message: Option<String>) -> Credentials {
|
||||
let path = config::config_path("credentials.toml");
|
||||
if reset && fs::remove_file(&path).is_err() {
|
||||
error!("could not delete credential file");
|
||||
}
|
||||
|
||||
if let Some(message) = error_message {
|
||||
let mut siv = cursive::default();
|
||||
let dialog = cursive::views::Dialog::around(cursive::views::TextView::new(format!(
|
||||
"Connection error:\n{}",
|
||||
message
|
||||
)))
|
||||
.button("Ok", |s| s.quit());
|
||||
siv.add_layer(dialog);
|
||||
siv.run();
|
||||
}
|
||||
|
||||
let creds =
|
||||
crate::config::load_or_generate_default(&path, authentication::create_credentials, true)
|
||||
.unwrap_or_else(|e| {
|
||||
@@ -184,12 +195,20 @@ fn main() {
|
||||
info!("Using cached credentials");
|
||||
c
|
||||
}
|
||||
None => credentials_prompt(false),
|
||||
None => credentials_prompt(false, None),
|
||||
}
|
||||
};
|
||||
|
||||
while !spotify::Spotify::test_credentials(credentials.clone()) {
|
||||
credentials = credentials_prompt(true);
|
||||
while let Err(error) = spotify::Spotify::test_credentials(credentials.clone()) {
|
||||
let reset = error
|
||||
.get_ref()
|
||||
.map_or(false, |err| err.to_string().contains("BadCredentials"));
|
||||
debug!("credential reset: {:?}", reset);
|
||||
let error_msg = match error.get_ref() {
|
||||
Some(inner) => inner.to_string(),
|
||||
None => error.to_string(),
|
||||
};
|
||||
credentials = credentials_prompt(reset, Some(error_msg));
|
||||
}
|
||||
|
||||
let theme = theme::load(&cfg);
|
||||
|
||||
@@ -44,12 +44,12 @@ use url::Url;
|
||||
|
||||
use core::task::Poll;
|
||||
|
||||
use std::env;
|
||||
use std::pin::Pin;
|
||||
use std::sync::atomic::{AtomicU16, Ordering};
|
||||
use std::sync::RwLock;
|
||||
use std::thread;
|
||||
use std::time::{Duration, SystemTime};
|
||||
use std::{env, io};
|
||||
|
||||
use crate::artist::Artist;
|
||||
use crate::config;
|
||||
@@ -329,15 +329,23 @@ impl Spotify {
|
||||
session_config
|
||||
}
|
||||
|
||||
pub fn test_credentials(credentials: Credentials) -> bool {
|
||||
let th = thread::spawn(move || {
|
||||
pub fn test_credentials(credentials: Credentials) -> Result<Session, std::io::Error> {
|
||||
let jh = thread::spawn(move || {
|
||||
let mut core = Core::new().unwrap();
|
||||
let config = Self::session_config();
|
||||
let handle = core.handle();
|
||||
|
||||
core.run(Session::connect(config, credentials, None, handle))
|
||||
});
|
||||
th.join().is_ok()
|
||||
match jh.join() {
|
||||
Ok(session) => session.or_else(Err),
|
||||
Err(e) => Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
e.downcast_ref::<String>()
|
||||
.unwrap_or(&"N/A".to_string())
|
||||
.to_string(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn create_session(core: &mut Core, cfg: &config::Config, credentials: Credentials) -> Session {
|
||||
|
||||
Reference in New Issue
Block a user