Skip to content

Commit b363871

Browse files
committed
feat: create post
1 parent f41ceaf commit b363871

File tree

7 files changed

+87
-4
lines changed

7 files changed

+87
-4
lines changed

src/args/cmd/post.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,12 @@ pub struct Opt {
4141
#[arg(long)]
4242
#[arg(value_name = "KEYWORD")]
4343
pub search: Option<String>,
44+
45+
#[arg(verbatim_doc_comment)]
46+
/// Create a post
47+
/// Example: cnb post --create 'Title' 'Body'
48+
/// The status of post is draft
49+
#[arg(long)]
50+
#[arg(value_names = ["TITLE","BODY"])]
51+
pub create: Option<Vec<String>>,
4452
}

src/args/parser.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ pub fn show_post(args: &Args) -> Option<Result<(String, usize)>> {
175175
list: false,
176176
delete: false,
177177
search: None,
178+
create: None,
178179
})),
179180
id: Some(id),
180181
with_pat,
@@ -199,6 +200,7 @@ pub fn show_post_meta(args: &Args) -> Option<Result<(String, usize)>> {
199200
list: false,
200201
delete: false,
201202
search: None,
203+
create: None,
202204
})),
203205
id: Some(id),
204206
with_pat,
@@ -223,6 +225,7 @@ pub fn list_post(args: &Args) -> Option<Result<(String, usize, usize)>> {
223225
list: true,
224226
delete: false,
225227
search: None,
228+
create: None,
226229
})),
227230
id: None,
228231
with_pat,
@@ -251,6 +254,7 @@ pub fn delete_post(args: &Args) -> Option<Result<(String, usize)>> {
251254
list: false,
252255
delete: true,
253256
search: None,
257+
create: None,
254258
})),
255259
id: Some(id),
256260
with_pat,
@@ -275,6 +279,7 @@ pub fn search_post(args: &Args) -> Option<Result<(String, &String, usize, usize)
275279
list: false,
276280
delete: false,
277281
search: Some(keyword),
282+
create: None,
278283
})),
279284
id: None,
280285
with_pat,
@@ -292,3 +297,32 @@ pub fn search_post(args: &Args) -> Option<Result<(String, &String, usize, usize)
292297
}
293298
.into_some()
294299
}
300+
301+
pub fn create_post(args: &Args) -> Option<Result<(String, &String, &String)>> {
302+
match args {
303+
Args {
304+
command:
305+
Some(Cmd::Post(cmd::post::Opt {
306+
show: false,
307+
show_meta: false,
308+
list: false,
309+
delete: false,
310+
search: None,
311+
create: Some(vec),
312+
})),
313+
id: None,
314+
with_pat,
315+
rev: _,
316+
skip: None,
317+
take: None,
318+
debug: _,
319+
style: _,
320+
} if vec.len() == 2 => {
321+
let title = &vec[0];
322+
let body = &vec[1];
323+
get_pat(with_pat).map(|pat| (pat, title, body))
324+
}
325+
_ => return None,
326+
}
327+
.into_some()
328+
}

src/display/colorful.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ pub fn list_post(entry_list: &[PostEntry], total_count: usize, rev: bool) {
155155

156156
pub fn delete_post(result: &Result<usize>) {
157157
match result {
158-
Ok(id) => println!("{}: {}", "Deleted".green(), id),
159-
Err(e) => println!("{}: {}", "Error".red(), e),
158+
Ok(id) => println!("{}: {}", "Ok".green(), id),
159+
Err(e) => println!("{}: {}", "Err".red(), e),
160160
}
161161
}
162162

@@ -167,3 +167,10 @@ pub fn search_post(id_list: &[usize], total_count: usize, rev: bool) {
167167
.dyn_rev(rev)
168168
.for_each(|id| println!("{}", id));
169169
}
170+
171+
pub fn create_post(result: &Result<usize>) {
172+
match result {
173+
Ok(id) => println!("{}: {}", "Ok".green(), id),
174+
Err(e) => println!("{}: {}", "Err".red(), e),
175+
}
176+
}

src/display/json.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ pub fn list_post(entry_list: &[PostEntry], total_count: usize, rev: bool) {
9595
pub fn delete_post(result: &Result<usize>) {
9696
let json = match result {
9797
Ok(id) => json!({
98-
"ok": true,
98+
"is_ok": true,
9999
"msg": id
100100
}),
101101
Err(e) => json!({
102-
"ok": false,
102+
"is_ok": false,
103103
"msg": e.to_string()
104104
}),
105105
};
@@ -116,3 +116,17 @@ pub fn search_post(id_list: &[usize], total_count: usize, rev: bool) {
116116

117117
println!("{}", json);
118118
}
119+
120+
pub fn create_post(result: &Result<usize>) {
121+
let json = match result {
122+
Ok(id) => json!({
123+
"is_ok": true,
124+
"msg": id
125+
}),
126+
Err(e) => json!({
127+
"is_ok": false,
128+
"msg": e.to_string()
129+
}),
130+
};
131+
println!("{}", json)
132+
}

src/display/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,11 @@ pub fn search_post(style: &Style, id_list: &[usize], total_count: usize, rev: bo
103103
Style::Json => json::search_post(id_list, total_count, rev),
104104
}
105105
}
106+
107+
pub fn create_post(style: &Style, result: &Result<usize>) {
108+
match style {
109+
Style::Colorful => colorful::create_post(result),
110+
Style::Normal => normal::create_post(result),
111+
Style::Json => json::create_post(result),
112+
}
113+
}

src/display/normal.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,10 @@ pub fn search_post(id_list: &[usize], total_count: usize, rev: bool) {
165165
.dyn_rev(rev)
166166
.for_each(|id| println!("{}", id));
167167
}
168+
169+
pub fn create_post(result: &Result<usize>) {
170+
match result {
171+
Ok(id) => println!("Ok: {}", id),
172+
Err(e) => println!("Err: {}", e),
173+
}
174+
}

src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ async fn main() -> Result<()> {
9191
let (id_list, total_count) = Post::new(pat).search(skip, take, kw).await?;
9292
display::search_post(style, &id_list, total_count, rev);
9393
}
94+
_ if let Some(r) = parser::create_post(&args) => {
95+
let (pat, title, body) = r?;
96+
let id = Post::new(pat).create(title, body, false).await;
97+
display::create_post(style, &id);
98+
}
9499

95100
_ if no_operation(&args) => {
96101
Args::command().print_help()?;

0 commit comments

Comments
 (0)