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

Fixed kcfg init command

parent 42255388
No related branches found
No related tags found
1 merge request!8Fixed kcfg init command
# CHANGELOG
## Unreleased
* Fixed `init` command
## 0.1.1
* Bumped `clap` version
......
......@@ -18,7 +18,7 @@ pub struct KcfgApp {
#[derive(Debug, ArgEnum, Parser)]
pub enum Command {
/// Initializes the environment relative to your current path
/// Prints the kcfg shell initialization code
Init(command_init::InitOptions),
/// Find and use the correct KUBECONFIG
Use(command_use::UseOptions),
......
use crate::common::check_directory_path;
use crate::common::check_file_path;
use crate::error::KcfgError;
use clap::{ArgEnum, Parser, ValueHint};
use indoc::formatdoc;
use std::str::FromStr;
/// Options for the `init` command
#[derive(Parser, Debug)]
pub struct InitOptions {
#[clap(arg_enum, default_value = "common")]
#[clap(arg_enum)]
/// target shell type
pub shell_type: InitShellType,
pub shell_type: Option<InitShellType>,
#[clap(short = 'p', long = "path", value_hint = ValueHint::DirPath)]
/// Define a custom path. Otherwise the current path will be used
/// Define a custom path to kcfg program, otherwise the current filesystem path to kcfg will be used
pub custom_path: Option<String>,
}
......@@ -25,20 +24,6 @@ pub struct InitOptions {
pub enum InitShellType {
Zsh,
Bash,
Common,
}
impl FromStr for InitShellType {
type Err = KcfgError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_ascii_lowercase().as_str() {
"zsh" => Ok(InitShellType::Zsh),
"bash" => Ok(InitShellType::Bash),
"common" => Ok(InitShellType::Common),
_ => Err(KcfgError::NotSupportedShellType(s.to_string())),
}
}
}
/// Match with the shell type the User has
......@@ -63,15 +48,18 @@ pub fn init(params: InitOptions) -> Result<String, KcfgError> {
.to_string()
}
Some(custom_path) => {
check_directory_path(&custom_path)?;
check_file_path(&custom_path)?;
custom_path
}
};
Ok(match params.shell_type {
InitShellType::Common => init_common_full(&path),
InitShellType::Zsh => init_zsh(&path),
InitShellType::Bash => init_bash(&path),
})
let res = match params.shell_type {
Some(shell_type) => match shell_type {
InitShellType::Bash => init_bash(&path),
InitShellType::Zsh => init_zsh(&path),
},
None => init_common_full(&path),
};
Ok(res)
}
/// # Arguments
......@@ -83,7 +71,7 @@ pub fn init(params: InitOptions) -> Result<String, KcfgError> {
/// Return a string that contains the shell function to use in the `Common` case
///
fn init_common_full(current_path: &str) -> String {
return formatdoc! {"
formatdoc! {"
function kcfg() {{
result=$({cmd} $@)
if [[ $result = 'export '* ]]
......@@ -95,7 +83,7 @@ fn init_common_full(current_path: &str) -> String {
}}
",
cmd = current_path
};
}
}
/// # Arguments
......@@ -107,7 +95,7 @@ fn init_common_full(current_path: &str) -> String {
/// Return a string that contains the shell command to use in the `zsh` case
///
fn init_zsh(current_path: &str) -> String {
formatdoc!("source <({} init zsh --print-full-init)", current_path)
format!("source <({} init)", current_path)
}
/// # Arguments
......@@ -125,9 +113,9 @@ fn init_bash(current_path: &str) -> String {
local minor=\"${{BASH_VERSINFO[1]}}\"
if ((major > 4)) || {{ ((major == 4)) && ((minor >= 1)); }}; then
source <(\"{cmd}\" init bash --print-full-init)
source <(\"{cmd}\" init)
else
source /dev/stdin <<<\"$(\"{cmd}\" init bash --print-full-init)\"
source /dev/stdin <<<\"$(\"{cmd}\" init)\"
fi
}}
__main
......@@ -147,7 +135,7 @@ mod tests {
#[test]
fn can_fail_with_wrong_path() {
let params = InitOptions {
shell_type: InitShellType::Bash,
shell_type: Some(InitShellType::Bash),
custom_path: Some("toto".to_string()),
};
match init(params) {
......@@ -161,15 +149,15 @@ mod tests {
}
#[test]
fn can_fail_with_wrong_directory() {
fn can_fail_with_wrong_filr() {
let params = InitOptions {
shell_type: InitShellType::Bash,
custom_path: Some("./Cargo.toml".to_string()),
shell_type: Some(InitShellType::Bash),
custom_path: Some("src".to_string()),
};
match init(params) {
Ok(_) => panic!("Test should have failed"),
Err(e) => {
if !matches!(e, KcfgError::WrongDirectory(_)) {
if !matches!(e, KcfgError::WrongFile(_)) {
panic!("Test failed with wrong error");
}
}
......@@ -179,8 +167,8 @@ mod tests {
#[test]
fn can_succeed_with_bash() {
let params = InitOptions {
shell_type: InitShellType::Bash,
custom_path: Some("/tmp".to_string()),
shell_type: Some(InitShellType::Bash),
custom_path: Some("src/main.rs".to_string()),
};
let res = init(params).unwrap();
assert_eq!(
......@@ -190,9 +178,9 @@ mod tests {
local minor=\"${{BASH_VERSINFO[1]}}\"
if ((major > 4)) || {{ ((major == 4)) && ((minor >= 1)); }}; then
source <(\"/tmp\" init bash --print-full-init)
source <(\"src/main.rs\" init)
else
source /dev/stdin <<<\"$(\"/tmp\" init bash --print-full-init)\"
source /dev/stdin <<<\"$(\"src/main.rs\" init)\"
fi
}}
__main
......@@ -205,24 +193,24 @@ mod tests {
#[test]
fn can_succeed_with_zsh() {
let params = InitOptions {
shell_type: InitShellType::Zsh,
custom_path: Some("/tmp".to_string()),
shell_type: Some(InitShellType::Zsh),
custom_path: Some("src/main.rs".to_string()),
};
let res = init(params).unwrap();
assert_eq!("source <(/tmp init zsh --print-full-init)".to_string(), res,)
assert_eq!("source <(src/main.rs init)".to_string(), res,)
}
#[test]
fn can_succeed_with_common() {
let params = InitOptions {
shell_type: InitShellType::Common,
custom_path: Some("/tmp".to_string()),
shell_type: None,
custom_path: Some("src/main.rs".to_string()),
};
let res = init(params).unwrap();
assert_eq!(
formatdoc! {"
function kcfg() {{
result=$(/tmp $@)
result=$(src/main.rs $@)
if [[ $result = 'export '* ]]
then
eval $result
......@@ -259,10 +247,7 @@ mod tests {
#[test]
fn test_init_zsh() {
assert_eq!(
"source <(test init zsh --print-full-init)".to_string(),
init_zsh("test")
);
assert_eq!("source <(test init)".to_string(), init_zsh("test"));
}
#[test]
......@@ -274,9 +259,9 @@ mod tests {
local minor=\"${{BASH_VERSINFO[1]}}\"
if ((major > 4)) || {{ ((major == 4)) && ((minor >= 1)); }}; then
source <(\"test\" init bash --print-full-init)
source <(\"test\" init)
else
source /dev/stdin <<<\"$(\"test\" init bash --print-full-init)\"
source /dev/stdin <<<\"$(\"test\" init)\"
fi
}}
__main
......
......@@ -5,8 +5,6 @@ use thiserror::Error;
#[derive(Debug, Error)]
pub enum KcfgError {
#[error("{0} is not a supported shell type")]
NotSupportedShellType(String),
#[error("{0} is not a valid path")]
InvalidPath(PathBuf),
#[error("{0} should be a directory")]
......
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