From ac506bb8027c88df4311501afc03121b3d6a089d Mon Sep 17 00:00:00 2001 From: Hironori Yamamoto Date: Fri, 4 Mar 2022 23:11:40 +0900 Subject: [PATCH 1/3] :recycle: refactor errors --- src/creds.rs | 17 +++++++---------- src/ctx.rs | 6 ++---- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/creds.rs b/src/creds.rs index 42755f6..f57c951 100644 --- a/src/creds.rs +++ b/src/creds.rs @@ -66,7 +66,7 @@ impl Credentials { let items = self.data .get(&format!("[{}]", name)) - .ok_or(ctx::CTXError::UnknownContextName { + .ok_or(ctx::CTXError::InvalidArgument { source: anyhow!(format!("unknown context name: {}", name)), })?; Ok(Profile { @@ -78,12 +78,9 @@ impl Credentials { pub fn set_default_profile(&mut self, name: &str) -> Result { let key = name_to_profile_key(name); - let items = self - .data - .get(&key) - .ok_or(ctx::CTXError::UnknownContextName { - source: anyhow!(format!("unknown context name: {}", name)), - })?; + let items = self.data.get(&key).ok_or(ctx::CTXError::InvalidArgument { + source: anyhow!(format!("unknown context name: {}", name)), + })?; self.current_key = Some(key); Ok(Profile { name: name.into(), @@ -97,11 +94,11 @@ impl Credentials { credentials_path: P, ) -> Result<(), ctx::CTXError> { let mut file = fs::File::create(credentials_path) - .map_err(|e| ctx::CTXError::IOError { source: e.into() })?; + .map_err(|e| ctx::CTXError::UnexpectedError { source: e.into() })?; file.write_all(self.to_string().as_bytes()) - .map_err(|e| ctx::CTXError::IOError { source: e.into() })?; + .map_err(|e| ctx::CTXError::UnexpectedError { source: e.into() })?; file.flush() - .map_err(|e| ctx::CTXError::IOError { source: e.into() })?; + .map_err(|e| ctx::CTXError::UnexpectedError { source: e.into() })?; Ok(()) } diff --git a/src/ctx.rs b/src/ctx.rs index 4c7e003..d7753ac 100644 --- a/src/ctx.rs +++ b/src/ctx.rs @@ -17,10 +17,8 @@ pub enum CTXError { ConfigurationIsBroken { source: anyhow::Error }, #[error("Invalid input")] InvalidArgument { source: anyhow::Error }, - #[error("Unknown context name")] - UnknownContextName { source: anyhow::Error }, - #[error("IOError")] - IOError { source: anyhow::Error }, + #[error("No context is selected")] + NoContextIsSelected {}, #[error("Unexpected error")] UnexpectedError { source: anyhow::Error }, } From 202f653ed8a64913119ed9a57334ec92354b96a7 Mon Sep 17 00:00:00 2001 From: Hironori Yamamoto Date: Fri, 4 Mar 2022 23:57:12 +0900 Subject: [PATCH 2/3] :bug: select no profile when any key other than the Enter key is pressed --- src/aws.rs | 13 +++++++------ src/main.rs | 16 +++++++++++++--- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/aws.rs b/src/aws.rs index 63b747b..a11f423 100644 --- a/src/aws.rs +++ b/src/aws.rs @@ -3,7 +3,7 @@ use crate::ctx; use anyhow::{anyhow, Result}; use dirs::home_dir; -use skim::prelude::{unbounded, SkimOptionsBuilder}; +use skim::prelude::{unbounded, Key, SkimOptionsBuilder}; use skim::{Skim, SkimItemReceiver, SkimItemSender}; use std::path::PathBuf; use std::sync::Arc; @@ -65,13 +65,14 @@ impl ctx::CTX for AWS { drop(tx_item); let selected_items = Skim::run_with(&options, Some(rx_item)) - .map(|out| out.selected_items) - .unwrap_or_else(Vec::new); + .map(|out| match out.final_key { + Key::Enter => Ok(out.selected_items), + _ => Err(ctx::CTXError::NoContextIsSelected {}), + }) + .unwrap_or(Ok(Vec::new()))?; let item = selected_items .get(0) - .ok_or(ctx::CTXError::InvalidArgument { - source: anyhow!("no context is selected"), - })?; + .ok_or(ctx::CTXError::NoContextIsSelected {})?; let context = (*item) .as_any() .downcast_ref::() diff --git a/src/main.rs b/src/main.rs index b1373ab..51b8e75 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,10 @@ use std::io; -use awsctx::{aws::AWS, ctx::CTX, view::show_contexts}; +use awsctx::{ + aws::AWS, + ctx::{CTXError, CTX}, + view::show_contexts, +}; use clap::{IntoApp, Parser, Subcommand}; use clap_complete::{generate, Generator, Shell}; @@ -46,9 +50,15 @@ fn main() { Opts::UseContext { profile } => { aws.use_context(profile.as_str()).unwrap(); } - Opts::UseContextByInteractiveFinder {} => { - aws.use_context_interactive().unwrap(); + Opts::UseContextByInteractiveFinder {} => match aws.use_context_interactive() { + Ok(_) => Ok(()), + Err(err) => match err { + CTXError::NoContextIsSelected {} => Ok(()), + _ => Err(err), + }, } + .unwrap(), + Opts::ListContexts {} => { let contexts = aws.list_contexts().unwrap(); show_contexts(&contexts) From b31e5eb6e78cc37eefc714b86618587e368d0a5c Mon Sep 17 00:00:00 2001 From: Hironori Yamamoto Date: Fri, 4 Mar 2022 23:58:48 +0900 Subject: [PATCH 3/3] :arrow_up: bumpup verson --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2921656..edac5b0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -51,7 +51,7 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "awsctx" -version = "0.2.5" +version = "0.3.0" dependencies = [ "ansi_term", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index 03adfcf..ae55187 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "awsctx" -version = "0.2.5" +version = "0.3.0" authors = ["Hironoiri Yamamoto "] edition = "2018" description = "Context Manager for AWS Profiles"