Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/config/cache.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::api;
use crate::{api, prelude::Result};

/// Get the user account id from email provided by user while config creation.
/// For most of the API Call, user email will not be valid due to recent changes in GDPR policies.
Expand All @@ -13,7 +13,7 @@ use crate::api;
/// ```
/// let account_id = get_username(&configuration);
/// ```
pub fn get_username(configuration: &json::JsonValue) -> String {
pub fn get_username(configuration: &json::JsonValue) -> Result<String> {
let url = format!(
"user/search?query={}",
configuration["email"].as_str().unwrap()
Expand All @@ -27,6 +27,8 @@ pub fn get_username(configuration: &json::JsonValue) -> String {
version: 3,
};
let response = api::get(api_request).unwrap();
let account_id = String::from(response[0]["accountId"].as_str().unwrap());
account_id
match response[0]["accountId"].as_str(){
Some(acc_id) => Ok(String::from(acc_id)),
None => Err("Authentication Failed".into())
}
}
17 changes: 10 additions & 7 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::io;
use std::io::Read;
use std::io::Write;

use crate::prelude::Result;
mod cache;

/// Capitalize first letter of a word.
Expand Down Expand Up @@ -35,12 +36,12 @@ fn get_config_file_name() -> String {
/// ```
/// assert!(check_config_exists());
/// ```
fn check_config_exists() -> bool {
fs::metadata(get_config_file_name()).is_ok()
fn check_config_exists() -> Result<bool> {
Ok(fs::metadata(get_config_file_name()).is_ok())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary if Ok is returned always?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just to propagate the error from create config all the way to main, so it doesn't panic

Also, in the future there's value in adding a proper cli library like clap

}

/// Create configuration file by asking user with the required information.
fn create_config() {
fn create_config() -> Result<()> {
// Ask for the config file and create a new file.
let mut namespace = String::new();
let mut email = String::new();
Expand Down Expand Up @@ -68,9 +69,10 @@ fn create_config() {
alias: {},
transitions: {}
};
let account_id = cache::get_username(&configuration);
let account_id = cache::get_username(&configuration)?;
configuration["account_id"] = account_id.into();
write_config(configuration);
Ok(())
}

/// Write the updated configuration to the file.
Expand Down Expand Up @@ -285,11 +287,12 @@ pub fn transition_exists(project_code: String, transition_name: String) -> bool
/// Ensure the config exists.
/// It will first check the config file exists.
/// If it does not, it will ask the user to create one.
pub fn ensure_config() {
let config_exists = check_config_exists();
pub fn ensure_config() -> Result<()> {
let config_exists = check_config_exists()?;
if !config_exists {
create_config();
create_config()?;
}
Ok(())
}

/// List all the provided alias.
Expand Down
7 changes: 4 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ use clap::App;
pub mod api;
pub mod config;
pub mod jira;
pub mod prelude;
pub mod subcommands;

fn main() {
config::ensure_config();
fn main() -> prelude::Result<()> {
config::ensure_config()?;
let app = App::new("JIRA Terminal")
.version(crate_version!())
.author("Amrit Ghimire <oss@amritghimire.com>")
Expand All @@ -45,6 +46,6 @@ fn main() {
.subcommand(subcommands::update::subcommand())
.subcommand(subcommands::autocompletion::subcommand())
.subcommand(subcommands::new_subcommand::subcommand());

subcommands::handle_matches(app);
Ok(())
}
1 change: 1 addition & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub type Result<T> = core::result::Result<T, Box<dyn std::error::Error>>;
Loading