Skip to content

Instantly share code, notes, and snippets.

@yteraoka
Created February 29, 2024 15:40
Show Gist options
  • Save yteraoka/c74cee3768832e720e21f20cdbda69aa to your computer and use it in GitHub Desktop.
Save yteraoka/c74cee3768832e720e21f20cdbda69aa to your computer and use it in GitHub Desktop.
dotfiles
alias cp="cp -i"
alias rm="rm -i"
alias mv="mv -i"
alias k="kubectl"
alias kd="kubectl describe"
alias g="git"
alias t="terraform"
alias tf="terraform"
alias tfdoc="terraform-docs"
alias awsid="aws sts get-caller-identity"
alias neat="kubectl-neat"
alias eksnodes="kubectl get node -L node.kubernetes.io/instance-type,topology.kubernetes.io/zone"
alias aksnodes="kubectl get node -L beta.kubernetes.io/os,node.kubernetes.io/instance-type,topology.kubernetes.io/region,topology.kubernetes.io/zone"
alias gkenodes="kubectl get node -L cloud.google.com/gke-nodepool,cloud.google.com/gke-os-distribution,cloud.google.com/gke-preemptible,beta.kubernetes.io/instance-type,failure-domain.beta.kubernetes.io/zone"
alias knode="kubectl get node -L node.kubernetes.io/instance-type,topology.kubernetes.io/zone,workload/system,workload/online,workload/ingress,workload/batch"
alias gcurl='curl -H "$(oauth2l header --json $GOOGLE_APPLICATION_CREDENTIALS cloud-platform userinfo.email)" -H "Content-Type: application/json"'
alias diffhl="source-highlight -s diff -f esc"
alias lsusb="system_profiler SPUSBDataType"
alias dils="docker image ls --format '{{.ID}}\t{{.Size}}\t{{.Repository}}:{{.Tag}}'"
alias dirm="docker image rm"
alias less="less -R"
set encoding=utf-8
"set fileencodings=iso-2022-jp,euc-jp,sjis,utf-8
set fileencodings=utf-8
set fileformats=unix,dos,mac
"set number
set autoindent
set smartindent
set ruler
set showmatch
set nobackup
"set noerrorbells
set cursorline
set visualbell
"set list listchars=tab:\▸\-
set wrapscan
set incsearch
set smartcase
set hlsearch
nmap <Esc><Esc> :nohlsearch<CR><Esc>
syntax on
"filetype plugin indent on
set laststatus=2
set statusline=%F%m%r
set statusline+=%=
set statusline+=\ \|\ %{&fileformat},%{&fileencoding}%{(&bomb?\"(bom)\":\"\")}
set statusline+=\ \|\ %Y
set statusline+=\ \|\ %l/%L,%c
if has("autocmd")
filetype plugin on
filetype indent on
"sw=shiftwidth, sts=softtabstop, ts=tabstop, et=expandtab
autocmd FileType html setlocal sw=2 sts=2 ts=2 et
autocmd FileType yaml setlocal sw=2 sts=2 ts=2 et
autocmd FileType sh setlocal sw=2 sts=2 ts=2 et
autocmd FileType json setlocal sw=2 sts=2 ts=2 et
autocmd FileType javascript setlocal sw=2 sts=2 ts=2 et
autocmd FileType ruby setlocal sw=2 sts=2 ts=2 et
autocmd FileType python setlocal sw=4 sts=4 ts=4 et
autocmd FileType terraform setlocal sw=2 sts=2 ts=2 et
endif
export LANG=ja_JP.utf8
export LC_TIME=C
export LC_MESSAGES=C
export EDITOR=vim
export AWS_DEFAULT_REGION=ap-northeast-1
export ANSIBLE_STDOUT_CALLBACK=yaml
export AWS_STS_REGIONAL_ENDPOINTS=regional
export AWS_SDK_LOAD_CONFIG=1
export BROWSER="/mnt/c/Program Files/Google/Chrome/Application/chrome.exe"
export GH_BROWSER='"/mnt/c/Program Files/Google/Chrome/Application/chrome.exe"'
#export OP_USER="1password Username"
if [ -f "${HOME}/.zscaler/ZscalerRootCertificate-2048-SHA256.crt" ] ; then
export ZSCALER_CA_CERT="${HOME}/.zscaler/ZscalerRootCertificate-2048-SHA256.crt"
fi
if [ $(uname -s) = "Linux" ] ; then
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
elif [ -d /opt/homebrew ] ; then
eval "$(/opt/homebrew/bin/brew shellenv)"
PATH="/opt/homebrew/opt/mysql-client/bin:$PATH"
PATH="/opt/homebrew/opt/coreutils/libexec/gnubin:$PATH"
elif [ -d "$HOME/.homebrew" ] ; then
eval "$($HOME/.homebrew/bin/brew shellenv)"
PATH="$HOME/.homebrew/opt/mysql-client/bin:$PATH"
PATH="$HOME/.homebrew/opt/coreutils/libexec/gnubin:$PATH"
fi
PATH=$HOME/google-cloud-sdk/bin:$PATH
PATH=$HOME/bin:$PATH
PATH="$PATH:$HOME/.krew/bin"
if [ -x $HOME/bin/mylesspipe.sh ] ; then
export LESSOPEN="|$HOME/bin/mylesspipe.sh %s" LESS_ADVANCED_PREPROCESSOR=1
fi
# Added by OrbStack: command-line tools and integration
if [ -f "$HOME/.orbstack/shell/init.zsh" ] ; then
source "$HOME/.orbstack/shell/init.zsh" 2> /dev/null || :
fi
#
# vim: sw=2 sts=2 ts=2 et ai
#
bindkey -e
setopt nobeep
setopt no_tify
HISTSIZE=10000 # メモリに保存される履歴の件数
SAVEHIST=10000 # 履歴ファイルに保存される履歴の件数
setopt hist_ignore_dups # 直前と同じコマンドをヒストリに追加しない
setopt hist_ignore_all_dups # 重複するコマンドは古い法を削除する
#setopt share_history # 異なるウィンドウでコマンドヒストリを共有する
setopt hist_no_store # historyコマンドは履歴に登録しない
setopt hist_reduce_blanks # 余分な空白は詰めて記録
setopt hist_verify # `!!`を実行したときにいきなり実行せずコマンドを見せ
test -f ${HOME}/.aliases && source ${HOME}/.aliases
if [ -n "${HOMEBREW_PREFIX}" ]; then
fpath+=($(brew --prefix)/share/zsh/site-functions)
fi
fpath+=($HOME/.zsh/site-functions)
autoload -U compinit && compinit
autoload -Uz kubepath gopath istiopath helmpath ap mfa cdq argoproxy gcenv azenv kc limadocker uenv
autoload -Uz vcs_info add-zsh-hook
autoload bashcompinit && bashcompinit
test -e "$HOME/bin/aws_completer" && complete -C "$HOME/bin/aws_completer" aws
test -e ${HOMEBREW_PREFIX}/bin/aws_completer && complete -C ${HOMEBREW_PREFIX}/bin/aws_completer aws
test -f ${HOME}/google-cloud-sdk/completion.zsh.inc && source ${HOME}/google-cloud-sdk/completion.zsh.inc
type direnv > /dev/null 2>&1 && eval "$(direnv hook zsh)"
type stern > /dev/null 2>&1 && source <(stern --completion zsh)
function prompt_gcenv() {
if [ -n "$CLOUDSDK_CONFIG" ] ; then
local project=""
if [ -f "$CLOUDSDK_CONFIG/configurations/config_default" ]; then
project=$(cat $CLOUDSDK_CONFIG/configurations/config_default | grep ^project | cut -d = -f 2 | sed 's/ //g')
project="($project)"
fi
prompt_gcenv_str="[gcloud:$(basename $CLOUDSDK_CONFIG)$project]"
else
prompt_gcenv_str=""
fi
}
function prompt_ap() {
if [ -n "$AWS_PROFILE" ] ; then
prompt_ap_str="[aws:$AWS_PROFILE]"
else
prompt_ap_str=""
fi
}
function prompt_azenv() {
if [ -n "$AZURE_CONFIG_DIR" ] ; then
prompt_azenv_str="[azure:$(basename $AZURE_CONFIG_DIR)]"
else
prompt_azenv_str=""
fi
}
function prompt_kubeconfig() {
if [ -n "$KUBIE_ACTIVE" ] ; then
prompt_kubeconfig_str="[kubie:$(kubectx -c)($(kubens -c 2> /dev/null))]"
elif [ -n "$KUBECONFIG" ] ; then
prompt_kubeconfig_str="[k8s:$(basename $KUBECONFIG)($(kubens -c 2> /dev/null))]"
else
prompt_kubeconfig_str=""
fi
}
function prompt_env_lf() {
if [ -z "$GCLOUDSDK_CONFIG" -a -z "$AWS_PROFILE" -a -z "$AZURE_CONFIG_DIR" -a -z "$KUBECONFIG" ] ; then
prompt_env_lf_str=""
else
prompt_env_lf_str="
"
fi
}
setopt prompt_subst
zstyle ':vcs_info:git:*' check-for-changes true
zstyle ':vcs_info:git:*' stagedstr "%F{yellow}!"
zstyle ':vcs_info:git:*' unstagedstr "%F{red}+"
zstyle ':vcs_info:*' formats "%F{green}%c%u[%b]%f"
zstyle ':vcs_info:*' actionformats '[%b|%a]'
add-zsh-hook precmd vcs_info
add-zsh-hook precmd prompt_gcenv
add-zsh-hook precmd prompt_ap
add-zsh-hook precmd prompt_azenv
add-zsh-hook precmd prompt_kubeconfig
add-zsh-hook precmd prompt_env_lf
PROMPT="%F{green}\${prompt_kubeconfig_str}%f%F{cyan}\${prompt_ap_str}%f%F{red}\${prompt_gcenv_str}%f%F{orange}\${prompt_azenv_str}%f
[%D %*] %F{cyan}%n%f@%F{yellow}%m%f
%F{magenta}%~%f
%(?.$.[exit %?]
$) "
PROMPT_EOL_MARK="
"
RPROMPT=$RPROMPT'${vcs_info_msg_0_}'
# 補完メッセージを読みやすくする
zstyle ':completion:*' verbose yes
zstyle ':completion:*' format '%B%d%b'
zstyle ':completion:*:warnings' format 'No matches for: %d'
zstyle ':completion:*' group-name ''
# sudo launchctl limit maxfiles 4096 65536
ulimit -n 4096
if [ -x "${HOMEBREW_PREFIX}/bin/mise" ]; then
eval "$(${HOMEBREW_PREFIX}/bin/mise activate zsh)"
fi
# The next line updates PATH for the Google Cloud SDK.
[ -f "$HOME/google-cloud-sdk/path.zsh.inc" ] && . "$HOME/google-cloud-sdk/path.zsh.inc"
# The next line enables shell command completion for gcloud.
[ -f "$HOME/google-cloud-sdk/completion.zsh.inc" ] && . "$HOME/google-cloud-sdk/completion.zsh.inc"
#compdef _ap ap
_ap() {
_values "commands" "show[show current profile]" "list[list profiles]" "unset[unset AWS_PROFILE]"
_values "profiles" $(grep "^\[profile" $HOME/.aws/config | awk '{print $2}' | sed 's/\]//')
}
#compdef gcenv
_values "" "show" "list" "login" "login-app" "unset" "tokeninfo" "impersonate" $(ls -d $HOME/.gcloud/* | xargs -n 1 basename)
#compdef _kc kc
__kc_configs() {
_values "configs" $(ls -d $HOME/.kubeconfig.d/* | xargs -n 1 basename | sort)
_values "command" "list" "show"
}
__kc_default() {
_values "command" "show[show current kubeconfig]" "unset[unset kubeconfig]" "delete[delete kubeconfig file]" {reset,truncate}"[truncate kubeconfig file]"
_values "configs" $(ls -d $HOME/.kubeconfig.d/* | xargs -n 1 basename | sort)
}
_kc() {
_arguments \
'(- *)'{-l,--list}'[list]' \
'(- *)'{-s,--show}'[show]' \
'*:configs:__kc_configs'
}
# ~/.zsh/site-functions/ap
local profile="$1"
local selector=
type peco > /dev/null 2>&1 && selector=peco
type fzf > /dev/null 2>&1 && selector=fzf
if [ -z "$profile" ] ; then
profile=$(grep "^\[profile" $HOME/.aws/config | awk '{print $2}' | sed 's/\]//' | sort | $selector)
fi
case "$profile" in
show|-p|-s)
echo $AWS_PROFILE
if [ -z "$AWS_ACCESS_KEY_ID" ] ; then
echo "AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID"
fi
if [ -z "$AWS_SECRET_ACCESS_KEY" ] ; then
echo "AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY"
fi
if [ -z "$AWS_SESSION_TOKEN" ] ; then
echo "AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN"
fi
return
;;
unset|clear|-u)
unset AWS_PROFILE
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
unset AWS_SESSION_TOKEN
return
;;
list|-l)
grep "^\[profile" $HOME/.aws/config | awk '{print $2}' | sed 's/\]//' | sort
return
;;
esac
grep -q "^\[profile $profile\]" $HOME/.aws/config
if [ $? -eq 0 ] ; then
export AWS_PROFILE="$profile"
echo $AWS_PROFILE
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
unset AWS_SESSION_TOKEN
else
echo "Unknown profile: $profile" 1>&2
fi
# ~/.zsh/site-functions/cdq
cd $(ghq root)/$(ghq list | sort -r | fzf)
# ~/.zsh/site-functions/gcenv
case "$1" in
create)
if [ -z "$2" ] ; then
echo "Usage: gcenv create <profile-name>" 1>&2
return 1
fi
if [ ! -d "$HOME/.gcloud/$2" ] ; then
mkdir -p "$HOME/.gcloud/$2"
export CLOUDSDK_CONFIG="$HOME/.gcloud/$2"
else
echo "already exists" 1>&2
return 0
fi
;;
impersonate)
if [ -z "$2" ] ; then
echo "Usage: gcenv impersonate [email protected]" 1>&2
return 1
fi
if [ -n "$CLOUDSDK_CONFIG" ] ; then
echo "$2" > ${CLOUDSDK_CONFIG}/impersonate
fi
;;
info|show|s|-s)
echo "CLOUDSDK_CONFIG=${CLOUDSDK_CONFIG}"
echo "GOOGLE_APPLICATION_CREDENTIALS=${GOOGLE_APPLICATION_CREDENTIALS}"
echo "CLOUDSDK_AUTH_IMPERSONATE_SERVICE_ACCOUNT=${CLOUDSDK_AUTH_IMPERSONATE_SERVICE_ACCOUNT}"
echo "GOOGLE_IMPERSONATE_SERVICE_ACCOUNT=${GOOGLE_IMPERSONATE_SERVICE_ACCOUNT}"
;;
list|ls|-l)
ls -d $HOME/.gcloud/* | xargs -n 1 basename
;;
unset|clear|-u)
unset CLOUDSDK_CONFIG
unset GOOGLE_APPLICATION_CREDENTIALS
unset CLOUDSDK_AUTH_IMPERSONATE_SERVICE_ACCOUNT
unset GOOGLE_IMPERSONATE_SERVICE_ACCOUNT
;;
login)
if [ -n "$CLOUDSDK_CONFIG" ] ; then
_CLOUDSDK_AUTH_IMPERSONATE_SERVICE_ACCOUNT=${CLOUDSDK_AUTH_IMPERSONATE_SERVICE_ACCOUNT}
_GOOGLE_IMPERSONATE_SERVICE_ACCOUNT=${GOOGLE_IMPERSONATE_SERVICE_ACCOUNT}
unset CLOUDSDK_AUTH_IMPERSONATE_SERVICE_ACCOUNT
unset GOOGLE_IMPERSONATE_SERVICE_ACCOUNT
gcloud auth login --update-adc
CLOUDSDK_AUTH_IMPERSONATE_SERVICE_ACCOUNT=${_CLOUDSDK_AUTH_IMPERSONATE_SERVICE_ACCOUNT}
GOOGLE_IMPERSONATE_SERVICE_ACCOUNT=${_GOOGLE_IMPERSONATE_SERVICE_ACCOUNT}
unset _CLOUDSDK_AUTH_IMPERSONATE_SERVICE_ACCOUNT
unset _GOOGLE_IMPERSONATE_SERVICE_ACCOUNT
fi
;;
login-app|loginapp)
if [ -n "$CLOUDSDK_CONFIG" ] ; then
_CLOUDSDK_AUTH_IMPERSONATE_SERVICE_ACCOUNT=${CLOUDSDK_AUTH_IMPERSONATE_SERVICE_ACCOUNT}
_GOOGLE_IMPERSONATE_SERVICE_ACCOUNT=${GOOGLE_IMPERSONATE_SERVICE_ACCOUNT}
unset CLOUDSDK_AUTH_IMPERSONATE_SERVICE_ACCOUNT
unset GOOGLE_IMPERSONATE_SERVICE_ACCOUNT
gcloud auth application-default login
CLOUDSDK_AUTH_IMPERSONATE_SERVICE_ACCOUNT=${_CLOUDSDK_AUTH_IMPERSONATE_SERVICE_ACCOUNT}
GOOGLE_IMPERSONATE_SERVICE_ACCOUNT=${_GOOGLE_IMPERSONATE_SERVICE_ACCOUNT}
unset _CLOUDSDK_AUTH_IMPERSONATE_SERVICE_ACCOUNT
unset _GOOGLE_IMPERSONATE_SERVICE_ACCOUNT
fi
;;
tokeninfo)
if [ -n "$CLOUDSDK_CONFIG" ] ; then
curl -s https://www.googleapis.com/oauth2/v1/tokeninfo -d "access_token=$(gcloud auth print-access-token)"
fi
;;
tokeninfo-app)
if [ -n "$CLOUDSDK_CONFIG" ] ; then
curl -s https://www.googleapis.com/oauth2/v1/tokeninfo -d "access_token=$(gcloud auth application-default print-access-token)"
fi
;;
help)
echo "CLOUDSDK_CONFIG=${CLOUDSDK_CONFIG}"
echo "GOOGLE_APPLICATION_CREDENTIALS=${GOOGLE_APPLICATION_CREDENTIALS}"
echo "gcloud auth login --no-launch-browser"
echo "gcloud auth application-default login --no-launch-browser"
;;
/*)
if [ -d "$1" ] ; then
export CLOUDSDK_CONFIG="$1"
fi
;;
*)
if [ -z "$1" ] ; then
export CLOUDSDK_CONFIG=$HOME/.gcloud/$(ls -d $HOME/.gcloud/* | xargs -n 1 basename | sort | fzf)
elif [ -d "$HOME/.gcloud/$1" ] ; then
export CLOUDSDK_CONFIG="$HOME/.gcloud/$1"
else
export CLOUDSDK_CONFIG=$HOME/.gcloud/$(ls -d $HOME/.gcloud/* | xargs -n 1 basename | sort | fzf)
fi
;;
esac
if [ -n "${CLOUDSDK_CONFIG}" ] ; then
if [ -f "${CLOUDSDK_CONFIG}/application_default_credentials.json" ] ; then
export GOOGLE_APPLICATION_CREDENTIALS="${CLOUDSDK_CONFIG}/application_default_credentials.json"
fi
if [ -f "${CLOUDSDK_CONFIG}/impersonate" ]; then
export CLOUDSDK_AUTH_IMPERSONATE_SERVICE_ACCOUNT=$(cat "${CLOUDSDK_CONFIG}/impersonate")
export GOOGLE_IMPERSONATE_SERVICE_ACCOUNT=$(cat "${CLOUDSDK_CONFIG}/impersonate")
else
unset CLOUDSDK_AUTH_IMPERSONATE_SERVICE_ACCOUNT
unset GOOGLE_IMPERSONATE_SERVICE_ACCOUNT
fi
fi
# ~/.zsh/site-functions/kc
local base_dir="$HOME/.kubeconfig.d"
[ -d "${base_dir}" ] || mkdir -m 0700 "${base_dir}"
case "$1" in
create)
if [ -z "$2" ] ; then
echo "Usage: kc create <config-name>" 1>&2
return 1
fi
if [ ! -f "${base_dir}/$2" ] ; then
touch "${base_dir}/$2"
chmod 0600 "${base_dir}/$2"
export KUBECONFIG="${base_dir}/$2"
else
echo "already exists" 1>&2
return 0
fi
;;
delete|rm)
if [ -z "$2" ] ; then
echo "Usage: kc create <config-name>" 1>&2
return 1
fi
if [ -f "${base_dir}/$2" ] ; then
echo -n "Delete ${base_dir}/${2}? [Y/n]: "
read ans
case "$ans" in
n*|N*)
return 0
;;
*)
rm "${base_dir}/$2"
;;
esac
fi
;;
reset|truncate|trunc)
local target=""
if [ -z "$2" ] ; then
if [ -z "$KUBECONFIG" ] ; then
echo "reset file not specified" 1>&2
return 1
else
target="$KUBECONFIG"
fi
else
target="${base_dir}/$2"
fi
if [ ! -f "${target}" ] ; then
echo "${base_dir}/$2 does not exist" 1>&2
return 1
fi
echo -n "Reset ${target}? [Y/n]: "
read ans
case "$ans" in
n*|N*)
return 0
;;
*)
cp /dev/null "${target}"
;;
esac
;;
show)
echo "KUBECONFIG=${KUBECONFIG}"
;;
list|ls)
ls -d ${base_dir}/* | xargs -n 1 basename
;;
unset|clear)
unset KUBECONFIG
;;
/*)
if [ -f "$1" ] ; then
export KUBECONFIG="$1"
fi
;;
*)
if [ -z "$1" ] ; then
export KUBECONFIG=${base_dir}/$(ls -d ${base_dir}/* | xargs -n 1 basename | sort | fzf)
elif [ -f "${base_dir}/$1" ] ; then
export KUBECONFIG="${base_dir}/$1"
else
export KUBECONFIG=${base_dir}/$(ls -d ${base_dir}/* | xargs -n 1 basename | sort | fzf)
fi
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment