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

Merge branch 'feat/fix_command_init' into 'develop'

Fixed kcfg init command

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