Skip to content

Commit 4f98556

Browse files
committed
feat: quiet option
1 parent f9fde72 commit 4f98556

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

src/args/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,12 @@ pub struct Args {
8585
#[clap(visible_alias = "foe")]
8686
#[arg(default_value_t = false)]
8787
pub fail_on_error: bool,
88+
89+
#[arg(verbatim_doc_comment)]
90+
/// Suppress all output
91+
/// Example: cnb --quiet ing --list
92+
#[arg(long)]
93+
#[clap(visible_alias = "silent")]
94+
#[arg(default_value_t = false)]
95+
pub quiet: bool,
8896
}

src/args/parser.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub const fn no_operation(args: &Args) -> bool {
2222
debug: _,
2323
style: _,
2424
fail_on_error: _,
25+
quiet: _,
2526
}
2627
)
2728
}
@@ -43,6 +44,7 @@ pub const fn user_info(args: &Args) -> bool {
4344
debug: _,
4445
style: _,
4546
fail_on_error: _,
47+
quiet: _,
4648
}
4749
)
4850
}
@@ -64,6 +66,7 @@ pub fn publish_ing(args: &Args) -> Option<&String> {
6466
debug: _,
6567
style: _,
6668
fail_on_error: _,
69+
quiet: _,
6770
} => content,
6871
_ => return None,
6972
}
@@ -87,6 +90,7 @@ pub fn login(args: &Args) -> Option<&String> {
8790
debug: _,
8891
style: _,
8992
fail_on_error: _,
93+
quiet: _,
9094
} => pat,
9195
_ => return None,
9296
}
@@ -110,6 +114,7 @@ pub const fn logout(args: &Args) -> bool {
110114
debug: _,
111115
style: _,
112116
fail_on_error: _,
117+
quiet: _,
113118
}
114119
)
115120
}
@@ -131,6 +136,7 @@ pub fn list_ing(args: &Args) -> Option<(usize, usize)> {
131136
debug: _,
132137
style: _,
133138
fail_on_error: _,
139+
quiet: _,
134140
} => {
135141
let skip = get_skip(skip);
136142
let take = get_take(take);
@@ -158,6 +164,7 @@ pub fn comment_ing(args: &Args) -> Option<(&String, usize)> {
158164
debug: _,
159165
style: _,
160166
fail_on_error: _,
167+
quiet: _,
161168
} => (content, *id),
162169
_ => return None,
163170
}
@@ -184,6 +191,7 @@ pub fn show_post(args: &Args) -> Option<usize> {
184191
debug: _,
185192
style: _,
186193
fail_on_error: _,
194+
quiet: _,
187195
} => *id,
188196
_ => return None,
189197
}
@@ -210,6 +218,7 @@ pub fn show_post_meta(args: &Args) -> Option<usize> {
210218
debug: _,
211219
style: _,
212220
fail_on_error: _,
221+
quiet: _,
213222
} => *id,
214223
_ => return None,
215224
}
@@ -236,6 +245,7 @@ pub fn list_post(args: &Args) -> Option<(usize, usize)> {
236245
debug: _,
237246
style: _,
238247
fail_on_error: _,
248+
quiet: _,
239249
} => {
240250
let skip = get_skip(skip);
241251
let take = get_take(take);
@@ -266,6 +276,7 @@ pub fn delete_post(args: &Args) -> Option<usize> {
266276
debug: _,
267277
style: _,
268278
fail_on_error: _,
279+
quiet: _,
269280
} => *id,
270281
_ => return None,
271282
}
@@ -292,6 +303,7 @@ pub fn search_post(args: &Args) -> Option<(&String, usize, usize)> {
292303
debug: _,
293304
style: _,
294305
fail_on_error: _,
306+
quiet: _,
295307
} => {
296308
let skip = get_skip(skip);
297309
let take = get_take(take);
@@ -327,6 +339,7 @@ pub fn create_post(args: &Args) -> Option<(&String, &String, bool)> {
327339
debug: _,
328340
style: _,
329341
fail_on_error: _,
342+
quiet: _,
330343
} => (title, body, *publish),
331344
_ => return None,
332345
}
@@ -362,6 +375,7 @@ pub fn update_post(
362375
debug: _,
363376
style: _,
364377
fail_on_error: _,
378+
quiet: _,
365379
} => (*id, title, body, publish),
366380
_ => return None,
367381
}

src/main.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use anyhow::Result;
1818
use clap::CommandFactory;
1919
use clap::Parser;
2020
use std::env;
21+
use std::ops::Not;
2122

2223
pub mod api;
2324
pub mod args;
@@ -46,86 +47,87 @@ async fn main() -> Result<()> {
4647
let style = &args.style;
4748
let rev = args.rev;
4849
let foe = args.fail_on_error;
50+
let quiet = args.quiet;
4951

5052
match args {
5153
_ if let Some(pat) = parser::login(&args) => {
5254
let cfg_path = session::login(pat);
5355
foe.then(||panic_if_err(&cfg_path));
54-
display::login(style, &cfg_path);
56+
quiet.not().then(||display::login(style, &cfg_path));
5557
}
5658
_ if parser::logout(&args) => {
5759
let cfg_path = &session::logout();
5860
foe.then(||panic_if_err(cfg_path));
59-
display::logout(style, cfg_path);
61+
quiet.not().then(||display::logout(style, cfg_path));
6062
}
6163
_ if parser::user_info(&args) => {
6264
let user_info = try {
6365
User::new(pat?).get_info().await?
6466
};
6567
foe.then(||panic_if_err(&user_info));
66-
display::user_info(style, &user_info);
68+
quiet.not().then(||display::user_info(style, &user_info));
6769
}
6870
_ if let Some((skip, take)) = parser::list_ing(&args) => {
6971
let ing_type = IngType::Public;
7072
let ing_vec = try {
7173
Ing::new(pat?).get_list(skip, take, &ing_type).await?
7274
};
7375
foe.then(||panic_if_err(&ing_vec));
74-
display::list_ing(style, &ing_vec, rev);
76+
quiet.not().then(||display::list_ing(style, &ing_vec, rev));
7577
}
7678
_ if let Some(content) = parser::publish_ing(&args) => {
7779
let content = try {
7880
Ing::new(pat?).publish(content).await?;
7981
content
8082
};
8183
foe.then(||panic_if_err(&content));
82-
display::publish_ing(style, &content);
84+
quiet.not().then(||display::publish_ing(style, &content));
8385
}
8486
_ if let Some((content, id))= parser::comment_ing(&args) => {
8587
let content = try {
8688
Ing::new(pat?).comment(id, content.clone(), None, None).await?;
8789
content
8890
};
8991
foe.then(||panic_if_err(&content));
90-
display::comment_ing(style, &content);
92+
quiet.not().then(||display::comment_ing(style, &content));
9193
}
9294
_ if let Some(id) = parser::show_post(&args) => {
9395
let entry = try { Post::new(pat?).get_one(id).await? };
9496
foe.then(||panic_if_err(&entry));
95-
display::show_post(style, &entry);
97+
quiet.not().then(||display::show_post(style, &entry));
9698
}
9799
_ if let Some(id) = parser::show_post_meta(&args) => {
98100
let entry = try { Post::new(pat?).get_one(id).await? };
99101
foe.then(||panic_if_err(&entry));
100-
display::show_post_meta(style, &entry);
102+
quiet.not().then(||display::show_post_meta(style, &entry));
101103
}
102104
_ if let Some((skip, take)) = parser::list_post(&args) => {
103105
let result = try { Post::new(pat?).get_meta_list(skip, take).await? };
104106
foe.then(||panic_if_err(&result));
105-
display::list_post(style, &result, rev);
107+
quiet.not().then(||display::list_post(style, &result, rev));
106108
}
107109
_ if let Some(id) = parser::delete_post(&args) => {
108110
let id = try {
109111
Post::new(pat?).del_one(id).await?;
110112
id
111113
};
112114
foe.then(||panic_if_err(&id));
113-
display::delete_post(style, &id);
115+
quiet.not().then(||display::delete_post(style, &id));
114116
}
115117
_ if let Some((kw, skip, take)) = parser::search_post(&args) => {
116118
let result = try { Post::new(pat?).search(skip, take, kw).await? };
117119
foe.then(||panic_if_err(&result));
118-
display::search_post(style, &result, rev);
120+
quiet.not().then(||display::search_post(style, &result, rev));
119121
}
120122
_ if let Some((title, body, publish)) = parser::create_post(&args) => {
121123
let id = try { Post::new(pat?).create(title, body, publish).await? };
122124
foe.then(||panic_if_err(&id));
123-
display::create_post(style, &id);
125+
quiet.not().then(||display::create_post(style, &id));
124126
}
125127
_ if let Some((id, title, body, publish)) = parser::update_post(&args) => {
126128
let id = try { Post::new(pat?).update(id,title, body, publish).await? };
127129
foe.then(||panic_if_err(&id));
128-
display::update_post(style, &id);
130+
quiet.not().then(||display::update_post(style, &id));
129131
}
130132

131133
_ if no_operation(&args) => {

0 commit comments

Comments
 (0)