Add support for printing json on the list command
This commit is contained in:
parent
3a2a5612c0
commit
0d244cf80d
6 changed files with 32 additions and 13 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -830,6 +830,7 @@ dependencies = [
|
||||||
"directories",
|
"directories",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"serde",
|
"serde",
|
||||||
|
"serde_json",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
"tokio",
|
"tokio",
|
||||||
"toml",
|
"toml",
|
||||||
|
|
|
@ -8,7 +8,7 @@ clap = { version = "4.4", features = ["derive"] }
|
||||||
reqwest = { version = "0.12", features = ["json"] }
|
reqwest = { version = "0.12", features = ["json"] }
|
||||||
tokio = { version = "1.0", features = ["full"] }
|
tokio = { version = "1.0", features = ["full"] }
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
#serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
#colored = "2.0"
|
#colored = "2.0"
|
||||||
toml = "0.8"
|
toml = "0.8"
|
||||||
config = "0.15"
|
config = "0.15"
|
||||||
|
|
|
@ -62,9 +62,10 @@ jirac create --project KEY ticket.md
|
||||||
## Listing tickets
|
## Listing tickets
|
||||||
|
|
||||||
```
|
```
|
||||||
Usage: jirac list
|
Usage: jirac list [OPTIONS]
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
|
--json
|
||||||
-h, --help Print help
|
-h, --help Print help
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,10 @@ pub enum Commands {
|
||||||
#[arg(value_name = "MARKDOWN_FILE")]
|
#[arg(value_name = "MARKDOWN_FILE")]
|
||||||
markdown_file: Option<PathBuf>,
|
markdown_file: Option<PathBuf>,
|
||||||
},
|
},
|
||||||
List,
|
List {
|
||||||
|
#[arg(long)]
|
||||||
|
json: bool,
|
||||||
|
},
|
||||||
Init {
|
Init {
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
url: String,
|
url: String,
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
use crate::jira_config::JiraConfig;
|
use crate::jira_config::JiraConfig;
|
||||||
use reqwest::header::{HeaderMap, HeaderValue, CONTENT_TYPE};
|
use reqwest::header::{HeaderMap, HeaderValue, CONTENT_TYPE};
|
||||||
use serde::Deserialize;
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize, Serialize)]
|
||||||
struct JiraIssue {
|
struct JiraIssue {
|
||||||
key: String,
|
key: String,
|
||||||
fields: JiraIssueResponseFields,
|
fields: JiraIssueResponseFields,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize, Serialize)]
|
||||||
struct JiraIssueResponseFields {
|
struct JiraIssueResponseFields {
|
||||||
summary: String,
|
summary: String,
|
||||||
status: Status,
|
status: Status,
|
||||||
|
@ -21,7 +21,7 @@ struct JiraSearchResponse {
|
||||||
total: u32,
|
total: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize, Serialize)]
|
||||||
struct Status {
|
struct Status {
|
||||||
name: String,
|
name: String,
|
||||||
}
|
}
|
||||||
|
@ -55,7 +55,7 @@ async fn list_jira_issues(
|
||||||
Ok(response.json::<JiraSearchResponse>().await?)
|
Ok(response.json::<JiraSearchResponse>().await?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn display_issues(issues: &[JiraIssue], jira_url: &str) {
|
fn display_issues_pretty(issues: &[JiraIssue], jira_url: &str) {
|
||||||
println!("Found {} issues:", issues.len());
|
println!("Found {} issues:", issues.len());
|
||||||
println!("{:-<80}", "");
|
println!("{:-<80}", "");
|
||||||
|
|
||||||
|
@ -69,16 +69,30 @@ fn display_issues(issues: &[JiraIssue], jira_url: &str) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn list() -> Result<(), Box<dyn std::error::Error>> {
|
fn display_issues_json(issues: &[JiraIssue]) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let j = serde_json::to_string_pretty(issues)?;
|
||||||
|
println!("{}", j);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn list(json: bool) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let config = JiraConfig::load().map_err(|e| format!("Configuration error: {}", e))?;
|
let config = JiraConfig::load().map_err(|e| format!("Configuration error: {}", e))?;
|
||||||
|
if !json {
|
||||||
println!("Fetching issues assigned...");
|
println!("Fetching issues assigned...");
|
||||||
|
}
|
||||||
|
|
||||||
match list_jira_issues(&config).await {
|
match list_jira_issues(&config).await {
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
|
if json {
|
||||||
if response.issues.is_empty() {
|
if response.issues.is_empty() {
|
||||||
|
println!("[]");
|
||||||
|
} else {
|
||||||
|
display_issues_json(&response.issues)?;
|
||||||
|
}
|
||||||
|
} else if response.issues.is_empty() {
|
||||||
println!("No open issues found for assigned to you");
|
println!("No open issues found for assigned to you");
|
||||||
} else {
|
} else {
|
||||||
display_issues(&response.issues, &config.url);
|
display_issues_pretty(&response.issues, &config.url);
|
||||||
println!("Total issues: {}", response.total);
|
println!("Total issues: {}", response.total);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
project,
|
project,
|
||||||
markdown_file,
|
markdown_file,
|
||||||
} => cmd::create::create(project, markdown_file).await?,
|
} => cmd::create::create(project, markdown_file).await?,
|
||||||
Commands::List => cmd::list::list().await?,
|
Commands::List { json } => cmd::list::list(json).await?,
|
||||||
Commands::Init { url, email, token } => {
|
Commands::Init { url, email, token } => {
|
||||||
JiraConfig::init(url, email, token).await?;
|
JiraConfig::init(url, email, token).await?;
|
||||||
println!("Configuration initialized successfully!");
|
println!("Configuration initialized successfully!");
|
||||||
|
|
Loading…
Reference in a new issue