diff --git a/README.md b/README.md index 500e62f..de34321 100644 --- a/README.md +++ b/README.md @@ -35,11 +35,11 @@ extern crate elefren; ```rust,no_run extern crate elefren; -use std::io; use std::error::Error; use elefren::prelude::*; use elefren::helpers::toml; // requires `features = ["toml"]` +use elefren::helpers::cli; fn main() -> Result<(), Box> { let mastodon = if let Ok(data) = toml::from_file("mastodon-data.toml") { @@ -59,16 +59,7 @@ fn register() -> Result> { let registration = Registration::new("https://mastodon.social") .client_name("elefren-examples") .build()?; - let url = registration.authorize_url()?; - - println!("Click this link to authorize on Mastodon: {}", url); - println!("Paste the returned authorization code: "); - - let mut input = String::new(); - io::stdin().read_line(&mut input)?; - - let code = input.trim().to_string(); - let mastodon = registration.complete(&code)?; + let mastodon = cli::authenticate(registration)?; // Save app data for using on the next run. toml::to_file(&*mastodon, "mastodon-data.toml")?; diff --git a/examples/register.rs b/examples/register.rs index 7b581ee..d6a2275 100644 --- a/examples/register.rs +++ b/examples/register.rs @@ -6,6 +6,7 @@ pub use self::elefren::prelude::*; use std::{error::Error, io}; +use self::elefren::helpers::cli; #[cfg(feature = "toml")] use self::elefren::helpers::toml; @@ -34,12 +35,7 @@ pub fn register() -> Result> { .scopes(Scopes::all()) .website("https://github.com/pwoolcoc/elefren") .build()?; - let url = registration.authorize_url()?; - - println!("Click this link to authorize on Mastodon: {}", url); - let code = read_line("Paste the returned authorization code: ")?; - - let mastodon = registration.complete(&code)?; + let mastodon = cli::authenticate(registration)?; // Save app data for using on the next run. toml::to_file(&*mastodon, "mastodon-data.toml")?; diff --git a/src/helpers/cli.rs b/src/helpers/cli.rs new file mode 100644 index 0000000..a1321d5 --- /dev/null +++ b/src/helpers/cli.rs @@ -0,0 +1,27 @@ +use std::io::{self, BufRead, Write}; + +use errors::Result; +use http_send::HttpSend; +use registration::Registered; +use Mastodon; + +/// Finishes the authentication process for the given `Registered` object, +/// using the command-line +pub fn authenticate(registration: Registered) -> Result> { + let url = registration.authorize_url()?; + + let stdout = io::stdout(); + let stdin = io::stdin(); + + let mut stdout = stdout.lock(); + let mut stdin = stdin.lock(); + + writeln!(&mut stdout, "Click this link to authorize: {}", url)?; + write!(&mut stdout, "Paste the returned authorization code: ")?; + stdout.flush()?; + + let mut input = String::new(); + stdin.read_line(&mut input)?; + let code = input.trim(); + Ok(registration.complete(code)?) +} diff --git a/src/helpers/mod.rs b/src/helpers/mod.rs index 82e7d7b..a982938 100644 --- a/src/helpers/mod.rs +++ b/src/helpers/mod.rs @@ -21,3 +21,6 @@ pub mod toml; /// features = ["json"] /// ``` pub mod json; + +/// Helpers for working with the command line +pub mod cli;