-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP:
- Loading branch information
Showing
19 changed files
with
933 additions
and
353 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,50 @@ | ||
mod pr; | ||
mod profile; | ||
|
||
use clap::{crate_version, crate_authors, App, Arg}; | ||
use crate::logger::Logger; | ||
use anyhow::{Result, anyhow}; | ||
use anyhow::Result; | ||
use clap::{crate_authors, crate_version, App, AppSettings, Arg}; | ||
use log::debug; | ||
|
||
|
||
pub fn get_app<'a, 'b>() -> App<'a, 'b> { | ||
App::new("yag") | ||
.version(crate_version!()) | ||
.author(crate_authors!()) | ||
.arg( | ||
Arg::with_name("v") | ||
.short("v") | ||
.long("verbose") | ||
.help("verbose mode") | ||
.global(true), | ||
) | ||
.subcommand(pr::sub_command()) | ||
.subcommand(profile::sub_command()) | ||
App::new("yag") | ||
.version(crate_version!()) | ||
.author(crate_authors!()) | ||
.global_setting(AppSettings::ColoredHelp) | ||
.global_setting(AppSettings::DisableHelpSubcommand) | ||
.global_setting(AppSettings::DontCollapseArgsInUsage) | ||
.global_setting(AppSettings::VersionlessSubcommands) | ||
.arg( | ||
Arg::with_name("v") | ||
.short("v") | ||
.long("verbose") | ||
.help("verbose mode") | ||
.global(true), | ||
) | ||
.subcommand(pr::sub_command().setting(AppSettings::SubcommandRequiredElseHelp)) | ||
.subcommand(profile::sub_command().setting(AppSettings::SubcommandRequiredElseHelp)) | ||
} | ||
|
||
|
||
pub async fn run() -> Result<()> { | ||
let mut app = get_app(); | ||
|
||
let matches = app.clone().get_matches(); | ||
|
||
Logger::init(matches.is_present("v"))?; | ||
debug!("verbose mode enabled"); | ||
|
||
let (command, arg_matches) = matches.subcommand(); | ||
debug!("command: {}", command); | ||
let arg_matches = arg_matches.ok_or(anyhow!("arg matches is none"))?; | ||
debug!("arg matches: {:#?}", arg_matches); | ||
match command { | ||
"pr" => pr::Command::new(&arg_matches).run().await, | ||
"profile" => profile::Command::new(&arg_matches).run().await, | ||
_ => Ok(app.write_long_help(&mut std::io::stdout())?), | ||
if let (command, Some(arg_matches)) = matches.subcommand() { | ||
debug!("command: {}", command); | ||
debug!("arg matches: {:#?}", arg_matches); | ||
match command { | ||
"pr" => pr::Command::new(&arg_matches).unwrap().run().await?, | ||
"profile" => profile::Command::new(&arg_matches).unwrap().run().await?, | ||
_ => (), | ||
} | ||
} else { | ||
app.print_long_help()?; | ||
println!(); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,43 @@ | ||
use clap::{App, ArgMatches, SubCommand}; | ||
use crate::profile::{load_profile, prompt_add_profile, write_profile}; | ||
use anyhow::Result; | ||
use crate::profile::{load_profile, prompt_add_profile}; | ||
use clap::{App, ArgMatches, SubCommand}; | ||
use log::debug; | ||
|
||
#[inline] | ||
pub fn sub_command<'a, 'b>() -> App<'a, 'b> { | ||
SubCommand::with_name("profile") | ||
.about("Manage profiles") | ||
.subcommand( | ||
SubCommand::with_name("add") | ||
.about("Add profile config interactively"), | ||
) | ||
.subcommand(SubCommand::with_name("add").about("Add profile config interactively")) | ||
} | ||
pub struct Command<'a> { | ||
command: &'a str, | ||
matches: &'a ArgMatches<'a>, | ||
} | ||
|
||
impl<'a> Command<'a> { | ||
pub fn new(matches: &'a ArgMatches<'a>) -> Self { | ||
Command { | ||
matches: matches, | ||
pub fn new(matches: &'a ArgMatches<'a>) -> Option<Self> { | ||
match matches.subcommand() { | ||
(command, Some(arg_matches)) => Some(Command { | ||
command: command, | ||
matches: arg_matches, | ||
}), | ||
_ => None, | ||
} | ||
} | ||
|
||
pub async fn run(&self) -> Result<()> { | ||
let (command, _) = self.matches.subcommand(); | ||
match command { | ||
match self.command { | ||
"add" => self.add().await, | ||
_ => Ok(println!("{}", self.matches.usage())), | ||
} | ||
} | ||
|
||
async fn add(&self) -> Result<()> { | ||
let mut profile = load_profile().await?; | ||
debug!("profile loaded: {:#?}", profile); | ||
prompt_add_profile(&mut profile).await?; | ||
debug!("profile added: {:#?}", profile); | ||
write_profile(&profile).await?; | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.