jirust_cli/
lib.rs

1#[macro_use]
2extern crate prettytable;
3
4use crate::args::commands::{Commands, JirustCliArgs};
5
6use crate::executors::jira_commands_executors::jira_version_executor::VersionExecutor;
7use clap::Parser;
8use config::config_file::ConfigFile;
9use executors::config_executor::ConfigExecutor;
10use executors::jira_commands_executors::ExecJiraCommand;
11use executors::jira_commands_executors::jira_issue_executor::IssueExecutor;
12use executors::jira_commands_executors::jira_issue_link_executor::LinkIssueExecutor;
13use executors::jira_commands_executors::jira_issue_transition_executor::IssueTransitionExecutor;
14use executors::jira_commands_executors::jira_project_executor::ProjectExecutor;
15use std::env::Args;
16use std::io::{Error, ErrorKind};
17use utils::PrintableData;
18
19pub mod args;
20pub mod config;
21pub mod executors;
22pub mod runners;
23pub mod utils;
24
25/// Manages the loading of the CLI configuration
26///
27/// # Arguments
28/// * `config_file_path` - The path to the configuration file
29/// * `args` - The arguments passed to the CLI
30///
31/// # Returns
32/// * A tuple containing the configuration file and the command to execute
33///
34/// # Errors
35/// * If the configuration file is not found
36/// * If the configuration file is missing mandatory fields
37///
38/// # Examples
39///
40/// ```no_run
41/// use jirust_cli::manage_config;
42/// use jirust_cli::config::config_file::ConfigFile;
43/// use jirust_cli::args::commands::Commands;
44///
45/// # fn main() -> Result<(), std::io::Error> {
46/// let config_file_path = String::from("config.json");
47/// let args = std::env::args();
48/// let (cfg_data, command) = manage_config(config_file_path, args)?;
49/// # Ok(())
50/// # }
51/// ```
52pub fn manage_config(
53    config_file_path: String,
54    args: Args,
55) -> Result<(ConfigFile, Commands), Error> {
56    let opts = match JirustCliArgs::try_parse_from(args) {
57        Ok(opts) => opts,
58        Err(err) => {
59            eprintln!("Error: {}", err);
60            err.exit();
61        }
62    };
63    let cfg_data = match ConfigFile::read_from_file(config_file_path.as_str()) {
64        Ok(cfg) => cfg,
65        Err(_) => {
66            return Err(Error::new(
67                ErrorKind::NotFound,
68                "Missing basic configuration, setup mandatory!",
69            ));
70        }
71    };
72    if cfg_data.get_auth_key().is_empty() || cfg_data.get_jira_url().is_empty() {
73        Err(Error::new(
74            ErrorKind::NotFound,
75            "Missing basic configuration, setup mandatory!",
76        ))
77    } else {
78        Ok((cfg_data, opts.subcmd))
79    }
80}
81
82/// Processes the command passed to the CLI
83///
84/// # Arguments
85/// * `command` - The command to execute
86/// * `config_file_path` - The path to the configuration file
87/// * `cfg_data` - The configuration file data
88///
89/// # Returns
90/// * A Result containing the result of the command execution
91///
92/// # Errors
93/// * If the command execution fails
94///
95/// # Examples
96///
97/// ```no_run
98/// use jirust_cli::process_command;
99/// use jirust_cli::config::config_file::ConfigFile;
100/// use jirust_cli::args::commands::{Commands, VersionArgs, VersionActionValues, PaginationArgs, OutputArgs};
101///
102/// # fn main() -> Result<(), std::io::Error> {
103/// let config_file_path = String::from("config.json");
104/// let args = VersionArgs {
105///   version_act: VersionActionValues::List,
106///   project_key: "project_key".to_string(),
107///   project_id: None,
108///   version_id: Some("97531".to_string()),
109///   version_name: Some("version_name".to_string()),
110///   version_description: Some("version_description".to_string()),
111///   version_start_date: None,
112///   version_release_date: None,
113///   version_archived: None,
114///   version_released: Some(true),
115///   changelog_file: None,
116///   pagination: PaginationArgs { page_size: Some(20), page_offset: None },
117///   output: OutputArgs { output_format: None, output_type: None },
118///   transition_assignee: None,
119///   transition_issues: None,
120/// };
121///
122/// let result = process_command(Commands::Version(args), config_file_path, ConfigFile::default());
123/// # Ok(())
124/// # }
125/// ```
126pub async fn process_command(
127    command: Commands,
128    config_file_path: String,
129    cfg_data: ConfigFile,
130) -> Result<Vec<PrintableData>, Box<dyn std::error::Error>> {
131    match command {
132        Commands::Config(args) => {
133            let config_executor = ConfigExecutor::new(config_file_path, args.cfg_act);
134            config_executor.exec_config_command(cfg_data).await
135        }
136        Commands::Version(args) => {
137            let version_executor = VersionExecutor::new(cfg_data, args.version_act, args);
138            version_executor.exec_jira_command().await
139        }
140        Commands::Project(args) => {
141            let project_executor = ProjectExecutor::new(cfg_data, args.project_act, args);
142            project_executor.exec_jira_command().await
143        }
144        Commands::Issue(args) => {
145            let issue_executor = IssueExecutor::new(cfg_data, args.issue_act, args);
146            issue_executor.exec_jira_command().await
147        }
148        Commands::Transition(args) => {
149            let issue_transition_executor =
150                IssueTransitionExecutor::new(cfg_data, args.transition_act, args);
151            issue_transition_executor.exec_jira_command().await
152        }
153        Commands::Link(args) => {
154            let link_issue_executor = LinkIssueExecutor::new(cfg_data, args.link_act, args);
155            link_issue_executor.exec_jira_command().await
156        }
157    }
158}