From 975ebf027fd78c70398f2c18b31f1a7faaaa40f8 Mon Sep 17 00:00:00 2001 From: Tom McKernan Date: Tue, 3 Sep 2019 11:57:21 -0400 Subject: [PATCH] adding status --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/cli.yml | 12 ++++++-- src/main.rs | 5 ++-- src/subcommands.rs | 71 +++++++++++++++++++++++++++++++++++++--------- 5 files changed, 71 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1d1e60e..4621696 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -49,7 +49,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "pm" -version = "0.2.2" +version = "0.3.0" dependencies = [ "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "json 0.11.15 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 75d2c65..a9ffda3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pm" -version = "0.2.2" +version = "0.3.0" authors = ["Tom McKernan "] edition = "2018" diff --git a/src/cli.yml b/src/cli.yml index 1376c33..3c19992 100644 --- a/src/cli.yml +++ b/src/cli.yml @@ -1,5 +1,5 @@ name: Project Manager -version: 0.2.2 +version: 0.3.0 author: Tom McKernan about: A project manager for your computer subcommands: @@ -41,7 +41,7 @@ subcommands: required: true help: The name of the project to delete - cmd: - about: runs given shell script in project directory. If command doesn't exist you will be prompted for it. + about: runs given command in project directory. If command doesn't exist you will be prompted for it. args: - PROJ_NAME: required: true @@ -50,8 +50,14 @@ subcommands: required: true help: name of the command to run - cmds: - about: lists commands for a given project + about: Lists commands for a given project args: - PROJ_NAME: required: true help: The name of the project + - status: + about: Displays the status of specified repo if none is provided, displays all + args: + - PROJ_NAME: + required: false + help: The name of the project diff --git a/src/main.rs b/src/main.rs index bc02f66..188119b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ mod setup; mod subcommands; use clap::{App, AppSettings, ArgMatches}; -use subcommands::{pull, list, add, delete, clone, cmd, cmds}; +use subcommands::{pull, list, add, delete, clone, cmd, cmds, status}; use setup::SettingsFile; @@ -18,7 +18,6 @@ fn main() { .setting(AppSettings::ArgRequiredElseHelp) .get_matches(); call_subcommands(matches, settings_file) - } fn call_subcommands(matches: ArgMatches, settings_file: SettingsFile) { @@ -36,6 +35,8 @@ fn call_subcommands(matches: ArgMatches, settings_file: SettingsFile) { cmd(matches, settings_file) } else if matches.is_present("cmds") { cmds(matches, settings_file) + } else if matches.is_present("status") { + status(matches, settings_file) } } diff --git a/src/subcommands.rs b/src/subcommands.rs index bff9f9a..0eed7e2 100644 --- a/src/subcommands.rs +++ b/src/subcommands.rs @@ -21,7 +21,7 @@ pub fn clone(matches: ArgMatches, settings_file: SettingsFile) { let mut child = exec_git( vec!["clone", repo_string.as_str(), repo_dir.clone().as_str()], - Option::from(Stdio::piped()), + Option::from(Stdio::inherit()), Option::from(Stdio::inherit()) ); child.wait().unwrap(); settings_file.clone().add_repo(object! { @@ -42,7 +42,7 @@ pub fn pull(matches: ArgMatches, settings_file: SettingsFile) { m["name"].to_string() == repo_name }).unwrap()["path"].to_string(); println!("Pulling: {}", repo_name); - exec_git(vec!["-C", path.as_str(), "pull"], Option::None) + exec_git(vec!["-C", path.as_str(), "pull"], Option::None, Option::None) .wait().unwrap(); println!("Done pulling: {}", repo_name) } else { @@ -51,7 +51,7 @@ pub fn pull(matches: ArgMatches, settings_file: SettingsFile) { println!("Pulling: {}", member["name"].clone().to_string()); child_list.push(( member["name"].to_string(), - exec_git(vec!["-C", member["path"].as_str().unwrap(), "pull"], Option::None) + exec_git(vec!["-C", member["path"].as_str().unwrap(), "pull"], Option::None, Option::None) )) } for child_pair in child_list { @@ -62,17 +62,7 @@ pub fn pull(matches: ArgMatches, settings_file: SettingsFile) { } } -fn exec_git(args: Vec<&str>, io: Option) -> Child { - let io_option = match io { - Some(io) => io, - None => Stdio::null() - }; - Command::new("git") - .args(args) - .stdout(io_option) - .spawn().unwrap() -} pub fn list(matches: ArgMatches, settings_file: SettingsFile) { let repos = settings_file.list_repos(); @@ -172,7 +162,6 @@ pub fn cmds(matches: ArgMatches, settings_file: SettingsFile) { } } - fn create_new_cmd(settings_file: SettingsFile, cmd_name: &str, repo_json: JsonValue) { println!("This command does not exist."); print!("Please enter the command you want to run in this directory: "); @@ -185,3 +174,57 @@ fn create_new_cmd(settings_file: SettingsFile, cmd_name: &str, repo_json: JsonVa settings_file.add_repo(repo_json_mut); } + +pub fn status(matches: ArgMatches, settings_file: SettingsFile) { + let repo_name = matches + .subcommand_matches("status").unwrap() + .value_of("PROJ_NAME"); + + match repo_name { + Some(name) => { + let repo_path = settings_file.get_repo_by_name(name)["path"].clone(); + println!("Status of {} in {}", name, repo_path); + exec_git( + vec![ + "-C", + repo_path.as_str().unwrap(), + "status" + ], + Option::from(Stdio::inherit()), + Option::from(Stdio::inherit()) + ).wait().unwrap(); + + }, + None => { + let repos = settings_file.list_repos(); + for member in repos.members() { + let path = member["path"].as_str().unwrap(); + let name = member["name"].as_str().unwrap(); + let print_string = format!("Status of {} in {}", name, path); + println!("{}\n", print_string); + exec_git( + vec![ + "-C", + path, + "status" + ], + Option::from(Stdio::inherit()), + Option::from(Stdio::inherit()) + ).wait().unwrap(); + println!("{}", "#".repeat(50)); + } + } + }; + +} + +fn exec_git(args: Vec<&str>, stdio: Option, stderr: Option) -> Child { + let stdio_option = stdio.unwrap_or(Stdio::null()); + let stderr_option = stderr.unwrap_or(Stdio::null()); + + Command::new("git") + .args(args) + .stdout(stdio_option) + .stderr(stderr_option) + .spawn().unwrap() +}