Skip to content

Commit

Permalink
feat(shell): migrate from bash to fish shell
Browse files Browse the repository at this point in the history
  • Loading branch information
tmeijn committed Dec 20, 2024
1 parent 99f5f37 commit 6f63073
Show file tree
Hide file tree
Showing 23 changed files with 350 additions and 39 deletions.
6 changes: 6 additions & 0 deletions chezmoi/.chezmoiignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ ansible

dconf.ini
installed-extensions.txt

# Fish
.config/fish/plugins
.config/fish/completion
.config/fish/fishprof.txt
.config/fish/fish_variables
21 changes: 1 addition & 20 deletions chezmoi/dot_bash_functions
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,6 @@ function g_commit_and_push() {
git push
}

function g_setup_mr_branch() {
local branch_name="$1"

# Determine default branch on remote
local default_branch="$(git remote show origin | sed -n '/HEAD branch/s/.*: //p')"

# Make sure the default branch is up to date locally
echo "default branch: '${default_branch}'. Updating default branch..."
git switch "${default_branch}"
git pull

echo "creating branch '${branch_name}'..."
git switch -c "${branch_name}" "${default_branch}"

git push -u origin "${branch_name}"

echo "Done!"
}

function tfinit() {
export TF_HTTP_USERNAME="tmeijn"

Expand Down Expand Up @@ -53,7 +34,7 @@ function tfinit() {

}

# Function to check if a directory has pending changes
# Function to check if a git repository has pending changes
check_git_status() {
find . -type d -name ".git" ! -path "*/.terraform/modules*" -exec dirname {} \; | while read -r dir; do
if [ -d "$dir/.git" ]; then
Expand Down
7 changes: 0 additions & 7 deletions chezmoi/dot_bash_profile
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ if [ -d "$HOME/.local/bin" ] ; then
PATH="$HOME/.local/bin:$PATH"
fi

eval "$(mise activate bash --shims)"

export AQUA_ROOT_DIR="${XDG_DATA_HOME:-$HOME/.local/share/aquaproj-aqua}"
export PATH="${AQUA_ROOT_DIR}/bin:$PATH"
export AQUA_GLOBAL_CONFIG=${AQUA_GLOBAL_CONFIG:-${XDG_CONFIG_HOME:-$HOME/.config}/aquaproj-aqua/aqua.yaml}
export AQUA_GENERATE_WITH_DETAIL=true

if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
3 changes: 0 additions & 3 deletions chezmoi/dot_bashrc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ fi
HISTCONTROL=ignoreboth

# append to the history file, don't overwrite it
shopt -s histappend
export PROMPT_COMMAND="history -a"

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
Expand Down Expand Up @@ -148,8 +147,6 @@ if command -v rbw &> /dev/null; then
export LAB_CORE_TOKEN="$gitlab_token" && echo "✍🏾 LAB_CORE_TOKEN"
github_token="$(rbw get GITHUB_TOKEN)"
export GITHUB_TOKEN="$github_token" && echo "✍🏾 GITHUB_TOKEN"
jira_token="$(rbw get JIRA_API_TOKEN)"
export JIRA_API_TOKEN="$jira_token" && echo "✍🏾 JIRA_API_TOKEN"
else
echo "🔒 Bitwarden vault locked, not loading tokens. 🔒"
echo "💡 To load tokens run the \`reco\` alias. 💡"
Expand Down
3 changes: 3 additions & 0 deletions chezmoi/dot_config/aquaproj-aqua/aqua.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,6 @@ packages:
- name: atc0005/[email protected]
description: Small CLI tool used to submit messages to Microsoft Teams
link: https://github.com/atc0005/send2teams
- name: sharkdp/[email protected]
description: A simple, fast and user-friendly alternative to 'find'
link: https://github.com/sharkdp/fd
Empty file.
42 changes: 42 additions & 0 deletions chezmoi/dot_config/fish/conf.d/10_abbreviations.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env fish

abbr --add fish-reload-config 'source ~/.config/fish/**/*.fish'
abbr --add g_commit_and_push --set-cursor 'git commit -am "%" && git push'
abbr --add starwars 'telnet towel.blinkenlights.nl'
abbr --add agi 'aqua g -g -i'


abbr --position anywhere --add p0 "&> /dev/null" # Pipe everything to /dev/null

# Function to set abbreviation if command exists
function set_abbr_if_cmd_exists
set -l cmd $argv[1]
set -l abbr_name $argv[2]
set -l abbr_value $argv[3]
if command -v $cmd >/dev/null
abbr -a $abbr_name $abbr_value
end
end

# Function to set environment variable if command exists
function set_env_if_cmd_exists
set -l cmd $argv[1]
set -l var_name $argv[2]
set -l var_value $argv[3]
if command -v $cmd >/dev/null
set -gx $var_name $var_value
end
end

# Aqua installed tools
set_abbr_if_cmd_exists eza ls eza
set_abbr_if_cmd_exists eza ll "eza -al"
set_abbr_if_cmd_exists bat cat bat
set_abbr_if_cmd_exists gping ping gping
set_abbr_if_cmd_exists hwatch watch hwatch
set_abbr_if_cmd_exists lazydocker lzd lazydocker
set_abbr_if_cmd_exists lazygit lzg lazygit

# Mise installed tools
set_env_if_cmd_exists moar PAGER "moar -no-clear-on-exit"
set_abbr_if_cmd_exists pgcli psql pgcli
19 changes: 19 additions & 0 deletions chezmoi/dot_config/fish/conf.d/20_aliases.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Repeats the last command with `sudo` prefix
alias pls='sudo $history[1]'

# Needed for granted to work, see: https://docs.commonfate.io/granted/internals/shell-alias#grantedinternalsshell-alias
alias assume="source "$(dirname $(aqua which assume))"/assume.fish"

# Terraform aliases
alias tfapply='terraform apply plan.tfplan'
alias tfcopyplan='terraform show -no-color plan.tfplan | pbcopy'
alias tfplan='terraform plan -out=plan.tfplan'

# Helpers for working with Clipboard
alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'
alias cb='flatpak run app.getclipboard.Clipboard'

# Provision Hetzner GitLab Runners using scheduled pipelines trigger
alias gl_runner_up='glab -R el-capitano/operations/hetzner-cloud-runners schedule run 2691862'
alias gl_runner_down='glab -R el-capitano/operations/hetzner-cloud-runners schedule run 383558'
16 changes: 16 additions & 0 deletions chezmoi/dot_config/fish/conf.d/30_tool_managers.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Set Aqua variables and paths
if set -q XDG_DATA_HOME
set -gx AQUA_ROOT_DIR "$XDG_DATA_HOME/aquaproj-aqua"
else
set -gx AQUA_ROOT_DIR "$HOME/.local/share/aquaproj-aqua"
end

set -gx PATH "$AQUA_ROOT_DIR/bin" $PATH

if set -q XDG_CONFIG_HOME
set -gx AQUA_GLOBAL_CONFIG "$XDG_CONFIG_HOME/aquaproj-aqua/aqua.yaml"
else
set -gx AQUA_GLOBAL_CONFIG "$HOME/.config/aquaproj-aqua/aqua.yaml"
end

set -gx AQUA_GENERATE_WITH_DETAIL true
37 changes: 37 additions & 0 deletions chezmoi/dot_config/fish/conf.d/99_bitwarden_tokens.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
##############################################
# BITWARDEN: auto load tokens if DB unlocked #
##############################################

function __load_tokens_if_rbw_unlocks --on-event rbw-unlocked
if command -v rbw >/dev/null 2>&1
if rbw unlocked >/dev/null 2>&1
echo "🔓🗝️ Session unlocked, loading tokens from Bitwarden... 🔓🗝️"

load_token OPENAI_API_KEY
load_token GITLAB_TOKEN TF_HTTP_PASSWORD TF_VAR_gitlab_token GL_TOKEN LAB_CORE_TOKEN
load_token GITHUB_TOKEN
else
echo "🔒 Bitwarden vault locked, not loading tokens. 🔒"
echo "💡 To load tokens run the `reco` alias. 💡"
end
end
end

function load_token
set -l token_name $argv[1]
set -l token_value (rbw get $token_name)
set -gx $token_name $token_value
echo "🔓 $token_name"
for alias in $argv[2..-1]
set -gx $alias $token_value
echo "🔓 $alias (alias of $token_name)"
end
end

function reco
rbw unlock
emit rbw-unlocked
end

# Call one time to see if already unlocked.
__load_tokens_if_rbw_unlocks
21 changes: 21 additions & 0 deletions chezmoi/dot_config/fish/conf.d/__init__.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Set fisher plugin path and load these plugins.
# Source: https://github.com/jorgebucaran/fisher/issues/640#issuecomment-1878499811
set fisher_path $__fish_config_dir/plugins

set fish_complete_path $fish_complete_path[1] $fisher_path/completions $fish_complete_path[2..]
set fish_function_path $fish_function_path[1] $fisher_path/functions $fish_function_path[2..]

for file in $fisher_path/conf.d/*.fish
source $file
end

#
# Helpers
#

# Source: https://www.reddit.com/r/fishshell/comments/1fmbypo/is_there_a_nicer_way_to_create_auto_complete_for/lo9vqzz/
function optspec2comp
string replace -r / "\n-" $argv |
string replace -r '^' - |
string replace -r '=$' ""
end
64 changes: 64 additions & 0 deletions chezmoi/dot_config/fish/config.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Some nice resources
# - https://www.reddit.com/r/fishshell/comments/1he9bd8/what_are_you_abbreviations/
# - https://www.reddit.com/r/fishshell/comments/1gp750x/can_i_see_your_fish_configs/

set -x TF_CLI_ARGS_plan "-lock=false"
set -x TF_HTTP_USERNAME tmeijn

fish_add_path $HOME/.krew/bin $HOME/.cargo/bin $HOME/.local/bin $HOME/bin

# Everything below this should only be loaded on interactive shells.
if ! status is-interactive
mise activate fish --shims | source
return 0
end

if command -v mise &>/dev/null
mise activate fish | source
mise hook-env -s fish | source
# Mise itself doesn't export this as a variable as Aqua does do, so we do it ourself.
set -gx MISE_GLOBAL_CONFIG (mise config ls --no-header | head -n1 | awk '{print $1}')
set -gx MISE_GLOBAL_CONFIG (string replace -a '~' $HOME $MISE_GLOBAL_CONFIG)
end

if command -v git-town &> /dev/null
alias gt='git-town'
end

if command -v kubectl &>/dev/null
function kubectl --wraps kubectl
command kubecolor $argv
end

# adds alias for "k" to "kubecolor" with completions
function k --wraps kubectl
command kubecolor $argv
end

# Set `dyff` as the kubectl differ (k diff ...)
set -x KUBECTL_EXTERNAL_DIFF "dyff between --omit-header --set-exit-code"
end

if command -v aws_completer &>/dev/null
# Enable AWS CLI autocompletion: https://github.com/aws/aws-cli/issues/1079#issuecomment-541997810
complete --command aws --no-files --arguments '(begin; set --local --export COMP_SHELL fish; set --local --export COMP_LINE (commandline); aws_completer | sed \'s/ $//\'; end)'
end

# Only update completions once a day since this is a slow operation.
if test ! -e $HOME/.update_completions_lock || test (math (date +%s) - (stat -c %Y $HOME/.update_completions_lock)) -gt 86400
update_completions
echo "Updating completions..."
touch $HOME/.update_completions_lock
end

# Can be used as fallback when rbw is broken.
if test -f ~/.env
set -gx (cat ~/.env | string split -f1 '=')
end

# ENHANCE!
command -v fixit >/dev/null; and fixit init fish | source
command -v zoxide >/dev/null; and zoxide init --cmd cd fish | source
command -v atuin >/dev/null; and atuin init fish | source
#command -v savvy >/dev/null; and savvy init fish | source # Disabled: not currently used, so saving in Fish startup costs.
command -v starship >/dev/null; and starship init fish | source
7 changes: 7 additions & 0 deletions chezmoi/dot_config/fish/fish_plugins
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
jorgebucaran/fisher
nickeb96/puffer-fish
jorgebucaran/replay.fish
jorgebucaran/autopair.fish
gazorby/fish-abbreviation-tips
patrickf1/fzf.fish
decors/fish-colored-man
Empty file.
10 changes: 10 additions & 0 deletions chezmoi/dot_config/fish/functions/benchmark.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function benchmark \
--description 'benchmark a shell' \
--argument-names shellname

test -n "$shellname" || set shellname fish
echo "running $shellname 10 times..."
for i in (seq 10)
/usr/bin/time -f "\t%E real,\t%U user,\t%S sys" $shellname -i -c exit
end
end
13 changes: 13 additions & 0 deletions chezmoi/dot_config/fish/functions/check_git_status.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Function to check if a git repository has pending changes
function check_git_status
find . -type d -name ".git" ! -path "*/.terraform/modules*" -exec dirname {} \; | while read -l dir
if test -d "$dir/.git"
pushd "$dir" > /dev/null; or return
if git status --porcelain | grep -q .
echo "=== Pending changes in $dir ==="
git status -s
end
popd > /dev/null; or return
end
end
end
14 changes: 14 additions & 0 deletions chezmoi/dot_config/fish/functions/profile_shell_startup.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function profile_shell_startup --description 'Profile fish shell startup time'
set shellname fish
set fprof $__fish_config_dir/fishprof.txt
fish --profile-startup=$fprof -i -c exit
awk 'NR==1 || $3==">"{print}' $fprof | string replace $HOME '~'

read -l -P "Do you want to see the sorted profile results? (y/N) " confirm
switch $confirm
case Y y
sort -nk2 $fprof
case '' N n
echo "Sorting skipped."
end
end
33 changes: 33 additions & 0 deletions chezmoi/dot_config/fish/functions/tfinit.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
set -g tfinit_optspec 'b/backend=' no-plan

function tfinit --description 'Initialize Terraform with an optional partial backend file.'
argparse $tfinit_optspec -- $argv
or return

set -l init_args

if test -f "backends/local.tfbackend"
echo "Local backend file detected, setting in command"
set -gx TF_CLI_ARGS_init "-backend-config=backends/local.tfbackend"
end

if set -q _flag_backend
if not test -f "backends/$_flag_backend.tfbackend"
echo "Backend file 'backends/$_flag_backend.tfbackend' does not exist!"
return 1
end
set init_args $init_args "-backend-config=backends/$_flag_backend.tfbackend"
set -gx TF_VAR_env "$_flag_backend"
end

set init_args $init_args -reconfigure $argv

terraform init $init_args

if not set -q _flag_no_plan
echo "==== Executing Terraform plan command... ===="
terraform plan -out=plan.tfplan
end
end

complete -f --command tfinit --arguments='(optspec2comp $tfinit_optspec)'
Loading

0 comments on commit 6f63073

Please sign in to comment.