Skip to content

Commit

Permalink
Merge pull request #19 from hiro-o918/feature/not-serlect-profile
Browse files Browse the repository at this point in the history
ignore profiles which is not selected
  • Loading branch information
hiro-o918 authored Mar 4, 2022
2 parents 5c2188d + b31e5eb commit be061cd
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "awsctx"
version = "0.2.5"
version = "0.3.0"
authors = ["Hironoiri Yamamoto <[email protected]>"]
edition = "2018"
description = "Context Manager for AWS Profiles"
Expand Down
13 changes: 7 additions & 6 deletions src/aws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<ctx::Context>()
Expand Down
17 changes: 7 additions & 10 deletions src/creds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -78,12 +78,9 @@ impl Credentials {

pub fn set_default_profile(&mut self, name: &str) -> Result<Profile, ctx::CTXError> {
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(),
Expand All @@ -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(())
}

Expand Down
6 changes: 2 additions & 4 deletions src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
}
Expand Down
16 changes: 13 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit be061cd

Please sign in to comment.