1#[macro_use]
5extern crate prettytable;
6
7use crate::args::commands::Commands;
8use crate::executors::jira_commands_executors::jira_version_executor::VersionExecutor;
9
10use config::config_file::ConfigFile;
11use executors::config_executor::ConfigExecutor;
12use executors::jira_commands_executors::ExecJiraCommand;
13use executors::jira_commands_executors::jira_issue_executor::IssueExecutor;
14use executors::jira_commands_executors::jira_issue_link_executor::LinkIssueExecutor;
15use executors::jira_commands_executors::jira_issue_transition_executor::IssueTransitionExecutor;
16use executors::jira_commands_executors::jira_project_executor::ProjectExecutor;
17use std::io::{Error, ErrorKind};
18use utils::PrintableData;
19
20#[cfg(target_family = "wasm")]
21use args::commands::JirustCliArgs;
22#[cfg(target_family = "wasm")]
23use clap::Parser;
24#[cfg(target_family = "wasm")]
25use wasm_bindgen::{JsValue, prelude::wasm_bindgen};
26#[cfg(target_family = "wasm")]
27use wasm_bindgen_futures::js_sys;
28
29pub mod args;
31pub mod config;
33pub mod executors;
35pub mod runners;
37pub mod utils;
39
40#[cfg(test)]
41pub mod tests;
42
43pub fn manage_config(config_file_path: String) -> Result<ConfigFile, Error> {
70 let cfg_data = match ConfigFile::read_from_file(config_file_path.as_str()) {
71 Ok(cfg) => cfg,
72 Err(_) => {
73 return Err(Error::new(
74 ErrorKind::NotFound,
75 "Missing basic configuration, setup mandatory!",
76 ));
77 }
78 };
79 if cfg_data.get_auth_key().is_empty() || cfg_data.get_jira_url().is_empty() {
80 Err(Error::new(
81 ErrorKind::NotFound,
82 "Missing basic configuration, setup mandatory!",
83 ))
84 } else {
85 Ok(cfg_data)
86 }
87}
88
89pub async fn process_command(
133 command: Commands,
134 config_file_path: Option<String>,
135 cfg_data: ConfigFile,
136) -> Result<Vec<PrintableData>, Box<dyn std::error::Error>> {
137 match command {
138 Commands::Config(args) => match config_file_path {
139 Some(path) => {
140 let config_executor = ConfigExecutor::new(path, args.cfg_act);
141 config_executor.exec_config_command(cfg_data).await
142 }
143 None => Err(Box::new(Error::new(
144 ErrorKind::NotFound,
145 "Missing config file path!",
146 ))),
147 },
148 Commands::Version(args) => {
149 let version_executor = VersionExecutor::new(cfg_data, args.version_act, args);
150 version_executor.exec_jira_command().await
151 }
152 Commands::Project(args) => {
153 let project_executor = ProjectExecutor::new(cfg_data, args.project_act, args);
154 project_executor.exec_jira_command().await
155 }
156 Commands::Issue(args) => {
157 let issue_executor = IssueExecutor::new(cfg_data, args.issue_act, args);
158 issue_executor.exec_jira_command().await
159 }
160 Commands::Transition(args) => {
161 let issue_transition_executor =
162 IssueTransitionExecutor::new(cfg_data, args.transition_act, args);
163 issue_transition_executor.exec_jira_command().await
164 }
165 Commands::Link(args) => {
166 let link_issue_executor = LinkIssueExecutor::new(cfg_data, args.link_act, args);
167 link_issue_executor.exec_jira_command().await
168 }
169 }
170}
171
172#[cfg(target_family = "wasm")]
173pub fn set_panic_hook() {
174 #[cfg(feature = "console_error_panic_hook")]
175 console_error_panic_hook::set_once();
176}
177
178#[cfg(target_family = "wasm")]
179#[wasm_bindgen]
180pub async fn run(js_args: js_sys::Array, js_cfg: JsValue) -> JsValue {
181 set_panic_hook();
182
183 let mut args: Vec<String> = vec!["jirust-cli".to_string()];
186
187 args.append(&mut js_args.iter().filter_map(|el| el.as_string()).collect());
189
190 let opts = match JirustCliArgs::try_parse_from(args) {
191 Ok(opts) => opts,
192 Err(err) => {
193 let err_s = format!("Error: {err}");
194 return serde_wasm_bindgen::to_value(&err_s).unwrap_or(JsValue::NULL);
195 }
196 };
197
198 let cfg_data: ConfigFile = serde_wasm_bindgen::from_value(js_cfg).expect("Config must be set!");
199
200 let result = process_command(opts.subcmd, None, cfg_data).await;
201
202 match result {
203 Ok(data) => serde_wasm_bindgen::to_value(&data).unwrap_or(JsValue::NULL),
204 Err(err) => {
205 let err_s = format!("Error: {err}");
206 serde_wasm_bindgen::to_value(&err_s).unwrap_or(JsValue::NULL)
207 }
208 }
209}