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

fix zsh/bash embedded plugin #339

Merged
merged 1 commit into from
Jan 16, 2024
Merged
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
110 changes: 74 additions & 36 deletions pkg/cmd/cli/profile/scripts/plugin.sh
Original file line number Diff line number Diff line change
@@ -1,68 +1,106 @@
#!/bin/bash

# Force encoding
export LANG=C.UTF-8
export LC_ALL=C.UTF-8

C8Y_SHELL=bash
if [ -n "$BASH_VERSION" ]; then
C8Y_SHELL=bash
elif [ -n "$ZSH_VERSION" ]; then
C8Y_SHELL=zsh
fi

install_bash_dependencies() {
__setup_bash() {
#
# Install bash dependencies
#
if [ ! -d "$HOME/.bash_completion.d" ]; then
mkdir -p "$HOME/.bash_completion.d"
fi

if [ ! -f "$HOME/.bash_completion.d/complete_alias" ]; then
echo "Installing bash completion for aliases"
curl -sfL https://raw.githubusercontent.com/cykerway/complete-alias/master/complete_alias \
> "$HOME/.bash_completion.d/complete_alias"
if command -V curl >/dev/null 2>&1; then
echo "Installing bash completion for aliases"
curl -sfL https://raw.githubusercontent.com/cykerway/complete-alias/master/complete_alias > "$HOME/.bash_completion.d/complete_alias"
elif command -V curl >/dev/null 2>&1; then
echo "Installing bash completion for aliases"
wget -O - https://raw.githubusercontent.com/cykerway/complete-alias/master/complete_alias > "$HOME/.bash_completion.d/complete_alias"
fi
fi

# Enable completion for aliases
# shellcheck disable=SC1091
[ -f /usr/share/bash-completion/bash_completion ] && source /usr/share/bash-completion/bash_completion
# shellcheck disable=SC1091
[ -f "$HOME/.bash_completion.d/complete_alias" ] && source "$HOME/.bash_completion.d/complete_alias"

if [ -f "$(brew --prefix)/etc/bash_completion" ]; then
. "$(brew --prefix)/etc/bash_completion"
if [ -f /etc/bash_completion ]; then
# shellcheck disable=SC1091
. "/etc/bash_completion"
elif command -v brew >/dev/null 2>&1; then
if [ -f "$(brew --prefix)/etc/bash_completion" ]; then
# shellcheck disable=SC1091
. "$(brew --prefix)/etc/bash_completion"
fi
fi
}

init-c8y() {
if command -v c8y >/dev/null 2>&1; then
if [ "$C8Y_SHELL" = "zsh" ]; then
# shellcheck disable=SC1090
source <(c8y completion bash)
}

# homebrew only: Add completions folder to the fpath so zsh can find it
if command -v brew >/dev/null 2>&1; then
FPATH="$(brew --prefix)/share/zsh/site-functions:$FPATH"
chmod -R go-w "$(brew --prefix)/share"
fi
__setup_zsh() {
# homebrew only: Add completions folder to the fpath so zsh can find it
if command -v brew >/dev/null 2>&1; then
FPATH="$(brew --prefix)/share/zsh/site-functions:$FPATH"
# TODO: Check if this line is really required
# chmod -R go-w "$(brew --prefix)/share"
fi

# init completions
autoload -U compinit; compinit
# init completions
autoload -U compinit; compinit

if [ -n "$ZSH_CUSTOM" ]; then
mkdir -p "$ZSH_CUSTOM/plugins/c8y"
IMPORT_LOCAL=1

if [ ! -f "$ZSH_CUSTOM/plugins/c8y/_c8y" ]; then
c8y completion zsh > "$ZSH_CUSTOM/plugins/c8y/_c8y"
echo "Updated c8y completions"
source "$HOME/.zshrc"
if [ -n "$ZSH_CUSTOM" ]; then
# oh-my-zsh
mkdir -p "$ZSH_CUSTOM/plugins/c8y"
if [ ! -f "$ZSH_CUSTOM/plugins/c8y/_c8y" ]; then
echo "Updating c8y completions: $ZSH_CUSTOM/plugins/c8y/_c8y"
c8y completion zsh > "$ZSH_CUSTOM/plugins/c8y/_c8y"
IMPORT_LOCAL=0
fi
else
# zsh (vanilla)
if [ ! -f "/usr/share/zsh/site-functions/_c8y" ]; then
if [ "$EUID" = "0" ]; then
if [ -d /usr/share/zsh/site-functions ]; then
echo "Updating c8y completions: /usr/share/zsh/site-functions/_c8y"
c8y completion zsh > "/usr/share/zsh/site-functions/_c8y"
IMPORT_LOCAL=0
fi
fi
fi
fi

if [ "$C8Y_SHELL" = "bash" ]; then
install_bash_dependencies
source <(c8y completion bash)
fi
if [ "$IMPORT_LOCAL" = 1 ]; then
# shellcheck disable=SC1090
source <(c8y completion zsh)
fi
}

init-c8y

#
# init
#
C8Y_SHELL=bash
if [ -n "$BASH_VERSION" ]; then
C8Y_SHELL=bash
elif [ -n "$ZSH_VERSION" ]; then
C8Y_SHELL=zsh
fi
if command -v c8y >/dev/null 2>&1; then
case "$C8Y_SHELL" in
bash)
__setup_bash
;;
zsh)
__setup_zsh
;;
esac
fi

########################################################################
# c8y helpers
Expand Down