Skip to content

Commit bdc9c7f

Browse files
committed
Add JSON Support for list command
1 parent 0163314 commit bdc9c7f

File tree

5 files changed

+51
-7
lines changed

5 files changed

+51
-7
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
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
@@ -1,6 +1,6 @@
11
[package]
22
name = "jira-terminal"
3-
version = "1.6.0"
3+
version = "2.0.0"
44
authors = ["Amrit Ghimire <[email protected]>"]
55
edition = "2018"
66
description = "This is a command line application that can be used as a personal productivity tool for interacting with JIRA"

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ jira-terminal help autocompletion
6666
```
6767

6868
```
69-
JIRA Terminal 1.5.0
69+
JIRA Terminal 2.0.0
7070
Amrit Ghimire <[email protected]>
7171
This is a command line application that can be used as a personal productivity tool for interacting with JIRA
7272
@@ -102,6 +102,7 @@ USAGE:
102102
103103
FLAGS:
104104
-h, --help Prints help information
105+
-J, --json JSON response
105106
-M, --me Issues assigned to you.
106107
-V, --version Prints version information
107108

src/jira/lists.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ fn display_content(option: &json::JsonValue, value: &json::JsonValue) {
2424
print!("{value:width$}|", value = content, width = width)
2525
}
2626

27+
fn return_json(option: &json::JsonValue, value: &json::JsonValue) -> json::JsonValue {
28+
if value.is_array() {
29+
let mut contents = json::JsonValue::new_array();
30+
let field = option["field"].as_str().unwrap_or("name");
31+
for entry in value.members() {
32+
let _ = contents.push(String::from(entry[field].as_str().unwrap_or("-")));
33+
}
34+
contents
35+
} else if value.is_object() {
36+
let field = option["field"].as_str().unwrap_or("name");
37+
value[field].clone()
38+
} else {
39+
value.clone()
40+
}
41+
}
42+
2743
fn display_header(option: &json::JsonValue) {
2844
print!(
2945
"{title:width$}|",
@@ -83,6 +99,7 @@ fn form_jql(matches: &ArgMatches) -> String {
8399
}
84100

85101
pub fn list_issues(matches: &ArgMatches) {
102+
let show_json = matches.is_present("json");
86103
let jql = form_jql(matches);
87104
let offset_result = matches.value_of("offset").unwrap_or("0").parse::<u32>();
88105
if offset_result.is_err() {
@@ -114,10 +131,7 @@ pub fn list_issues(matches: &ArgMatches) {
114131
);
115132
}
116133
let issues = &search_response.unwrap()["issues"];
117-
if !issues.is_array() {
118-
println!("No issues found for the filter.");
119-
std::process::exit(0);
120-
}
134+
121135
let display: String = String::from(
122136
matches
123137
.value_of("display")
@@ -138,6 +152,28 @@ pub fn list_issues(matches: &ArgMatches) {
138152
};
139153
let headers_to_display = display;
140154
let headers = headers_to_display.trim().split(',');
155+
156+
if show_json {
157+
let mut response = json::JsonValue::new_array();
158+
for issue in issues.members() {
159+
let mut data = json::JsonValue::new_object();
160+
for header in headers.clone() {
161+
if header == "key" {
162+
data[header] = return_json(&display_options[header], &issue[header]);
163+
} else {
164+
data[header] = return_json(&display_options[header], &issue["fields"][header]);
165+
}
166+
}
167+
let _ = response.push(data);
168+
}
169+
println!("{}", response.pretty(4));
170+
return;
171+
}
172+
173+
if !issues.is_array() {
174+
println!("No issues found for the filter.");
175+
std::process::exit(0);
176+
}
141177
let mut total = 0;
142178
for header in headers.clone() {
143179
if display_options[header].is_null() {

src/subcommands/list.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ pub fn subcommand() -> App<'static, 'static> {
2626
.value_name("ME")
2727
.takes_value(false)
2828
)
29+
.arg(Arg::with_name("json")
30+
.help("JSON response")
31+
.short("J")
32+
.long("json")
33+
.value_name("JSON")
34+
.takes_value(false)
35+
)
2936
.arg(Arg::with_name("component")
3037
.help("Component name or ID to filter with.")
3138
.short("c")

0 commit comments

Comments
 (0)