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",
|
||||
"reqwest",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tempfile",
|
||||
"tokio",
|
||||
"toml",
|
||||
|
|
|
@ -8,7 +8,7 @@ clap = { version = "4.4", features = ["derive"] }
|
|||
reqwest = { version = "0.12", features = ["json"] }
|
||||
tokio = { version = "1.0", features = ["full"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
#serde_json = "1.0"
|
||||
serde_json = "1.0"
|
||||
#colored = "2.0"
|
||||
toml = "0.8"
|
||||
config = "0.15"
|
||||
|
|
|
@ -62,9 +62,10 @@ jirac create --project KEY ticket.md
|
|||
## Listing tickets
|
||||
|
||||
```
|
||||
Usage: jirac list
|
||||
Usage: jirac list [OPTIONS]
|
||||
|
||||
Options:
|
||||
--json
|
||||
-h, --help Print help
|
||||
```
|
||||
|
||||
|
|
|
@ -17,7 +17,10 @@ pub enum Commands {
|
|||
#[arg(value_name = "MARKDOWN_FILE")]
|
||||
markdown_file: Option<PathBuf>,
|
||||
},
|
||||
List,
|
||||
List {
|
||||
#[arg(long)]
|
||||
json: bool,
|
||||
},
|
||||
Init {
|
||||
#[arg(long)]
|
||||
url: String,
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
use crate::jira_config::JiraConfig;
|
||||
use reqwest::header::{HeaderMap, HeaderValue, CONTENT_TYPE};
|
||||
use serde::Deserialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[derive(Deserialize, Serialize)]
|
||||
struct JiraIssue {
|
||||
key: String,
|
||||
fields: JiraIssueResponseFields,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[derive(Deserialize, Serialize)]
|
||||
struct JiraIssueResponseFields {
|
||||
summary: String,
|
||||
status: Status,
|
||||
|
@ -21,7 +21,7 @@ struct JiraSearchResponse {
|
|||
total: u32,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[derive(Deserialize, Serialize)]
|
||||
struct Status {
|
||||
name: String,
|
||||
}
|
||||
|
@ -55,7 +55,7 @@ async fn list_jira_issues(
|
|||
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!("{:-<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))?;
|
||||
println!("Fetching issues assigned...");
|
||||
if !json {
|
||||
println!("Fetching issues assigned...");
|
||||
}
|
||||
|
||||
match list_jira_issues(&config).await {
|
||||
Ok(response) => {
|
||||
if response.issues.is_empty() {
|
||||
if json {
|
||||
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");
|
||||
} else {
|
||||
display_issues(&response.issues, &config.url);
|
||||
display_issues_pretty(&response.issues, &config.url);
|
||||
println!("Total issues: {}", response.total);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
project,
|
||||
markdown_file,
|
||||
} => 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 } => {
|
||||
JiraConfig::init(url, email, token).await?;
|
||||
println!("Configuration initialized successfully!");
|
||||
|
|
Loading…
Reference in a new issue