Skip to content

Commit

Permalink
Merge pull request #2 from embik/bash-magic
Browse files Browse the repository at this point in the history
shell: make `zsh` magic more portable and add `bash` magic
  • Loading branch information
embik authored Nov 21, 2023
2 parents 1095308 + 2df1f9b commit 65b147d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
12 changes: 12 additions & 0 deletions src/cmd/shell/files/bash/kbs.source
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
alias _inline_fzf="fzf --height=50% --reverse -0 --inline-info --border"
alias _kbs_bin="$(type -p kbs)"

function kbs() {
if [ $# -eq 0 ]; then
# if no parameters are passed, we want to run fzf on available kubeconfigs and set the selected one as active kubeconfig
eval "$(_kbs_bin use $(_kbs_bin ls | _inline_fzf))"
else
# if parameters are passed, we just call the kbs binary directly
${_kbs_bin} $@
fi
}
4 changes: 2 additions & 2 deletions src/cmd/shell/files/zsh/kbs.source
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
alias _inline_fzf="fzf --height=50% --reverse -0 --inline-info --border"
alias _kbs_bin="$(whence -cp kbs)"

function kbs() {
local _kbs_bin="$(whereis -q kbs)"
if [ $# -eq 0 ]; then
# if no parameters are passed, we want to run fzf on available kubeconfigs and set the selected one as active kubeconfig
eval "$(eval ${_kbs_bin} use $(echo "$(eval ${_kbs_bin} ls)" | _inline_fzf))"
eval "$(_kbs_bin use $(_kbs_bin ls | _inline_fzf))"
else
# if parameters are passed, we just call the kbs binary directly
${_kbs_bin} $@
Expand Down
15 changes: 8 additions & 7 deletions src/cmd/shell/magic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ pub fn execute(matches: &ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
.get_one::<Shell>("shell")
.ok_or("cannot read shell")?;

match shell {
Shell::Zsh => {
let zsh_magic = include_str!("./files/zsh/kbs.source");
print!("{zsh_magic}");
}
}
let magic = match shell {
Shell::Zsh => include_str!("./files/zsh/kbs.source"),
Shell::Bash => include_str!("./files/bash/kbs.source"),
};
print!("{magic}");

Ok(())
}
Expand All @@ -35,16 +34,18 @@ pub fn execute(matches: &ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
#[non_exhaustive]
enum Shell {
Zsh,
Bash,
}

impl clap::ValueEnum for Shell {
fn value_variants<'a>() -> &'a [Self] {
&[Shell::Zsh]
&[Shell::Zsh, Shell::Bash]
}

fn to_possible_value<'a>(&self) -> Option<PossibleValue> {
Some(match self {
Shell::Zsh => PossibleValue::new("zsh"),
Shell::Bash => PossibleValue::new("bash"),
})
}
}

0 comments on commit 65b147d

Please sign in to comment.