1#[macro_use]
2extern crate prettytable;
3
4use crate::args::commands::Commands;
5use crate::executors::jira_commands_executors::jira_version_executor::VersionExecutor;
6
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
17#[cfg(target_family = "wasm")]
18use args::commands::JirustCliArgs;
19#[cfg(target_family = "wasm")]
20use clap::Parser;
21#[cfg(target_family = "wasm")]
22use wasm_bindgen::{JsValue, prelude::wasm_bindgen};
23#[cfg(target_family = "wasm")]
24use wasm_bindgen_futures::js_sys;
25
26pub mod args;
27pub mod config;
28pub mod executors;
29pub mod runners;
30pub mod utils;
31
32#[cfg(test)]
33pub mod tests;
34
35pub fn manage_config(config_file_path: String) -> Result<ConfigFile, Error> {
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)
78 }
79}
80
81pub async fn process_command(
125 command: Commands,
126 config_file_path: Option<String>,
127 cfg_data: ConfigFile,
128) -> Result<Vec<PrintableData>, Box<dyn std::error::Error>> {
129 match command {
130 Commands::Config(args) => match config_file_path {
131 Some(path) => {
132 let config_executor = ConfigExecutor::new(path, args.cfg_act);
133 config_executor.exec_config_command(cfg_data).await
134 }
135 None => Err(Box::new(Error::new(
136 ErrorKind::NotFound,
137 "Missing config file path!",
138 ))),
139 },
140 Commands::Version(args) => {
141 let version_executor = VersionExecutor::new(cfg_data, args.version_act, args);
142 version_executor.exec_jira_command().await
143 }
144 Commands::Project(args) => {
145 let project_executor = ProjectExecutor::new(cfg_data, args.project_act, args);
146 project_executor.exec_jira_command().await
147 }
148 Commands::Issue(args) => {
149 let issue_executor = IssueExecutor::new(cfg_data, args.issue_act, args);
150 issue_executor.exec_jira_command().await
151 }
152 Commands::Transition(args) => {
153 let issue_transition_executor =
154 IssueTransitionExecutor::new(cfg_data, args.transition_act, args);
155 issue_transition_executor.exec_jira_command().await
156 }
157 Commands::Link(args) => {
158 let link_issue_executor = LinkIssueExecutor::new(cfg_data, args.link_act, args);
159 link_issue_executor.exec_jira_command().await
160 }
161 }
162}
163
164#[cfg(target_family = "wasm")]
165pub fn set_panic_hook() {
166 #[cfg(feature = "console_error_panic_hook")]
167 console_error_panic_hook::set_once();
168}
169
170#[cfg(target_family = "wasm")]
171#[wasm_bindgen]
172pub async fn run(js_args: js_sys::Array, js_cfg: JsValue) -> JsValue {
173 set_panic_hook();
174
175 let mut args: Vec<String> = vec!["jirust-cli".to_string()];
178
179 args.append(&mut js_args.iter().filter_map(|el| el.as_string()).collect());
181
182 let opts = match JirustCliArgs::try_parse_from(args) {
183 Ok(opts) => opts,
184 Err(err) => {
185 let err_s = format!("Error: {err}");
186 return serde_wasm_bindgen::to_value(&err_s).unwrap_or(JsValue::NULL);
187 }
188 };
189
190 let cfg_data: ConfigFile = serde_wasm_bindgen::from_value(js_cfg).expect("Config must be set!");
191
192 let result = process_command(opts.subcmd, None, cfg_data).await;
193
194 match result {
195 Ok(data) => serde_wasm_bindgen::to_value(&data).unwrap_or(JsValue::NULL),
196 Err(err) => {
197 let err_s = format!("Error: {err}");
198 serde_wasm_bindgen::to_value(&err_s).unwrap_or(JsValue::NULL)
199 }
200 }
201}