Add a search command

This commit is contained in:
Daan Boerlage 2025-01-21 20:44:38 +01:00
parent 8423cc1240
commit 0c459e2770
Signed by: daan
GPG key ID: FCE070E1E4956606
5 changed files with 80 additions and 12 deletions

View file

@ -10,27 +10,37 @@ pub struct Cli {
#[derive(Subcommand)]
pub enum Commands {
/// Create a ticket
/// Create an issue
Create {
/// The project key in which to create the ticket
/// The project key in which to create the issue
#[arg(long)]
project: Option<String>,
/// Open the new ticket in a browser
/// Open the new issue in a browser
#[arg(long)]
open: bool,
/// A markdown file
/// A Markdown file
#[arg(value_name = "MARKDOWN_FILE")]
markdown_file: Option<PathBuf>,
},
/// Find tickets currently assigned to you
/// Find issues currently assigned to you
List {
/// Print json rather than pretty print
/// Print JSON rather than pretty print
#[arg(long)]
json: bool,
},
/// Setup the configuration
/// Search for issues
Search {
/// Print JSON rather than pretty print
#[arg(long)]
json: bool,
/// A JQL string
#[arg(value_name = "JQL")]
jql: String,
},
/// Set up the configuration
Init {
/// Jira instance URL
#[arg(long)]

View file

@ -1,2 +1,3 @@
pub mod create;
pub mod list;
pub mod search;

32
src/cmd/search.rs Normal file
View file

@ -0,0 +1,32 @@
use crate::jira_config::JiraConfig;
use crate::types::issue::{display_issues_json, display_issues_pretty};
pub async fn exec(json: bool, jql: String) -> Result<(), Box<dyn std::error::Error>> {
let config = JiraConfig::load().map_err(|e| format!("Configuration error: {}", e))?;
if !json {
println!("Searching for issues...");
}
match crate::jql::run(&config, &jql).await {
Ok(response) => {
if json {
if response.issues.is_empty() {
println!("[]");
} else {
display_issues_json(&response.issues)?;
}
} else if response.issues.is_empty() {
println!("No results found for query.");
} else {
display_issues_pretty(&response.issues)?;
println!("Total issues: {}", response.total);
}
}
Err(e) => {
eprintln!("Error fetching issues: {}", e);
std::process::exit(1);
}
}
Ok(())
}

View file

@ -19,6 +19,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
markdown_file,
} => cmd::create::create(project, open, markdown_file).await?,
Commands::List { json } => cmd::list::list(json).await?,
Commands::Search { json, jql } => cmd::search::exec(json, jql).await?,
Commands::Init { url, email, token } => {
JiraConfig::init(url, email, token).await?;
println!("Configuration initialized successfully!");