@@ -21,13 +21,18 @@ use anyhow::Result;
21
21
use clap:: CommandFactory ;
22
22
use clap:: Parser ;
23
23
use std:: env;
24
- use std:: ops:: Not ;
25
24
26
25
pub mod api;
27
26
pub mod args;
28
27
pub mod display;
29
28
pub mod infra;
30
29
30
+ fn panic_if_err < T > ( result : & Result < T > ) {
31
+ if let Err ( e) = result {
32
+ panic ! ( "{}" , e)
33
+ }
34
+ }
35
+
31
36
#[ tokio:: main( flavor = "multi_thread" ) ]
32
37
async fn main ( ) -> Result < ( ) > {
33
38
let args_vec = env:: args ( ) . collect :: < Vec < _ > > ( ) ;
@@ -44,18 +49,22 @@ async fn main() -> Result<()> {
44
49
let style = & args. style ;
45
50
let time_style = & args. time_style ;
46
51
let rev = args. rev ;
52
+ let foe = args. fail_on_error ;
47
53
48
54
let output = match args {
49
55
_ if let Some ( pat) = parser:: login ( & args) => {
50
56
let cfg_path = session:: login ( pat) ;
57
+ foe. then ( || panic_if_err ( & cfg_path) ) ;
51
58
display:: login ( style, & cfg_path)
52
59
}
53
60
_ if parser:: logout ( & args) => {
54
- let cfg_path = & session:: logout ( ) ;
55
- display:: logout ( style, cfg_path)
61
+ let cfg_path = session:: logout ( ) ;
62
+ foe. then ( || panic_if_err ( & cfg_path) ) ;
63
+ display:: logout ( style, & cfg_path)
56
64
}
57
65
_ if parser:: user_info ( & args) => {
58
66
let user_info = User :: new ( pat?) . get_info ( ) . await ;
67
+ foe. then ( || panic_if_err ( & user_info) ) ;
59
68
display:: user_info ( style, & user_info) ?
60
69
}
61
70
_ if let Some ( ( skip, take, r#type, align) ) = parser:: list_ing ( & args) => {
@@ -72,59 +81,71 @@ async fn main() -> Result<()> {
72
81
. into_iter ( )
73
82
. collect :: < Result < Vec < _ > > > ( ) ?
74
83
} ;
84
+ foe. then ( || panic_if_err ( & ing_with_comment_list) ) ;
75
85
display:: list_ing ( style, time_style, & ing_with_comment_list, rev, align) ?
76
86
}
77
87
_ if let Some ( content) = parser:: publish_ing ( & args) => {
78
88
let content = try {
79
89
Ing :: new ( pat?) . publish ( content) . await ?;
80
90
content
81
91
} ;
92
+ foe. then ( || panic_if_err ( & content) ) ;
82
93
display:: publish_ing ( style, & content)
83
94
}
84
95
_ if let Some ( ( content, id) ) = parser:: comment_ing ( & args) => {
85
96
let content = try {
86
97
Ing :: new ( pat?) . comment ( id, content. clone ( ) , None , None ) . await ?;
87
98
content
88
99
} ;
100
+ foe. then ( || panic_if_err ( & content) ) ;
89
101
display:: comment_ing ( style, & content)
90
102
}
91
103
_ if let Some ( id) = parser:: show_post ( & args) => {
92
104
let entry = Post :: new ( pat?) . get_one ( id) . await ;
105
+ foe. then ( || panic_if_err ( & entry) ) ;
93
106
display:: show_post ( style, & entry) ?
94
107
}
95
108
_ if let Some ( id) = parser:: show_post_meta ( & args) => {
96
109
let entry = Post :: new ( pat?) . get_one ( id) . await ;
110
+ foe. then ( || panic_if_err ( & entry) ) ;
97
111
display:: show_post_meta ( style, time_style, & entry) ?
98
112
}
99
113
_ if let Some ( id) = parser:: show_post_comment ( & args) => {
100
114
let comment_vec = Post :: new ( pat?) . get_comment_list ( id) . await ;
115
+ foe. then ( || panic_if_err ( & comment_vec) ) ;
101
116
display:: show_post_comment ( style, time_style, & comment_vec, rev) ?
102
117
}
103
118
_ if let Some ( ( skip, take) ) = parser:: list_post ( & args) => {
104
119
let meta_vec = Post :: new ( pat?) . get_meta_list ( skip, take) . await ;
120
+ foe. then ( || panic_if_err ( & meta_vec) ) ;
105
121
display:: list_post ( style, & meta_vec, rev) ?
106
122
}
107
123
_ if let Some ( id) = parser:: delete_post ( & args) => {
108
124
let id = try {
109
125
Post :: new ( pat?) . del_one ( id) . await ?;
110
126
id
111
127
} ;
128
+ foe. then ( || panic_if_err ( & id) ) ;
112
129
display:: delete_post ( style, & id)
113
130
}
114
131
_ if let Some ( ( kw, skip, take) ) = parser:: search_post ( & args) => {
115
132
let result = Post :: new ( pat?) . search ( skip, take, kw) . await ;
133
+ foe. then ( || panic_if_err ( & result) ) ;
116
134
display:: search_post ( style, & result, rev) ?
117
135
}
118
136
_ if let Some ( ( title, body, publish) ) = parser:: create_post ( & args) => {
119
137
let id = Post :: new ( pat?) . create ( title, body, publish) . await ;
138
+ foe. then ( || panic_if_err ( & id) ) ;
120
139
display:: create_post ( style, & id)
121
140
}
122
141
_ if let Some ( ( id, title, body, publish) ) = parser:: update_post ( & args) => {
123
142
let id = Post :: new ( pat?) . update ( id, title, body, publish) . await ;
143
+ foe. then ( || panic_if_err ( & id) ) ;
124
144
display:: update_post ( style, & id)
125
145
}
126
146
_ if let Some ( ( skip, take) ) = parser:: list_news ( & args) => {
127
147
let news_vec = News :: new ( pat?) . get_list ( skip, take) . await ;
148
+ foe. then ( || panic_if_err ( & news_vec) ) ;
128
149
display:: list_news ( style, time_style, & news_vec, rev) ?
129
150
}
130
151
@@ -135,11 +156,7 @@ async fn main() -> Result<()> {
135
156
_ => "Invalid usage, follow '--help' for more information" . to_owned ( )
136
157
} ;
137
158
138
- if args. fail_on_error {
139
- panic ! ( "{}" , output) ;
140
- }
141
- if args. quiet . not ( ) {
142
- print ! ( "{}" , output) ;
143
- }
159
+ print ! ( "{}" , output) ;
160
+
144
161
( ) . into_ok ( )
145
162
}
0 commit comments