Merge pull request #143 from SimonPersson/general_main_helper

Improve for library users
This commit is contained in:
Paul Lietar 2017-01-06 17:32:28 +01:00 committed by GitHub
commit ae7c177823
4 changed files with 36 additions and 35 deletions

View file

@ -6,13 +6,16 @@ use crypto::hmac::Hmac;
use crypto::pbkdf2::pbkdf2; use crypto::pbkdf2::pbkdf2;
use crypto::sha1::Sha1; use crypto::sha1::Sha1;
use protobuf::ProtobufEnum; use protobuf::ProtobufEnum;
use rpassword;
use serde; use serde;
use serde_json; use serde_json;
use std::io::{self, Read, Write}; use std::io::{self, stderr, Read, Write};
use std::fs::File; use std::fs::File;
use std::path::Path; use std::path::Path;
use rustc_serialize::base64::{self, FromBase64, ToBase64}; use rustc_serialize::base64::{self, FromBase64, ToBase64};
use session::Session;
use protocol::authentication::AuthenticationType; use protocol::authentication::AuthenticationType;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -170,3 +173,31 @@ fn deserialize_base64<D>(de: &mut D) -> Result<Vec<u8>, D::Error>
mod discovery; mod discovery;
pub use self::discovery::discovery_login; pub use self::discovery::discovery_login;
pub fn get_credentials(session: &Session, username: Option<String>, password: Option<String>) -> Credentials {
let credentials = session.cache().get_credentials();
match (username, password, credentials) {
(Some(username), Some(password), _)
=> Credentials::with_password(username, password),
(Some(ref username), _, Some(ref credentials)) if *username == credentials.username
=> credentials.clone(),
(Some(username), None, _) => {
write!(stderr(), "Password for {}: ", username).unwrap();
stderr().flush().unwrap();
let password = rpassword::read_password().unwrap();
Credentials::with_password(username.clone(), password)
}
(None, _, Some(credentials))
=> credentials,
(None, _, None) => {
info!("No username provided and no stored credentials, starting discovery ...");
discovery_login(&session.config().device_name, session.device_id()).unwrap()
}
}
}

View file

@ -8,6 +8,7 @@ use std::thread;
use librespot::spirc::SpircManager; use librespot::spirc::SpircManager;
use librespot::main_helper; use librespot::main_helper;
use librespot::authentication::get_credentials;
fn usage(program: &str, opts: &getopts::Options) -> String { fn usage(program: &str, opts: &getopts::Options) -> String {
let brief = format!("Usage: {} [options]", program); let brief = format!("Usage: {} [options]", program);
@ -33,7 +34,8 @@ fn main() {
main_helper::setup_logging(&matches); main_helper::setup_logging(&matches);
let session = main_helper::create_session(&matches); let session = main_helper::create_session(&matches);
let credentials = main_helper::get_credentials(&session, &matches); let credentials = get_credentials(&session, matches.opt_str("username"),
matches.opt_str("password"));
session.login(credentials).unwrap(); session.login(credentials).unwrap();
let player = main_helper::create_player(&session, &matches); let player = main_helper::create_player(&session, &matches);

View file

@ -1,13 +1,10 @@
use env_logger::LogBuilder; use env_logger::LogBuilder;
use getopts; use getopts;
use rpassword;
use std::env; use std::env;
use std::io::{stderr, Write};
use std::path::PathBuf; use std::path::PathBuf;
use std::process::exit; use std::process::exit;
use audio_backend::{BACKENDS, Sink}; use audio_backend::{BACKENDS, Sink};
use authentication::{Credentials, discovery_login};
use cache::{Cache, DefaultCache, NoCache}; use cache::{Cache, DefaultCache, NoCache};
use player::Player; use player::Player;
use session::{Bitrate, Config, Session}; use session::{Bitrate, Config, Session};
@ -92,36 +89,6 @@ pub fn create_session(matches: &getopts::Matches) -> Session {
Session::new(config, cache) Session::new(config, cache)
} }
pub fn get_credentials(session: &Session, matches: &getopts::Matches) -> Credentials {
let credentials = session.cache().get_credentials();
match (matches.opt_str("username"),
matches.opt_str("password"),
credentials) {
(Some(username), Some(password), _)
=> Credentials::with_password(username, password),
(Some(ref username), _, Some(ref credentials)) if *username == credentials.username
=> credentials.clone(),
(Some(username), None, _) => {
write!(stderr(), "Password for {}: ", username).unwrap();
stderr().flush().unwrap();
let password = rpassword::read_password().unwrap();
Credentials::with_password(username.clone(), password)
}
(None, _, Some(credentials))
=> credentials,
(None, _, None) => {
info!("No username provided and no stored credentials, starting discovery ...");
discovery_login(&session.config().device_name, session.device_id()).unwrap()
}
}
}
pub fn create_player(session: &Session, matches: &getopts::Matches) -> Player { pub fn create_player(session: &Session, matches: &getopts::Matches) -> Player {
let backend_name = matches.opt_str("backend"); let backend_name = matches.opt_str("backend");
let device_name = matches.opt_str("device"); let device_name = matches.opt_str("device");

View file

@ -28,6 +28,7 @@ use version;
use stream; use stream;
#[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq)]
pub enum Bitrate { pub enum Bitrate {
Bitrate96, Bitrate96,
Bitrate160, Bitrate160,