Skip to content

Commit f1c7abd

Browse files
authored
Merge pull request #12 from omarahm3/feat/global-git-commands
feat(all): run git command on all project repositories
2 parents 92a8c18 + 535aacc commit f1c7abd

File tree

7 files changed

+89
-7
lines changed

7 files changed

+89
-7
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ repository = "https://github.com/omarahm3/switcher"
66
readme = "README.md"
77
license = "MIT"
88
keywords = ["microservices", "git", "multi-projects"]
9-
version = "0.0.7-alpha"
9+
version = "0.0.8-alpha"
1010
authors = ["Omar Ahmed <[email protected]>"]
1111
edition = "2021"
1212
rust-version ="1.57"

src/commands/all.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
use colored::*;
2+
use std::process::exit;
3+
4+
use crate::core::{cli::ProgramInfo, config, git};
5+
6+
fn help() {
7+
println!(
8+
"
9+
This will let you run git commands on all of your registered project repositories
10+
11+
Examples:
12+
switcher all help
13+
switcher all <PROJECT_NAME> stash clear
14+
"
15+
);
16+
}
17+
18+
pub fn run_git(program: ProgramInfo) {
19+
let args = match program.args {
20+
None => {
21+
println!("Command must be: 'switcher all <PROJECT_INFO> <GIT_COMMAND>' run 'help' to check examples");
22+
exit(1);
23+
}
24+
Some(args) => args,
25+
};
26+
27+
let mut options = args.iter();
28+
let project_name = match options.next() {
29+
None => "help",
30+
Some(sub_command) => sub_command,
31+
};
32+
33+
if project_name == "help" {
34+
help();
35+
exit(0);
36+
}
37+
38+
println!("Project Name: {}", project_name);
39+
40+
let git_command = options.as_slice().to_vec();
41+
42+
if git_command.is_empty() {
43+
println!("Command must be: 'switcher all <PROJECT_INFO> <GIT_COMMAND>' run 'help' to check examples");
44+
exit(1);
45+
}
46+
let command_str: String = git_command.join(" ");
47+
// Convert Vec<String> to Vec<&str> for git::run_command
48+
let git_command: Vec<&str> = git_command.iter().map(String::as_ref).collect();
49+
50+
let mut config = config::get();
51+
let project = match config.get_project(project_name) {
52+
None => {
53+
println!(
54+
"Unable to find project [{}] please consider running 'add_project {} <|PATH>'",
55+
&project_name, &project_name
56+
);
57+
exit(1);
58+
}
59+
Some(project) => project,
60+
};
61+
62+
let repositories = &project.repositories;
63+
64+
for repository in repositories {
65+
println!("!> Running {}", command_str);
66+
67+
let (err, output) = git::run_command("git".to_string(), git_command.to_vec(), repository);
68+
69+
if !err.is_empty() {
70+
println!("{}", err.red());
71+
}
72+
73+
if !output.is_empty() {
74+
println!("{}", output.blue());
75+
}
76+
}
77+
}

src/commands/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod all;
12
pub mod help;
23
pub mod projects;
34
pub mod types;

src/commands/projects.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ pub fn sync_projects(program: ProgramInfo) {
121121
let project = match config.get_project(&params.name) {
122122
None => {
123123
println!(
124-
"Unable to find project [{}] please consider runnnig 'add_project {} <|PATH>'",
124+
"Unable to find project [{}] please consider running 'add_project {} <|PATH>'",
125125
&params.name, &params.name
126126
);
127127
exit(1);
@@ -154,7 +154,7 @@ pub fn setup(program: ProgramInfo) {
154154
let mut project = match config.get_project(&project_name) {
155155
None => {
156156
println!(
157-
"Unable to find project [{}] please consider runnnig 'add_project {} <|PATH>'",
157+
"Unable to find project [{}] please consider running 'add_project {} <|PATH>'",
158158
&project_name, &project_name
159159
);
160160
exit(1);

src/core/cli.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub enum CliCommand {
1111
Project,
1212
Version,
1313
Feature,
14+
All,
1415
}
1516

1617
impl CliCommand {
@@ -23,6 +24,7 @@ impl CliCommand {
2324
"branch" => CliCommand::Branch,
2425
"version" => CliCommand::Version,
2526
"feature" => CliCommand::Feature,
27+
"all" => CliCommand::All,
2628
_ => CliCommand::Help,
2729
}
2830
}

src/core/git.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ fn build_command(command: &str) -> (String, Vec<&str>) {
9090
(command.to_string(), args.to_vec())
9191
}
9292

93-
fn run_command(command: String, args: Vec<&str>, cwd: &Path) -> (String, String) {
93+
pub fn run_command(command: String, args: Vec<&str>, cwd: &Path) -> (String, String) {
9494
let command = match Command::new(command).current_dir(cwd).args(args).output() {
9595
Err(err) => panic!("Error running command [{}]", err),
9696
Ok(cmd) => cmd,

src/main.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
mod commands;
22
mod core;
33

4-
use crate::commands::help;
5-
use crate::commands::projects;
6-
use crate::commands::version;
74
use crate::core::cli::get_program_info;
85
use crate::core::cli::CliCommand;
96
use crate::core::config;
7+
use commands::all;
8+
use commands::help;
9+
use commands::projects;
10+
use commands::version;
1011

1112
fn main() {
1213
config::init();
@@ -21,5 +22,6 @@ fn main() {
2122
CliCommand::Project => projects::check(program_info),
2223
CliCommand::Version => version::print_version(),
2324
CliCommand::Feature => projects::sync_feature(program_info),
25+
CliCommand::All => all::run_git(program_info),
2426
}
2527
}

0 commit comments

Comments
 (0)