Skip to content

Commit 7667d69

Browse files
authored
feat(cli): support multi LICENSE (#14)
* support multi LICENSE * update README.md * require [LICENSE_TYPE]
1 parent b15eefe commit 7667d69

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ The CLI supports the following licenses:
5353

5454
### Output to stdout
5555

56-
```shell
57-
license-generate --output /dev/stdout
58-
```
56+
license-generator MIT --output /dev/stdout --author "azu"
57+
58+
### multi LICENSE
59+
60+
license-generator MIT Apache --author "azu"
5961

6062
## Tests
6163

src/main.rs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use std::env;
1111

1212
#[derive(StructOpt, Debug)]
1313
struct Opt {
14-
#[structopt(name = "INPUT")]
15-
input: String,
14+
#[structopt(name = "INPUT", required = true)]
15+
inputs: Vec<String>,
1616
#[structopt(long = "author")]
1717
author: String,
1818
#[structopt(long = "project")]
@@ -25,13 +25,6 @@ struct Opt {
2525

2626
fn main() -> Result<(), Box<dyn std::error::Error>> {
2727
let opt = Opt::from_args();
28-
let license = create_license(
29-
opt.input.as_str()
30-
);
31-
let license = license.unwrap_or_else(|| {
32-
eprintln!("Not found match license: {}", opt.input);
33-
process::exit(1);
34-
});
3528
let dt = Local::now();
3629
let current_year = dt.year();
3730
let year = opt.year.unwrap_or_else(|| {
@@ -48,17 +41,29 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
4841
.into_string()
4942
.expect("use --project: Fail to unwrap os_string")
5043
});
51-
52-
let license_text = license.notice(
53-
year,
54-
&author,
55-
&project);
5644
let output = opt.output.unwrap_or_else(|| {
57-
"LICENSE".to_string()
58-
});
59-
write_license(&license_text, &output).unwrap_or_else(|error| {
60-
eprintln!("Can not write license text to \"{}\": {}", output, error);
61-
process::exit(1);
45+
"LICENSE".to_string()
6246
});
47+
let multi_license = opt.inputs.len() > 1;
48+
49+
opt.inputs
50+
.iter()
51+
.for_each(|s| {
52+
let license = create_license(s.as_str()).unwrap_or_else(|| {
53+
eprintln!("Not found match license: {}", s);
54+
process::exit(1);
55+
});
56+
let license_text = license.notice(
57+
year,
58+
&author,
59+
&project
60+
);
61+
let output = if multi_license { format!("{}_{}", output, s.to_uppercase()) }
62+
else { output.clone() };
63+
write_license(&license_text, &output).unwrap_or_else(|error| {
64+
eprintln!("Can not write license text to \"{}\": {}", output, error);
65+
process::exit(1);
66+
});
67+
});
6368
Ok(())
6469
}

0 commit comments

Comments
 (0)