Skip to content

Commit e30a64e

Browse files
committed
Make the parsed Clap config static.
1 parent 4e08f44 commit e30a64e

3 files changed

Lines changed: 35 additions & 20 deletions

File tree

Cargo.lock

Lines changed: 14 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ is-it-maintained-open-issues = { repository = "arusahni/git-req" }
2121
maintenance = {status = "actively-developed"}
2222

2323
[dependencies]
24+
lazy_static = "1.4.0"
2425
regex = "0.2"
2526
serde = "1.0"
2627
serde_json = "1.0"

src/main.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,24 @@
22
mod git;
33
mod remotes;
44

5-
use clap::{crate_authors, crate_version, load_yaml, App, AppSettings, ArgMatches};
5+
use clap::{crate_authors, crate_version, App, AppSettings, ArgMatches, YamlLoader};
66
use colored::*;
77
use git2::ErrorCode;
8+
use lazy_static::lazy_static;
89
use log::{debug, error, info, trace};
910
use std::io::{self, stdin, stdout, Cursor, Write};
10-
use std::{env, process};
11+
use std::{env, include_str, process};
1112
use tabwriter::TabWriter;
13+
use yaml_rust::Yaml;
14+
15+
lazy_static! {
16+
static ref APP_CFG: Yaml = {
17+
YamlLoader::load_from_str(include_str!("../cli-flags.yml"))
18+
.expect("Failed to load CLI config")
19+
.pop()
20+
.unwrap()
21+
};
22+
}
1223

1324
/// Get the remote url
1425
fn get_remote_url(remote_name: &str) -> String {
@@ -171,6 +182,7 @@ fn generate_completion(app: &mut App, shell_name: &str) {
171182
print!("{}", &output);
172183
}
173184

185+
/// Get the name of the remote to use for the operation
174186
fn get_remote_name(matches: &ArgMatches) -> String {
175187
let default_remote_name = match git::get_project_config("defaultremote") {
176188
Some(remote_name) => remote_name,
@@ -207,8 +219,9 @@ fn get_remote_name(matches: &ArgMatches) -> String {
207219
)
208220
}
209221

210-
fn build_cli(cfg: &yaml_rust::Yaml) -> App {
211-
App::from_yaml(&cfg)
222+
/// Get the Clap app for CLI matching et al.
223+
fn build_cli<'a>() -> App<'a, 'a> {
224+
App::from_yaml(&APP_CFG)
212225
.version(crate_version!())
213226
.author(crate_authors!("\n"))
214227
.setting(AppSettings::ArgsNegateSubcommands)
@@ -222,8 +235,8 @@ fn main() {
222235
.parse_filters(&env::var("REQ_LOG").unwrap_or_default())
223236
.try_init();
224237

225-
let cfg = load_yaml!("../cli-flags.yml");
226-
let app = build_cli(&cfg);
238+
let mut app = build_cli();
239+
227240
let matches = app.get_matches();
228241

229242
if let Some(project_id) = matches.value_of("NEW_PROJECT_ID") {
@@ -239,7 +252,7 @@ fn main() {
239252
} else if let Some(remote_name) = matches.value_of("NEW_DEFAULT_REMOTE") {
240253
set_default_remote(remote_name);
241254
} else if let Some(shell_name) = matches.value_of("GENERATE_COMPLETIONS") {
242-
let mut app = build_cli(&cfg);
255+
app = build_cli();
243256
generate_completion(&mut app, &shell_name);
244257
} else {
245258
checkout_mr(

0 commit comments

Comments
 (0)