diff --git a/src/cli/group/cli_logic.rs b/src/cli/group/cli_logic.rs index e335021..3a590e9 100644 --- a/src/cli/group/cli_logic.rs +++ b/src/cli/group/cli_logic.rs @@ -8,7 +8,7 @@ use clap::ArgMatches; use rayon::prelude::*; -use jira_cli::{Global, library::group}; +use jira_cli::{group, Global}; pub fn add_user(global: &Global, args: &ArgMatches) { let account_ids: Vec<&String> = args diff --git a/src/cli/issue/cli_logic.rs b/src/cli/issue/cli_logic.rs index eceade2..dc8d084 100644 --- a/src/cli/issue/cli_logic.rs +++ b/src/cli/issue/cli_logic.rs @@ -8,8 +8,7 @@ use clap::ArgMatches; use rayon::prelude::*; -use jira_cli::{Global, library::issue}; - +use jira_cli::{issue, Global}; pub fn add_label(global: &Global, args: &ArgMatches) { let issue_keys: Vec<&String> = args diff --git a/src/cli/labels.rs b/src/cli/labels.rs index a2433d5..a0f2897 100644 --- a/src/cli/labels.rs +++ b/src/cli/labels.rs @@ -7,8 +7,9 @@ use clap::{Arg, ArgMatches, Command}; +use jira_cli::labels::list; + use crate::Global; -use jira_cli::library::labels::list_labels; pub fn cli_commands() -> Command { Command::new("labels") @@ -29,7 +30,7 @@ pub fn cli_commands() -> Command { } pub fn logic_commands(global: &Global, args: &ArgMatches) { - list_labels( + list( global.domain.as_str(), global.user.as_str(), global.token.as_str(), diff --git a/src/cli.rs b/src/cli/mod.rs similarity index 100% rename from src/cli.rs rename to src/cli/mod.rs index 2f3f7dc..d191daa 100644 --- a/src/cli.rs +++ b/src/cli/mod.rs @@ -5,11 +5,6 @@ * SPDX-License-Identifier: GPL-2.0-only */ -use clap::{ - crate_authors, crate_description, crate_name, crate_version, value_parser, Arg, Command, -}; -use clap_complete::Shell; - pub mod check_version; pub mod group; pub mod issue; @@ -18,6 +13,11 @@ pub mod license; pub mod project; pub mod user; +use clap::{ + crate_authors, crate_description, crate_name, crate_version, value_parser, Arg, Command, +}; +use clap_complete::Shell; + fn generate() -> Command { Command::new("generate") .about("Generate autocompletion script for your shell") diff --git a/src/cli/project/cli_logic.rs b/src/cli/project/cli_logic.rs index 1cd86c8..9d18346 100644 --- a/src/cli/project/cli_logic.rs +++ b/src/cli/project/cli_logic.rs @@ -8,7 +8,7 @@ use clap::ArgMatches; use rayon::prelude::*; -use jira_cli::{Global, library::project}; +use jira_cli::{project, Global}; pub fn create(global: &Global, args: &ArgMatches) { project::create( @@ -24,7 +24,7 @@ pub fn create(global: &Global, args: &ArgMatches) { } pub fn delete_project(global: &Global, args: &ArgMatches) { - project::delete_project( + project::delete( global, args.get_one::("project_key").unwrap().as_str(), ); diff --git a/src/cli/user/cli_logic.rs b/src/cli/user/cli_logic.rs index 168a3fe..cc9d1ee 100644 --- a/src/cli/user/cli_logic.rs +++ b/src/cli/user/cli_logic.rs @@ -6,7 +6,8 @@ */ use clap::ArgMatches; -use jira_cli::{Global, library::user}; + +use jira_cli::{user, Global}; pub fn create(global: &Global, args: &ArgMatches) { user::create( diff --git a/src/lib.rs b/src/lib.rs deleted file mode 100644 index 729a046..0000000 --- a/src/lib.rs +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (c) 2022, sycured - * All rights reserved - * - * SPDX-License-Identifier: GPL-2.0-only - */ - -#![forbid(unsafe_code)] - -pub mod library; - -use std::collections::HashMap; - -use attohttpc::{delete, get, post, put, Error, Response}; -use base64::{engine::general_purpose as b64, Engine}; -use comfy_table::{ - modifiers::UTF8_ROUND_CORNERS, presets::UTF8_FULL, Cell, CellAlignment, ContentArrangement, - Table, -}; -use dialoguer::Confirm; -use serde_json::Value; -use zeroize::{Zeroize, ZeroizeOnDrop}; - -#[derive(Zeroize, ZeroizeOnDrop)] -pub struct Global { - pub domain: String, - pub user: String, - pub token: String, -} - -fn b64auth(user: &str, token: &str) -> String { - b64::STANDARD.encode(format!("{user}:{token}")) -} - -#[allow(clippy::missing_panics_doc)] -#[must_use] -pub fn confirm(prompt: String) -> bool { - Confirm::new().with_prompt(prompt).interact().unwrap() -} - -#[allow(clippy::missing_panics_doc)] -pub fn create_and_print_table( - header: Vec<&str>, - column_alignment: &HashMap, - rows: Vec>, -) { - let mut table = Table::new(); - table - .set_header(header) - .load_preset(UTF8_FULL) - .apply_modifier(UTF8_ROUND_CORNERS) - .set_content_arrangement(ContentArrangement::DynamicFullWidth); - for (key, value) in column_alignment.iter() { - table.column_mut(*key).unwrap().set_cell_alignment(*value); - } - for row in rows { - table.add_row(row); - } - println!("{table}"); -} - -#[allow(clippy::missing_errors_doc)] -pub fn delete_request(url: &str, user: &str, token: &str) -> Result { - delete(url) - .header( - "Authorization", - format!("Basic {b64}", b64 = b64auth(user, token)), - ) - .send()? - .error_for_status() -} - -#[allow(clippy::missing_errors_doc)] -pub fn get_request(url: &str, user: &str, token: &str) -> Result { - get(url) - .header("Accept", "application/json") - .header_append( - "Authorization", - format!("Basic {b64}", b64 = b64auth(user, token)), - ) - .send()? - .error_for_status() -} - -#[allow(clippy::missing_errors_doc)] -pub fn post_request( - url: &str, - payload: &Value, - user: &str, - token: &str, -) -> Result { - post(url) - .header("Accept", "application/json") - .header_append( - "Authorization", - format!("Basic {b64}", b64 = b64auth(user, token)), - ) - .json(payload)? - .send()? - .error_for_status() -} - -#[allow(clippy::missing_errors_doc)] -pub fn put_request(url: &str, payload: &Value, user: &str, token: &str) -> Result { - put(url) - .header("Accept", "application/json") - .header_append( - "Authorization", - format!("Basic {b64}", b64 = b64auth(user, token)), - ) - .json(payload)? - .send()? - .error_for_status() -} diff --git a/src/library/group.rs b/src/library/group.rs index 231a26e..4e1939e 100644 --- a/src/library/group.rs +++ b/src/library/group.rs @@ -11,7 +11,9 @@ use comfy_table::{Cell, CellAlignment}; use rayon::prelude::*; use serde_json::{json, Value}; -use crate::{confirm, create_and_print_table, delete_request, get_request, post_request, library::urls::URLS, Global}; +use crate::{ + confirm, create_and_print_table, delete_request, get_request, post_request, urls::URLS, Global, +}; pub fn add_user(global: &Global, account_id: &str, group_id: &str) { let url: String = format!( @@ -62,6 +64,7 @@ pub fn delete(global: &Global, group_id: &str) { } //noinspection DuplicatedCode +#[allow(clippy::missing_panics_doc)] pub fn find(global: &Global, query: &str) { let url: String = format!( "https://{}{}/picker?query={query}", @@ -95,6 +98,7 @@ pub fn find(global: &Global, query: &str) { } //noinspection DuplicatedCode +#[allow(clippy::missing_panics_doc)] pub fn list_groups(global: &Global, start_at: &str, max_results: &str) { let url: String = format!( "https://{}{}/bulk?startAt={start_at}&maxResults={max_results}", @@ -128,6 +132,7 @@ pub fn list_groups(global: &Global, start_at: &str, max_results: &str) { } //noinspection DuplicatedCode +#[allow(clippy::missing_panics_doc)] pub fn list_users( global: &Global, group_id: &str, diff --git a/src/library/issue.rs b/src/library/issue.rs index 1142b1d..cbaa137 100644 --- a/src/library/issue.rs +++ b/src/library/issue.rs @@ -11,7 +11,10 @@ use comfy_table::{Cell, CellAlignment}; use rayon::prelude::*; use serde_json::{json, Value}; -use crate::{create_and_print_table, delete_request, Global, get_request, post_request, put_request,library::urls::URLS}; +use crate::{ + create_and_print_table, delete_request, get_request, post_request, put_request, urls::URLS, + Global, +}; pub fn add_label(global: &Global, issue_key: &str, label: &str) { let url: String = format!("https://{}{}/{issue_key}", global.domain, URLS["issue"]); @@ -84,7 +87,7 @@ pub fn assign(global: &Global, issue_key: &str, account_id: &str, success_messag } } -#[allow(clippy::too_many_arguments)] +#[allow(clippy::missing_panics_doc)] pub fn create( global: &Global, reporter_account_id: &str, @@ -125,6 +128,7 @@ pub fn create( } } +#[allow(clippy::missing_panics_doc)] pub fn create_link_type(global: &Global, name: &str, inward: &str, outward: &str) { let url: String = format!("https://{}{}", global.domain, URLS["issue_link_types"]); let payload: Value = json!({ @@ -185,6 +189,7 @@ pub fn delete_link_type(global: &Global, link_type_id: &str) { } //noinspection DuplicatedCode +#[allow(clippy::missing_panics_doc)] pub fn get_link_type(global: &Global, link_type_id: &str) { let url: String = format!( "https://{}{}/{link_type_id}", @@ -222,6 +227,7 @@ pub fn get_link_type(global: &Global, link_type_id: &str) { } } +#[allow(clippy::missing_panics_doc)] pub fn get_transitions(global: &Global, issue_key: &str) { let url: String = format!( "https://{}{}/{issue_key}/transitions", @@ -260,6 +266,7 @@ pub fn get_transitions(global: &Global, issue_key: &str) { } //noinspection DuplicatedCode +#[allow(clippy::missing_panics_doc)] pub fn list_link_types(global: &Global) { let url: String = format!("https://{}{}", global.domain, URLS["issue_link_types"]); match get_request(&url, global.user.as_str(), global.token.as_str()) { @@ -296,6 +303,7 @@ pub fn list_link_types(global: &Global) { } } +#[allow(clippy::missing_panics_doc)] pub fn list_priorities(global: &Global) { let url: String = format!("https://{}{}", global.domain, URLS["priority"]); match get_request(&url, global.user.as_str(), global.token.as_str()) { @@ -312,6 +320,7 @@ pub fn list_priorities(global: &Global) { } } +#[allow(clippy::missing_panics_doc)] pub fn list_types(global: &Global, project_key: &str) { let url: String = format!( "https://{}{}/createmeta?projectKeys={project_key}", @@ -336,6 +345,7 @@ pub fn list_types(global: &Global, project_key: &str) { } //noinspection DuplicatedCode +#[allow(clippy::missing_panics_doc)] pub fn list_votes(global: &Global, issue_key: &str) { let url: String = format!( "https://{}{}/{issue_key}/votes", @@ -435,6 +445,7 @@ pub fn remove_vote(global: &Global, issue_key: &str) { } } +#[allow(clippy::missing_panics_doc)] pub fn show_fixversions(global: &Global, issue_key: &str) { let url: String = format!("https://{}{}/{issue_key}", global.domain, URLS["issue"]); match get_request(&url, global.user.as_str(), global.token.as_str()) { diff --git a/src/library/labels.rs b/src/library/labels.rs index c7e211f..25bafec 100644 --- a/src/library/labels.rs +++ b/src/library/labels.rs @@ -6,11 +6,14 @@ */ use std::process::exit; + use rayon::prelude::*; use serde_json::Value; -use crate::{get_request, library::urls::URLS}; -pub fn list_labels(domain: &str, user: &str, token: &str, start_at: &str, max_results: &str) { +use crate::{get_request, urls::URLS}; + +#[allow(clippy::missing_panics_doc)] +pub fn list(domain: &str, user: &str, token: &str, start_at: &str, max_results: &str) { let url: String = format!( "https://{}{}?startAt={}&maxResults={}", domain, URLS["label"], start_at, max_results diff --git a/src/library/mod.rs b/src/library/mod.rs index dd7bac4..57c614e 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -5,6 +5,8 @@ * SPDX-License-Identifier: GPL-2.0-only */ +#![forbid(unsafe_code)] + pub mod group; pub mod issue; pub mod labels; diff --git a/src/library/project.rs b/src/library/project.rs index 05011c5..668f8a8 100644 --- a/src/library/project.rs +++ b/src/library/project.rs @@ -13,10 +13,10 @@ use rayon::prelude::*; use serde_json::{json, Value}; use crate::{ - confirm, create_and_print_table, delete_request, get_request, post_request, put_request, library::urls::URLS, Global + confirm, create_and_print_table, delete_request, get_request, post_request, put_request, + urls::URLS, Global, }; - #[allow(clippy::too_many_arguments)] pub fn create( global: &Global, @@ -45,7 +45,7 @@ pub fn create( } #[allow(clippy::unit_arg)] -pub fn delete_project(global: &Global, project_key: &str) { +pub fn delete(global: &Global, project_key: &str) { let url: String = format!("https://{}{}/{project_key}", global.domain, URLS["project"]); if confirm(format!( "Are you sure you want to delete the project key: {project_key}?" @@ -62,6 +62,7 @@ pub fn delete_project(global: &Global, project_key: &str) { } } +#[allow(clippy::missing_panics_doc)] pub fn get_id(global: &Global, project_key: &str) { let url: String = format!("https://{}{}/{project_key}", global.domain, URLS["project"]); match get_request(&url, global.user.as_str(), global.token.as_str()) { @@ -76,6 +77,7 @@ pub fn get_id(global: &Global, project_key: &str) { } } +#[allow(clippy::missing_panics_doc)] pub fn list_features(global: &Global, project_key: &str) { let url: String = format!( "https://{}{}/{project_key}/features", @@ -120,6 +122,7 @@ pub fn list_features(global: &Global, project_key: &str) { } } +#[allow(clippy::missing_panics_doc)] pub fn list_versions(global: &Global, project_key: &str) { let url: String = format!( "https://{}{}/{project_key}/versions", @@ -173,6 +176,7 @@ pub fn list_versions(global: &Global, project_key: &str) { } } +#[allow(clippy::missing_panics_doc)] pub fn new_version(global: &Global, project_id: &str, version_name: &str) { let url: String = format!("https://{}{}", global.domain, URLS["version"]); let payload: Value = json!({ diff --git a/src/library/user.rs b/src/library/user.rs index f04ca32..70b1517 100644 --- a/src/library/user.rs +++ b/src/library/user.rs @@ -10,8 +10,7 @@ use std::process::exit; use rayon::prelude::*; use serde_json::{json, Value}; -use crate::{confirm, delete_request, get_request, Global, post_request, library::urls::URLS}; - +use crate::{confirm, delete_request, get_request, post_request, urls::URLS, Global}; pub fn create(global: &Global, email: &str, display_name: &str) { let url = format!("https://{}{}", global.domain, URLS["user"]); @@ -49,6 +48,7 @@ pub fn delete(global: &Global, account_id: &str) { } } +#[allow(clippy::missing_panics_doc)] pub fn get_account_id(global: &Global, email_address: &str) { let url: String = format!( "https://{}{}?query={email_address}", @@ -67,6 +67,7 @@ pub fn get_account_id(global: &Global, email_address: &str) { } } +#[allow(clippy::missing_panics_doc)] pub fn get_user_groups(global: &Global, account_id: &str) { let url: String = format!( "https://{}{}/groups?accountId={account_id}", diff --git a/src/main.rs b/src/main.rs index 893ccd5..9b0c93f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,8 @@ #![forbid(unsafe_code)] +mod cli; + use std::io::stdout; use clap::Command; @@ -14,15 +16,15 @@ use clap_complete::{generate, Generator, Shell}; use human_panic::setup_panic; #[cfg(not(target_os = "windows"))] use jemallocator::Jemalloc; + use jira_cli::Global; + use crate::cli::{check_version, group, issue, labels, license, project, user}; #[cfg(not(target_os = "windows"))] #[global_allocator] static GLOBAL: Jemalloc = Jemalloc; -mod cli; - fn print_completions(gen: G, cmd: &mut Command) { generate(gen, cmd, cmd.get_name().to_owned(), &mut stdout()); }