aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTeddy Wing2020-08-01 20:08:54 +0200
committerTeddy Wing2020-08-01 20:09:49 +0200
commited17b139448c2c0ecc7b7f02f11a6b57939f8f90 (patch)
tree9b644a9ac5761d042d49e576b1b191e2e1ad5429 /src
parent58b81567354cf6c0bd3623e86deb7b6609b1f3be (diff)
downloadgit-suggestion-ed17b139448c2c0ecc7b7f02f11a6b57939f8f90.tar.bz2
Config: Move `github_token` to a function
Improve organisation.
Diffstat (limited to 'src')
-rw-r--r--src/config.rs33
1 files changed, 19 insertions, 14 deletions
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<String, Error> {
+ 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 {