From ed17b139448c2c0ecc7b7f02f11a6b57939f8f90 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 1 Aug 2020 20:08:54 +0200 Subject: Config: Move `github_token` to a function Improve organisation. --- src/config.rs | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/config.rs b/src/config.rs index 321ea50..615c0d4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,7 +1,7 @@ use std::env; -use getopts::Options; -use git2::Repository; +use getopts::{self, Options}; +use git2::{self, Repository}; use thiserror::Error; use crate::owner_repo::{self, OwnerRepo}; @@ -43,17 +43,6 @@ impl Config { let git_config = Repository::open(".")?.config()?; - let github_token = match opt_matches.opt_str("github-token") { - Some(t) => Ok(t), - None => - match git_config.get_string(&git_config_key("githubToken")) { - Err(e) if e.code() == git2::ErrorCode::NotFound => - env::var("GITHUB_TOKEN") - .map_err(|e| Error::EnvVar(e)), - r => r.map_err(|e| Error::Git(e)), - }, - }?; - let remote = match opt_matches.opt_str("remote") { Some(r) => Ok(Some(r)), None => match git_config.get_string(&git_config_key("remote")) { @@ -65,11 +54,27 @@ impl Config { let o_r = OwnerRepo::from_remote(remote.as_deref())?; Ok(Config { - github_token, + github_token: Self::github_token(&opt_matches, &git_config)?, owner: o_r.owner, repo: o_r.repo, }) } + + fn github_token( + opt_matches: &getopts::Matches, + git_config: &git2::Config, + ) -> Result { + match opt_matches.opt_str("github-token") { + Some(t) => Ok(t), + None => + match git_config.get_string(&git_config_key("githubToken")) { + Err(e) if e.code() == git2::ErrorCode::NotFound => + env::var("GITHUB_TOKEN") + .map_err(|e| Error::EnvVar(e)), + r => r.map_err(|e| Error::Git(e)), + }, + } + } } fn git_config_key(key: &str) -> String { -- cgit v1.2.3