Skip to content
Snippets Groups Projects
Commit 676674f9 authored by Félix Lescaudey de Maneville's avatar Félix Lescaudey de Maneville
Browse files

Refactoring

parent 92d06882
No related branches found
No related tags found
No related merge requests found
Pipeline #385030323 passed
......@@ -49,9 +49,10 @@ impl FromStr for InitShellType {
///
/// # Returns
///
/// Return an error if the current path is unavailable (FileSystem error) or invalid.
/// A `String` containing the produced code is returned on success.
/// An error is returned if the current path is unavailable (FileSystem error) or invalid.
///
pub fn router(params: InitOptions) -> Result<(), KcfgError> {
pub fn router(params: InitOptions) -> Result<String, KcfgError> {
let path = match params.custom_path {
None => {
let current_path = std::env::current_exe()?;
......@@ -62,13 +63,11 @@ pub fn router(params: InitOptions) -> Result<(), KcfgError> {
}
Some(p) => p,
};
let res = match params.shell_type {
Ok(match params.shell_type {
InitShellType::Common => init_common_full(&path),
InitShellType::Zsh => init_zsh(&path),
InitShellType::Bash => init_bash(&path),
};
print!("{}", res);
Ok(())
})
}
/// # Arguments
......
......@@ -34,11 +34,10 @@ pub struct UseOptions {
input: Vec<String>,
}
pub fn router(params: UseOptions) -> Result<(), KcfgError> {
pub fn router(params: UseOptions) -> Result<String, KcfgError> {
// Check if params are empty
if params.input.is_empty() {
println!("export {}=", KUBECONFIG);
return Ok(());
return Ok(format!("export {}=", KUBECONFIG));
}
// Check the Path
......@@ -69,13 +68,14 @@ pub fn router(params: UseOptions) -> Result<(), KcfgError> {
let name = skip_none!(captured.get(1)).as_str().to_string();
let ext = skip_none!(captured.get(2)).as_str().to_string();
if starting_string == name {
println!("export {}={}/{}{}", KUBECONFIG, path_str, name, ext);
return Ok(());
return Ok(format!(
"export {}={}/{}{}",
KUBECONFIG, path_str, name, ext
));
}
if name.starts_with(&format!("{}.", starting_string)) {
println!("{}", &name[starting_string.len() + 1..]);
return Ok(());
return Ok(name[starting_string.len() + 1..].to_string());
}
}
Ok(())
Err(KcfgError::ConfigNotFound(params.input))
}
......@@ -23,4 +23,6 @@ pub enum KcfgError {
#[source]
std::env::VarError,
),
#[error("Could not find a config using {0:?} as input")]
ConfigNotFound(Vec<String>),
}
......@@ -3,7 +3,6 @@
//! kcfg
use crate::error::KcfgError;
use clap::{AppSettings, Clap};
mod command_init;
......@@ -36,11 +35,15 @@ enum Command {
Use(command_use::UseOptions),
}
fn main() -> Result<(), KcfgError> {
fn main() {
let opts: Opts = Opts::parse();
match opts.command {
let res = match opts.command {
Command::Init(params) => command_init::router(params),
Command::Use(params) => command_use::router(params),
};
match res {
Ok(code) => println!("{}", code),
Err(e) => eprintln!("Error: {}", e),
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment