Skip to content

Commit

Permalink
Add thiserror error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nicmr committed Jan 30, 2024
1 parent dc8c580 commit 53cb819
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 51 deletions.
54 changes: 33 additions & 21 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ serde_yaml = "0.9.3"
serde = { version = "1.0.141", features = ["derive"] }
lazy_static = "1.4.0"
skim = "0.9.4"
thiserror = "1.0.56"

[dev-dependencies]
assert_cmd = "2.0"
20 changes: 2 additions & 18 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::model::KubeConfig;
use crate::{error::SetContextError, model::KubeConfig};
use crate::config;

use core::fmt;
use std::{
io::Cursor,
process::{Command, Stdio},
Expand Down Expand Up @@ -92,21 +91,6 @@ pub fn set_context(ctx: &str, temp_dir: &str, config: &KubeConfig) -> Result<(),
config::write(choice, None, temp_dir);
Ok(())
} else {
Err(SetContextError::ContextNotFound{ctx: ctx.to_owned()})
}
}

#[derive(Debug, Clone)]
pub enum SetContextError {
ContextNotFound {
ctx : String
},
}

impl fmt::Display for SetContextError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SetContextError::ContextNotFound{ctx} => write!(f, "no context exists with the name: \"{}\"", ctx),
}
Err(SetContextError::KubeContextNotFound{ctx: ctx.to_owned()})
}
}
19 changes: 19 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use thiserror::Error;

#[derive(Error, Debug)]
pub enum Error {
#[error("failed to set context: {0}")]
SetContext(#[source] SetContextError),
#[error("no item selected when prompted to select {prompt}")]
NoItemSelected {
prompt: &'static str
},
}

#[derive(Error, Debug)]
pub enum SetContextError {
#[error("no context exists with the name {ctx}")]
KubeContextNotFound {
ctx: String
},
}
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ mod commands;
mod config;
mod model;
mod modes;
mod error;

use crate::error::Error;
use clap::Parser;
use std::env;
use std::io;
Expand Down Expand Up @@ -69,7 +71,7 @@ enum Mode {
}

impl Mode {
fn invoke(&self) -> Result <(), String> {
fn invoke(&self) -> Result <(), Error> {
let args = Cli::parse();
match self {
Mode::Namespace => modes::namespace(args),
Expand Down
26 changes: 15 additions & 11 deletions src/modes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{commands::{self}, config, Cli, DEST, KUBECONFIG};
use crate::{commands::{self}, config, error::Error, Cli, DEST, KUBECONFIG};

pub fn default_context(args: Cli) -> Result<(), String> {
pub fn default_context(args: Cli) -> Result<(), Error> {
let config = config::get();

if args.current {
Expand All @@ -14,15 +14,16 @@ pub fn default_context(args: Cli) -> Result<(), String> {
.map(|context| context.name.to_string())
.collect();

commands::selectable_list(options).expect("No item selected")
commands::selectable_list(options)
.ok_or(Error::NoItemSelected{prompt: "context" })?
}
Some(x) => x.trim().to_string(),
};

commands::set_default_context(&ctx);

let set_context_result = commands::set_context(&ctx, &DEST, &config)
.map_err(|err| err.to_string());
.map_err(Error::SetContext);

if set_context_result.is_ok() {
println!("{}", KUBECONFIG.as_str());
Expand All @@ -31,7 +32,7 @@ pub fn default_context(args: Cli) -> Result<(), String> {
set_context_result
}

pub fn context(args: Cli) -> Result<(), String> {
pub fn context(args: Cli) -> Result<(), Error> {
if args.current {
let config = config::get_current_session();
println!("{}", config.current_context);
Expand All @@ -45,13 +46,14 @@ pub fn context(args: Cli) -> Result<(), String> {
.map(|context| context.name.to_string())
.collect();

commands::selectable_list(options).ok_or("No item selected")?
commands::selectable_list(options)
.ok_or(Error::NoItemSelected{prompt: "context"})?
}
Some(x) => x.trim().to_string(),
};

let set_context_result = commands::set_context(&ctx, &DEST, &config)
.map_err(|err| err.to_string());
.map_err(Error::SetContext);

if set_context_result.is_ok() {
println!(
Expand All @@ -65,7 +67,7 @@ pub fn context(args: Cli) -> Result<(), String> {
set_context_result
}

pub fn namespace(args: Cli) -> Result<(), String> {
pub fn namespace(args: Cli) -> Result<(), Error> {
let config = config::get_current_session();
if args.current {
let ctx = config
Expand All @@ -89,7 +91,8 @@ pub fn namespace(args: Cli) -> Result<(), String> {
let ns = match args.value {
None => {
let namespaces : Vec<String> = commands::get_namespaces();
commands::selectable_list(namespaces).ok_or("No item selected")?
commands::selectable_list(namespaces)
.ok_or(Error::NoItemSelected{prompt: "namespace"})?
}
Some(x) => x.trim().to_string(),
};
Expand All @@ -105,7 +108,7 @@ pub fn namespace(args: Cli) -> Result<(), String> {
Ok(())
}

pub fn default_namespace(args: Cli) -> Result<(), String> {
pub fn default_namespace(args: Cli) -> Result<(), Error> {
let config = config::get();
let ctx = commands::get_current_context();

Expand All @@ -132,7 +135,8 @@ pub fn default_namespace(args: Cli) -> Result<(), String> {
let ns = match args.value {
None => {
let namespaces : Vec<String> = commands::get_namespaces();
commands::selectable_list(namespaces).ok_or("No item selected")?
commands::selectable_list(namespaces)
.ok_or(Error::NoItemSelected{prompt: "namespace"})?
}
Some(x) => x.trim().to_string(),
};
Expand Down

0 comments on commit 53cb819

Please sign in to comment.