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