-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathcli.rs
247 lines (213 loc) · 8.53 KB
/
cli.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
use clap::{Parser, ValueEnum};
use clog::{fmt::ChangelogFormat, Clog, LinkStyle as ClogLinkStyle};
use strum::{Display, EnumString};
use crate::{
error::{CliError, CliResult},
DEFAULT_CONFIG_FILE,
};
static VERSION: &str = env!("CARGO_PKG_VERSION");
static AFTER_HELP: &str = "
If your .git directory is a child of your project directory (most common, such \
as /myproject/.git) AND not in the current working directory (i.e you need to \
use --work-tree or --git-dir) you only need to specify either the --work-tree \
(i.e. /myproject) OR --git-dir (i.e. /myproject/.git), you don't need to use \
both.
If using the --config to specify a clog configuration TOML file NOT in the \
current working directory (meaning you need to use --work-tree or --git-dir) \
AND the TOML file is inside your project directory (i.e. \
/myproject/.clog.toml) you do not need to use --work-tree or --git-dir.
";
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, ValueEnum, EnumString, Display)]
#[strum(serialize_all = "lowercase", ascii_case_insensitive)]
pub enum OutFormat {
#[default]
Markdown,
Json,
}
impl From<OutFormat> for ChangelogFormat {
fn from(of: OutFormat) -> ChangelogFormat {
match of {
OutFormat::Markdown => ChangelogFormat::Markdown,
OutFormat::Json => ChangelogFormat::Json,
}
}
}
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, ValueEnum, EnumString, Display)]
#[strum(serialize_all = "lowercase", ascii_case_insensitive)]
pub enum LinkStyle {
#[default]
Github,
Gitlab,
Stash,
Cgit,
}
impl From<LinkStyle> for ClogLinkStyle {
fn from(ls: LinkStyle) -> ClogLinkStyle {
match ls {
LinkStyle::Github => ClogLinkStyle::Github,
LinkStyle::Gitlab => ClogLinkStyle::Gitlab,
LinkStyle::Stash => ClogLinkStyle::Stash,
LinkStyle::Cgit => ClogLinkStyle::Cgit,
}
}
}
/// a conventional changelog for the rest of us
#[derive(Debug, Clone, PartialEq, Eq, Parser)]
#[command(name= "clog", version = VERSION, after_help = AFTER_HELP)]
pub struct Args {
/// Repository used for generating commit and issue links (without the .git,
/// e.g. https://github.com/thoughtram/clog)
#[arg(short, long, value_name = "URL")]
pub repository: Option<String>,
/// e.g. 12a8546
#[arg(short, long, value_name = "COMMIT")]
pub from: Option<String>,
/// The output format, defaults to markdown
#[arg(short = 'T', long, value_name = "STR", default_value_t)]
pub format: OutFormat,
/// Increment major version by one (Sets minor and patch to 0)
#[arg(short = 'M', long)]
pub major: bool,
/// Local .git directory (defaults to "$(pwd)/.git")
#[arg(short, long, value_name = "PATH")]
pub git_dir: Option<String>,
/// Local working tree of the git project (defaults to "$(pwd)")
#[arg(short, long, value_name = "PATH")]
pub work_tree: Option<String>,
/// Increment minor version by one (Sets patch to 0)
#[arg(short, long)]
pub minor: bool,
/// Increment patch version by one
#[arg(short, long)]
pub patch: bool,
// e.g. "Crazy Release Title"
#[arg(short, long, value_name = "STR")]
pub subtitle: Option<String>,
/// e.g. 8057684
#[arg(short, long, value_name = "COMMIT", default_value = "HEAD")]
pub to: String,
/// Where to write the changelog (Defaults to stdout when omitted)
#[arg(short, long, value_name = "PATH")]
pub outfile: Option<String>,
/// The Clog Configuration TOML file to use
#[arg(short, long, value_name = "COMMIT", default_value = DEFAULT_CONFIG_FILE)]
pub config: String,
/// A changelog to append to, but *NOT* write to (Useful in conjunction with
/// --outfile)
#[arg(short, long, value_name = "PATH")]
pub infile: Option<String>,
/// e.g. 1.0.1
#[arg(long, value_name = "VER", group = "setver")]
pub setversion: Option<String>,
/// use latest tag as start (instead of --from)
#[arg(short = 'F', long, conflicts_with = "from")]
pub from_latest_tag: bool,
/// The style of repository link to generate
#[arg(short, long, value_name = "STR", default_value_t)]
pub link_style: LinkStyle,
/// A previous changelog to prepend new changes to (this is like using the
/// same file for both --infile and --outfile and should not be used in
/// conjunction with either)
#[arg(short = 'C', long, value_name = "PATH", conflicts_with_all = ["infile", "outfile"])]
pub changelog: Option<String>,
}
impl Args {
pub fn into_clog(self) -> CliResult<Clog> {
debugln!("Creating clog from matches");
let mut clog = if self.work_tree.is_some() && self.git_dir.is_some() {
debugln!(
"User passed in both\n\tworking dir: {:?}\n\tgit dir: {:?}",
self.work_tree,
self.git_dir
);
Clog::new()?
.git_dir(self.git_dir.as_ref().unwrap())
.git_work_tree(self.work_tree.as_ref().unwrap())
} else if let Some(dir) = &self.work_tree {
debugln!("User passed in working dir: {:?}", dir);
// use --config --work-tree
Clog::from_config(&self.config)?.git_work_tree(dir)
} else if let Some(dir) = &self.git_dir {
debugln!("User passed in git dir: {:?}", dir);
// use --config --git-dir
Clog::from_config(&self.config)?.git_dir(dir)
} else {
debugln!("User only passed config");
// use --config only
Clog::from_config(&self.config)?
};
// compute version early, so we can exit on error
clog.version = {
// less typing later...
let (major, minor, patch) = (self.major, self.minor, self.patch);
if self.setversion.is_some() {
self.setversion
} else if major || minor || patch {
let mut had_v = false;
let v_string = clog.get_latest_tag_ver();
let first_char = v_string.chars().next().unwrap_or(' ');
let v_slice = if first_char == 'v' || first_char == 'V' {
had_v = true;
v_string.trim_start_matches(['v', 'V'])
} else {
&v_string[..]
};
match semver::Version::parse(v_slice) {
Ok(ref mut v) => {
// if-else may be quicker, but it's longer mentally, and this isn't slow
match (major, minor, patch) {
(true, _, _) => {
v.major += 1;
v.minor = 0;
v.patch = 0;
}
(_, true, _) => {
v.minor += 1;
v.patch = 0;
}
(_, _, true) => {
v.patch += 1;
clog.patch_ver = true;
}
_ => unreachable!(),
}
Some(format!("{}{v}", if had_v { "v" } else { "" }))
}
Err(e) => {
return Err(CliError::Semver(
Box::new(e),
String::from(
"Failed to parse version into valid SemVer. \
Ensure the version is in the X.Y.Z format.",
),
));
}
}
} else {
clog.version
}
};
if let Some(from) = &self.from {
clog.from = Some(from.to_owned());
} else if self.from_latest_tag {
clog.from = Some(clog.get_latest_tag()?);
}
if let Some(file) = &self.outfile {
clog.outfile = Some(file.to_owned());
}
if let Some(file) = &self.infile {
clog.infile = Some(file.to_owned());
}
if let Some(file) = &self.changelog {
clog.infile = Some(file.to_owned());
clog.outfile = Some(file.to_owned());
}
clog.repo = self.repository;
clog.subtitle = self.subtitle;
clog.link_style = self.link_style.into();
clog.out_format = self.format.into();
self.to.clone_into(&mut clog.to);
debugln!("Returning clog:\n{:?}", clog);
Ok(clog)
}
}