aboutsummaryrefslogtreecommitdiffstats
path: root/src/config.rs
blob: d8732d415d2d4851b0c96afd363054fe52b821ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright (c) 2020  Teddy Wing
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.


use std::env;
use std::process;

use getopts::{self, Options};
use git2::{self, Repository};
use thiserror::Error;

use crate::owner_repo::{self, OwnerRepo};
use crate::VERSION;


/// Program-specific prefix for Git config values.
const GIT_CONFIG_PREFIX: &'static str = "githubSuggestion.";

/// Configuration errors.
#[derive(Debug, Error)]
pub enum Error {
    #[error("Unable to parse arguments: {0}")]
    Opts(#[from] getopts::Fail),

    #[error("Error getting environment variable '{var}'")]
    EnvVar {
        source: env::VarError,
        var: String,
    },

    #[error(transparent)]
    OwnerRepo(#[from] owner_repo::Error),

    #[error(transparent)]
    Git(#[from] git2::Error),
}

/// Configuration extracted from config files and command line arguments.
pub struct Config {
    pub github_token: String,
    pub o_r: Result<OwnerRepo, owner_repo::Error>,
    pub suggestions: Vec<String>,
}

impl Config {
    /// Set up command line arguments. Extract configuration values from command
    /// line arguments, Git config, and environment variables.
    pub fn get(args: &[String], usage_brief: &str) -> Result<Self, Error> {
        let mut opts = Options::new();

        opts.optopt(
            "",
            "github-token",
            r#"GitHub API token with "repo" permission"#,
            "TOKEN",
        );
        opts.optopt(
            "",
            "remote",
            "remote name, defaults to 'origin'",
            "REMOTE",
        );
        opts.optflag("h", "help", "print this help menu");
        opts.optflag("V", "version", "show the program version");

        let opt_matches = opts.parse(&args[1..])?;

        if opt_matches.opt_present("h") {
            print_usage(&opts, usage_brief);

            process::exit(exitcode::USAGE);
        }

        if opt_matches.opt_present("V") {
            println!("{}", VERSION);

            process::exit(exitcode::OK);
        }

        if opt_matches.free.is_empty() {
            print_usage(&opts, usage_brief);

            process::exit(exitcode::USAGE);
        }

        let git_config = Repository::open(".")?.config()?;

        let o_r = OwnerRepo::from_remote(
            Self::remote(&opt_matches, &git_config)?.as_deref(),
        );

        Ok(Config {
            github_token: Self::github_token(&opt_matches, &git_config)?,
            o_r: o_r,
            suggestions: opt_matches.free,
        })
    }

    /// Get a GitHub token, checking the following places in order:
    ///
    /// 1. Command line argument
    /// 2. Git config
    /// 3. Environment variable
    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 => {
                        let key = "GITHUB_TOKEN";

                        env::var(key)
                            .map_err(|e| Error::EnvVar {
                                source: e,
                                var: key.to_owned(),
                            })
                    },
                    r => r.map_err(|e| Error::Git(e)),
                },
        }
    }

    /// Get the Git remote name from the following places in order:
    ///
    /// 1. Command line argument
    /// 2. Git config
    ///
    /// If the value wasn't set, return `Ok(None)`.
    fn remote(
        opt_matches: &getopts::Matches,
        git_config: &git2::Config,
    ) -> Result<Option<String>, git2::Error> {
        match opt_matches.opt_str("remote") {
            Some(r) => Ok(Some(r)),
            None => match git_config.get_string(&git_config_key("remote")) {
                Err(e) if e.code() == git2::ErrorCode::NotFound => Ok(None),
                r => r.map(|r| Some(r)),
            },
        }
    }
}

/// Print command line usage information to standard output.
fn print_usage(opts: &Options, brief: &str) {
    print!("{}", opts.usage(brief));
}

/// Build a Git config key using the program-specific prefix and a subkey.
fn git_config_key(key: &str) -> String {
    format!("{}.{}", GIT_CONFIG_PREFIX, key)
}