Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: pass request token as a flag instead of an argument #19

Merged
merged 1 commit into from Nov 17, 2018
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Some generated files are not rendered by default. Learn more.

@@ -4,6 +4,7 @@ version = "0.1.0"
authors = ["0xazure <stuart.crust@gmail.com>"]

[dependencies]
clap = "~2.32.0"
reqwest = "0.8.7"
serde = "1.0"
serde_json = "1.0"
@@ -1,12 +1,13 @@
extern crate chrono;
extern crate clap;
extern crate reqwest;
extern crate serde_derive;
extern crate serde_json;

use chrono::{DateTime, Utc};
use reqwest::header::{qitem, Accept, Authorization, Bearer, Link, RelationType, UserAgent};
use serde_derive::Deserialize;
use std::{env, error, fmt, mem};
use std::{error, fmt, mem};

#[derive(Debug)]
pub struct Config {
@@ -15,19 +16,6 @@ pub struct Config {
}

impl Config {
pub fn new(mut args: env::Args) -> Result<Config, &'static str> {
args.next();

let username = match args.next() {
None => return Err("No username provided"),
Some(arg) => arg,
};

let token = args.next();

Ok(Config { username, token })
}

fn url(self) -> Option<String> {
Some(format!(
"https://api.github.com/users/{}/starred",
@@ -36,6 +24,15 @@ impl Config {
}
}

impl<'a> From<clap::ArgMatches<'a>> for Config {
fn from(matches: clap::ArgMatches) -> Self {
Config {
username: matches.value_of("USERNAME").unwrap().to_owned(),
token: matches.value_of("TOKEN").map(String::from),
}
}
}

#[derive(Debug)]
struct ClientBuilder {
inner: reqwest::ClientBuilder,
@@ -1,13 +1,25 @@
extern crate clap;
extern crate supernova;

use std::{env, process};
use clap::{App, Arg, crate_name, crate_version};
use std::process;
use supernova::Config;

fn main() {
let config = Config::new(env::args()).unwrap_or_else(|err| {
println!("Problem parsing arguments: {}", err);
process::exit(1);
});
let config: Config = App::new(crate_name!())
.version(crate_version!())
.arg(
Arg::with_name("USERNAME")
.help("The user whose stars to collect")
.required(true),
).arg(
Arg::with_name("TOKEN")
.short("t")
.long("token")
.help("Sets the authentication token for requests to GitHub")
.takes_value(true),
).get_matches()
.into();

if let Err(e) = supernova::collect_stars(config) {
println!("Application error: {}", e);
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.