Skip to content

Commit 65b147d

Browse files
authored
Merge pull request #2 from embik/bash-magic
shell: make `zsh` magic more portable and add `bash` magic
2 parents 1095308 + 2df1f9b commit 65b147d

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

src/cmd/shell/files/bash/kbs.source

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
alias _inline_fzf="fzf --height=50% --reverse -0 --inline-info --border"
2+
alias _kbs_bin="$(type -p kbs)"
3+
4+
function kbs() {
5+
if [ $# -eq 0 ]; then
6+
# if no parameters are passed, we want to run fzf on available kubeconfigs and set the selected one as active kubeconfig
7+
eval "$(_kbs_bin use $(_kbs_bin ls | _inline_fzf))"
8+
else
9+
# if parameters are passed, we just call the kbs binary directly
10+
${_kbs_bin} $@
11+
fi
12+
}

src/cmd/shell/files/zsh/kbs.source

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
alias _inline_fzf="fzf --height=50% --reverse -0 --inline-info --border"
2+
alias _kbs_bin="$(whence -cp kbs)"
23

34
function kbs() {
4-
local _kbs_bin="$(whereis -q kbs)"
55
if [ $# -eq 0 ]; then
66
# if no parameters are passed, we want to run fzf on available kubeconfigs and set the selected one as active kubeconfig
7-
eval "$(eval ${_kbs_bin} use $(echo "$(eval ${_kbs_bin} ls)" | _inline_fzf))"
7+
eval "$(_kbs_bin use $(_kbs_bin ls | _inline_fzf))"
88
else
99
# if parameters are passed, we just call the kbs binary directly
1010
${_kbs_bin} $@

src/cmd/shell/magic.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ pub fn execute(matches: &ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
2121
.get_one::<Shell>("shell")
2222
.ok_or("cannot read shell")?;
2323

24-
match shell {
25-
Shell::Zsh => {
26-
let zsh_magic = include_str!("./files/zsh/kbs.source");
27-
print!("{zsh_magic}");
28-
}
29-
}
24+
let magic = match shell {
25+
Shell::Zsh => include_str!("./files/zsh/kbs.source"),
26+
Shell::Bash => include_str!("./files/bash/kbs.source"),
27+
};
28+
print!("{magic}");
3029

3130
Ok(())
3231
}
@@ -35,16 +34,18 @@ pub fn execute(matches: &ArgMatches) -> Result<(), Box<dyn std::error::Error>> {
3534
#[non_exhaustive]
3635
enum Shell {
3736
Zsh,
37+
Bash,
3838
}
3939

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

4545
fn to_possible_value<'a>(&self) -> Option<PossibleValue> {
4646
Some(match self {
4747
Shell::Zsh => PossibleValue::new("zsh"),
48+
Shell::Bash => PossibleValue::new("bash"),
4849
})
4950
}
5051
}

0 commit comments

Comments
 (0)