jirust_cli/
lib.rs

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