Skip to content

Commit 41f26bc

Browse files
committed
refactor: merge app.rs and main.rs
1 parent 5dfd80a commit 41f26bc

File tree

5 files changed

+64
-72
lines changed

5 files changed

+64
-72
lines changed

Cargo.lock

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ edition = "2018"
1717
uuid = { version = "0.5", features = ["v4"] }
1818
atomicwrites = "0.1"
1919
email = "0.0"
20-
clap = "2.31"
20+
clap = "2.33.0"
2121
vobject = "0.2"
2222
cursive = "0.14"
2323
anyhow = "1.0.26"

src/app.rs

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ extern crate email;
77
extern crate uuid;
88
extern crate vobject;
99

10-
pub mod app;
1110
pub mod cli;
1211
mod editor;
1312
pub mod utils;

src/main.rs

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,43 @@ use anyhow::Result;
44
use std::fs;
55
use std::io;
66
use std::io::{Read, Write};
7+
use clap::{App, AppSettings, Arg, SubCommand};
78

8-
use mates::app;
99
use mates::cli;
1010
use mates::utils;
1111

1212
fn main() -> Result<()> {
13-
let matches = app::app().get_matches();
14-
15-
let command = matches.subcommand_name().unwrap();
13+
let app = App::new("mates")
14+
.version(env!("CARGO_PKG_VERSION"))
15+
.author("Markus Unterwaditzer")
16+
.about("A simple commandline addressbook")
17+
.setting(AppSettings::SubcommandRequired)
18+
.subcommand(SubCommand::with_name("index").about("Rewrite/create the index"))
19+
.subcommand(
20+
SubCommand::with_name("mutt-query")
21+
.about("Search for contact, output is usable for mutt's query_command.")
22+
.arg(Arg::with_name("query").index(1)),
23+
)
24+
.subcommand(
25+
SubCommand::with_name("file-query")
26+
.about("Search for contact, return just the filename.")
27+
.arg(Arg::with_name("query").index(1)),
28+
)
29+
.subcommand(
30+
SubCommand::with_name("email-query")
31+
.about("Search for contact, return \"name <email>\".")
32+
.arg(Arg::with_name("query").index(1)),
33+
)
34+
.subcommand(
35+
SubCommand::with_name("add")
36+
.about("Take mail from stdin, add sender to contacts. Print filename."),
37+
)
38+
.subcommand(
39+
SubCommand::with_name("edit")
40+
.about("Open contact (given by filepath or search-string) interactively.")
41+
.arg(Arg::with_name("file-or-query").index(1)),
42+
)
43+
.get_matches();
1644

1745
let config = match cli::Configuration::new() {
1846
Ok(x) => x,
@@ -21,31 +49,30 @@ fn main() -> Result<()> {
2149
}
2250
};
2351

24-
let submatches = matches
25-
.subcommand_matches(command)
26-
.expect("Internal error.");
27-
28-
match command {
29-
"index" => {
52+
match app.subcommand() {
53+
("index", Some(_subs)) => {
3054
println!(
3155
"Rebuilding index file \"{}\"...",
3256
config.index_path.display()
3357
);
3458
cli::build_index(&config.index_path, &config.vdir_path)?;
3559
}
36-
"mutt-query" => {
37-
let query = submatches.value_of("query").unwrap_or("");
38-
cli::mutt_query(&config, &query[..])?;
60+
("mutt-query", Some(subs)) => {
61+
if let Some(value) = subs.value_of("query") {
62+
cli::mutt_query(&config, value)?
63+
}
3964
}
40-
"file-query" => {
41-
let query = submatches.value_of("query").unwrap_or("");
42-
cli::file_query(&config, &query[..])?;
65+
("file-query", Some(subs)) => {
66+
if let Some(value) = subs.value_of("query") {
67+
cli::file_query(&config, value)?
68+
}
4369
}
44-
"email-query" => {
45-
let query = submatches.value_of("query").unwrap_or("");
46-
cli::email_query(&config, &query[..])?;
70+
("email-query", Some(subs)) => {
71+
if let Some(value) = subs.value_of("query") {
72+
cli::email_query(&config, value)?
73+
}
4774
}
48-
"add" => {
75+
("add", Some(..)) => {
4976
let stdin = io::stdin();
5077
let mut email = String::new();
5178
stdin.lock().read_to_string(&mut email)?;
@@ -60,13 +87,13 @@ fn main() -> Result<()> {
6087
let index_entry = utils::index_item_from_contact(&contact)?;
6188
index_fp.write_all(index_entry.as_bytes())?;
6289
}
63-
"edit" => {
64-
let query = submatches.value_of("file-or-query").unwrap_or("");
65-
cli::edit_contact(&config, &query[..])?;
66-
}
67-
_ => {
68-
return Err(anyhow!("Invalid command: {}", command));
90+
("edit", Some(subs)) => {
91+
if let Some(value) = subs.value_of("file-or-query") {
92+
cli::edit_contact(&config, value)?
93+
}
6994
}
70-
};
95+
_ => (),
96+
}
97+
7198
Ok(())
7299
}

0 commit comments

Comments
 (0)