Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shell: make zsh magic more portable and add bash magic #2

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"),
})
}
}