-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlib.rs
116 lines (102 loc) · 3.43 KB
/
lib.rs
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
//! Command for including co-authors in Git commits when collaborating on code.
#![forbid(unsafe_code)]
use std::error::Error;
use std::path::PathBuf;
use git2::Repository;
use structopt::StructOpt;
mod active_copirate;
mod completions;
mod copirate;
mod embark;
mod prepare_commit_msg;
mod sail;
mod solo;
pub type BoxResult<T> = Result<T, Box<dyn Error>>;
pub const HOOK_PATH: &str = ".git/hooks/prepare-commit-msg";
#[derive(StructOpt, Clone, Debug)]
#[structopt(name = "Rmob", version = "0.1.0", author = "")]
struct Rmob {
#[structopt(
default_value = ".git-copirates",
env = "GIT_COPIRATES_FILE",
long = "git-copirates-file"
)]
git_copirates_file: String,
#[structopt(subcommand)] // Note that we mark a field as a subcommand
cmd: Command,
}
#[derive(StructOpt, Clone, Debug)]
enum CopirateSubcommand {
/// Adds a co-pirate to the list
#[structopt(name = "enlist", alias = "add")]
Add {
alias: String,
name: String,
email: String,
},
/// Removes co-pirate from the list
#[structopt(name = "keelhaul", alias = "delete", alias = "remove")]
Remove { alias: String },
}
#[derive(StructOpt, Clone, Debug)]
enum Command {
/// Embark on rmob fer this git repo, call this once t' use rmob in yer git repo
#[structopt(name = "embark")]
Embark,
/// Start pairin' or mobbin' by passin' a list of yer co-pirates te sail wit'
// TODO: Accept only two-character input
#[structopt(name = "sail")]
Sail { copirates: Vec<String> },
/// Sail solo (short fer `rmob sail solo`)
#[structopt(name = "solo")]
Solo,
/// Edit copirates list
#[structopt(name = "copirate")]
Copirate {
#[structopt(subcommand)]
cmd: CopirateSubcommand,
},
/// Generates autocompletion config for shell
#[structopt(name = "generate-shell-completions")]
GenerateShellCompletions { shell: String },
/// Called from the git hook only
#[structopt(name = "prepare-commit-msg")]
PrepareCommitMessage {
#[structopt(parse(from_os_str))]
commit_message_file: PathBuf,
},
}
/// Executes the `rmob` application.
pub fn run() -> BoxResult<()> {
let rmob = Rmob::from_args();
let copirates_file = rmob.git_copirates_file;
let repo = Repository::discover(".")?;
let repo_dir = repo.workdir().ok_or("You're ON LAND, stupid.")?;
match rmob.cmd {
Command::Embark => embark::embark(repo_dir)?,
Command::Solo => solo::solo(repo_dir)?,
Command::Sail { ref copirates } if copirates == &["solo"] => solo::solo(repo_dir)?,
Command::Sail { ref copirates } => sail::sail(&copirates_file, copirates, repo_dir)?,
Command::Copirate { ref cmd } => {
match cmd {
CopirateSubcommand::Add {
ref alias,
ref name,
ref email,
} => copirate::add(&copirates_file, alias, name, email)?,
CopirateSubcommand::Remove { ref alias } => {
copirate::remove(&copirates_file, alias)?
}
};
}
Command::GenerateShellCompletions { ref shell } => {
completions::generate(&mut Rmob::clap(), shell)?
}
Command::PrepareCommitMessage {
commit_message_file,
} => {
prepare_commit_msg::inject_into_commit_message_file(&commit_message_file, repo_dir)?;
}
}
Ok(())
}