From 39f4f893c6ad0c643d143c42859c93367f69731c Mon Sep 17 00:00:00 2001 From: T31M Date: Sat, 22 Feb 2020 21:32:57 +0100 Subject: [PATCH] feat: lots of changes, wrote own fish theme --- dot.vimrc | 15 +++ fish/config.fish | 6 ++ fish/fishfile | 3 + fish/theme-t31m/cdk.fish | 3 + fish/theme-t31m/envAWS.fish | 67 ++++++++++++ fish/theme-t31m/fish_prompt.fish | 63 +++++++++++ fish/theme-t31m/fish_right_prompt.fish | 143 +++++++++++++++++++++++++ fish/theme-t31m/fish_title.fish | 3 + fish/theme-t31m/t31m_prompt.fish | 20 ++++ fish_setup.sh | 31 ++++++ main.sh | 31 +++++- 11 files changed, 383 insertions(+), 2 deletions(-) create mode 100644 dot.vimrc create mode 100644 fish/config.fish create mode 100644 fish/fishfile create mode 100644 fish/theme-t31m/cdk.fish create mode 100644 fish/theme-t31m/envAWS.fish create mode 100644 fish/theme-t31m/fish_prompt.fish create mode 100644 fish/theme-t31m/fish_right_prompt.fish create mode 100644 fish/theme-t31m/fish_title.fish create mode 100644 fish/theme-t31m/t31m_prompt.fish create mode 100644 fish_setup.sh diff --git a/dot.vimrc b/dot.vimrc new file mode 100644 index 0000000..2bdecde --- /dev/null +++ b/dot.vimrc @@ -0,0 +1,15 @@ +:set mouse=r +:color desert +:syntax on +:set nu +:set list +:set listchars=tab:>- + +:filetype plugin indent on +" show existing tab with 4 spaces width +:set tabstop=4 +" when indenting with '>', use 4 spaces width +:set shiftwidth=4 +" On pressing tab, insert 4 spaces +:set expandtab + diff --git a/fish/config.fish b/fish/config.fish new file mode 100644 index 0000000..8c386ec --- /dev/null +++ b/fish/config.fish @@ -0,0 +1,6 @@ +set -gx PYENV_ROOT $HOME/.pyenv +set -gx PATH ~/bin ~/.local/bin $PYENV_ROOT/bin $PATH + +#pyenv config +status --is-interactive; and pyenv init - | source +status --is-interactive; and pyenv virtualenv-init - | source diff --git a/fish/fishfile b/fish/fishfile new file mode 100644 index 0000000..29bc98e --- /dev/null +++ b/fish/fishfile @@ -0,0 +1,3 @@ +FabioAntunes/fish-nvm +edc/bass +jethrokuan/fzf diff --git a/fish/theme-t31m/cdk.fish b/fish/theme-t31m/cdk.fish new file mode 100644 index 0000000..7bb7dcb --- /dev/null +++ b/fish/theme-t31m/cdk.fish @@ -0,0 +1,3 @@ +function cdk -d "execute aws cdk" -w cdk + __nvm_run "cdk" $argv +end diff --git a/fish/theme-t31m/envAWS.fish b/fish/theme-t31m/envAWS.fish new file mode 100644 index 0000000..e3f3d1c --- /dev/null +++ b/fish/theme-t31m/envAWS.fish @@ -0,0 +1,67 @@ +# TWAICE AWS Credential Management for FiSH + +# TODO change your user +set AWS_IAM_USER "" + +# TODO setup your aws-mfa credential keys in the credentials file + +# AWS Account details +set DEV_ACCOUNT_ID "" +set STG_ACCOUNT_ID "" +set PROD_ACCOUNT_ID "" +set OPS_ACCOUNT_ID "" + +export AWS_REGION=eu-west-1 +export AWS_DEFAULT_REGION=eu-west-1 + + +function envAWS --description 'switch to different aws account environments (-) with MFA' + if test (count $argv) -lt 1 + echo "Provide an AWS Environment: dev, staging, prod, ops" + return + end + + switch $argv[1] + case dev + echo "switching to /refreshing dev" + aws-mfa --assume-role arn:aws:iam::$DEV_ACCOUNT_ID:role/Administrators --duration 43200 --role-session-name $AWS_IAM_USER + getAWSenv + case stg + echo "switching to /refreshing staging" + aws-mfa --assume-role arn:aws:iam::$STG_ACCOUNT_ID:role/Administrators --duration 43200 --role-session-name $AWS_IAM_USER + getAWSenv + case prod + echo "switching to /refreshing prod" + aws-mfa --assume-role arn:aws:iam::$PROD_ACCOUNT_ID:role/Administrators --duration 3600 --role-session-name $AWS_IAM_USER + getAWSenv + case ops + echo "switching to /refreshing ops" + aws-mfa --assume-role arn:aws:iam::$OPS_ACCOUNT_ID:role/Administrators --duration 3600 --role-session-name $AWS_IAM_USER + getAWSenv + case '*' + echo "Wrong / Invalid Environment provided" + return 1 + end +end + +function getAWSenv --description 'verify and set environment' + set aws_account_id (aws sts get-caller-identity | jq -r '.Account') + + if not test -n "$aws_account_id" + echo "aws sts could not get identify. Are you logged in ?" + set -ge env # clear env + return 1 + end + if [ $aws_account_id = $DEV_ACCOUNT_ID ] + set -g env dev + else if [ $aws_account_id = $STG_ACCOUNT_ID] + set -g env stg + else if [ $aws_account_id = $PROD_ACCOUNT_ID] + set -g env prod + else if [ $aws_account_id = $OPS_ACCOUNT_ID] + set -g env ops + else + set -ge env + return 1 + end +end diff --git a/fish/theme-t31m/fish_prompt.fish b/fish/theme-t31m/fish_prompt.fish new file mode 100644 index 0000000..cc5cb2c --- /dev/null +++ b/fish/theme-t31m/fish_prompt.fish @@ -0,0 +1,63 @@ +function fish_prompt + set -l symbol "λ " + set -l code $status + + if test -n "$ssh_client" + set -l host (hostname -s) + set -l who (whoami) + echo -n -s (red)"("(cyan)"$who"(red)":"(cyan)"$host"(red)") "(off) + end + + if git::is_repo + set -l branch (git::branch_name ^/dev/null) + set -l ref (git show-ref --head --abbrev | awk '{print substr($0,0,7)}' | sed -n 1p) + + if git::is_stashed + echo -n -s (white)"^"(off) + end + + echo -n -s (red)"("(off) + + if git::is_dirty + printf (white)"*"(off) + end + + if command git symbolic-ref HEAD > /dev/null ^/dev/null + if git::is_staged + printf (cyan)"$branch"(off) + else + printf (yellow)"$branch"(off) + end + else + printf (dim)"$ref"(off) + end + + for remote in (git remote) + set -l behind_count (echo (command git rev-list $branch..$remote/$branch ^/dev/null | wc -l | tr -d " ")) + set -l ahead_count (echo (command git rev-list $remote/$branch..$branch ^/dev/null | wc -l | tr -d " ")) + + if test $ahead_count -ne 0; or test $behind_count -ne 0; and test (git remote | wc -l) -gt 1 + echo -n -s " "(orange)$remote(off) + end + + if test $ahead_count -ne 0 + echo -n -s (white)" +"$ahead_count(off) + end + + if test $behind_count -ne 0 + echo -n -s (white)" -"$behind_count(off) + end + end + + echo -n -s (red)")"(off) + end + + set t31m_custom (t31m_prompt) + + if test "$code" = 0 + echo -n -s "$t31m_custom"(red)"$symbol"(off) + else + echo -n -s (dim)"$symbol"(off) + end + +end diff --git a/fish/theme-t31m/fish_right_prompt.fish b/fish/theme-t31m/fish_right_prompt.fish new file mode 100644 index 0000000..1149bff --- /dev/null +++ b/fish/theme-t31m/fish_right_prompt.fish @@ -0,0 +1,143 @@ +# Colors +function orange + set_color -o ee5819 +end + +function yellow + set_color -o b58900 +end + +function red + set_color -o d30102 +end + +function cyan + set_color -o 2aa198 +end + +function white + set_color -o fdf6e3 +end + +function dim + set_color -o 4f4f4f +end + +function off + set_color -o normal +end + +# Git +function git::is_repo + test -d .git; or command git rev-parse --git-dir >/dev/null ^/dev/null +end + +function git::ahead -a ahead behind diverged none + not git::is_repo; and return + + set -l commit_count (command git rev-list --count --left-right "@{upstream}...HEAD" ^/dev/null) + + switch "$commit_count" + case "" + # no upstream + case "0"\t"0" + test -n "$none"; and echo "$none"; or echo "" + case "*"\t"0" + test -n "$behind"; and echo "$behind"; or echo "-" + case "0"\t"*" + test -n "$ahead"; and echo "$ahead"; or echo "+" + case "*" + test -n "$diverged"; and echo "$diverged"; or echo "±" + end +end + +function git::branch_name + git::is_repo; and begin + command git symbolic-ref --short HEAD ^/dev/null; + or command git show-ref --head -s --abbrev | head -n1 ^/dev/null + end +end + +function git::is_dirty + git::is_repo; and not command git diff --no-ext-diff --quiet --exit-code +end + +function git::is_staged + git::is_repo; and begin + not command git diff --cached --no-ext-diff --quiet --exit-code + end +end + +function git::is_stashed + git::is_repo; and begin + command git rev-parse --verify --quiet refs/stash >/dev/null + end +end + +function git::is_touched + git::is_repo; and begin + test -n (echo (command git status --porcelain)) + end +end + +function git::untracked + git::is_repo; and begin + command git ls-files --other --exclude-standard + end +end + +# Kubernetes + +function k8s::current_context + command kubectl config current-context +end + +function k8s::current_namespace + command kubectl config view --minify -o jsonpath='{.contexts[0].context.namespace}' +end + +# Terraform + +# Test whether this is a terraform directory by finding .tf files +function terraform::directory + command find . -name '*.tf' >/dev/null ^/dev/null -maxdepth 0 +end + +function terraform::workspace + terraform::directory; and begin + test -e .terraform/environment + end +end + +function fish_right_prompt + + if test "$theme_complete_path" = "yes" + set cwd (prompt_pwd) + else + set cwd (basename (prompt_pwd)) + + if git::is_repo + set root_folder (command git rev-parse --show-toplevel ^/dev/null) + set parent_root_folder (dirname $root_folder) + set cwd (echo $PWD | sed -e "s|$parent_root_folder/||") + end + end + + command -sq kubectl; and k8s::current_context >/dev/null 2>/dev/null; and begin + set -l k8s_namespace (k8s::current_namespace) + if test -z "$k8s_namespace" + printf (yellow)"("(dim)(k8s::current_context)(yellow)") "(off) + else + printf (yellow)"("(dim)(k8s::current_context)"/$k8s_namespace"(yellow)") "(off) + end + end + + if terraform::workspace + set terraform_workspace_name (command cat .terraform/environment) + printf (yellow)"("(dim)$terraform_workspace_name(yellow)") "(off) + end + + printf (yellow)"("(dim)$cwd(yellow)") "(off) + printf (dim)(date +%H(yellow):(dim)%M(yellow):(dim)%S)(off) + +end diff --git a/fish/theme-t31m/fish_title.fish b/fish/theme-t31m/fish_title.fish new file mode 100644 index 0000000..34e2c5d --- /dev/null +++ b/fish/theme-t31m/fish_title.fish @@ -0,0 +1,3 @@ +function fish_title + echo "$PWD | $_" | sed "s|$HOME|~|g" +end \ No newline at end of file diff --git a/fish/theme-t31m/t31m_prompt.fish b/fish/theme-t31m/t31m_prompt.fish new file mode 100644 index 0000000..4c291ed --- /dev/null +++ b/fish/theme-t31m/t31m_prompt.fish @@ -0,0 +1,20 @@ +# by T31M +function prompt::awsenv + if test -n "$env" + echo -n -s "[aws=$env]" + end +end + +function prompt::pyenv + if test -n "$PYENV_VERSION" + echo -n -s "[$PYENV_VERSION]" + end +end + +function t31m_prompt + set awsenv (prompt::awsenv) + set pyenv (prompt::pyenv) + echo -n -s (red)"$awsenv"(cyan)"$pyenv" +end + + diff --git a/fish_setup.sh b/fish_setup.sh new file mode 100644 index 0000000..e7126b4 --- /dev/null +++ b/fish_setup.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +# install fisher +curl https://git.io/fisher --create-dirs -sLo ~/.config/fish/functions/fisher.fish + +# copy fish config +if test -f "~/.config/fish/config.fish"; then + echo "config.fish already exist, please rename to continue." + exit 1 +fi +cp fish/config.fish ~/.config/fish/ + +# copy fishfile +if test -f "~/.config/fish/fishfile"; then + echo "fishfile already exist, please rename to continue." + exit 1 +fi +cp fish/fishfile ~/.config/fish/ + +# install packages from fishfile +fish -c "fisher self-update" +fish -c fisher +fish -c "fisher add (pwd)/fish/theme-t31m" + +# customize theme / prompt +cp dot.config/fish/functions/* ~/.config/fish/functions + +# set fish default promt +chsh -s /usr/bin/fish + +cp dot.vimrc ~/.vimrc diff --git a/main.sh b/main.sh index 305c5ab..f4349c5 100644 --- a/main.sh +++ b/main.sh @@ -1,5 +1,32 @@ #!/bin/bash -#Baseline Packages i need (Debian specific...) +USER=t31m + +# we need to be root for this install script +if [[ $EUID -ne 0 ]]; then + echo "This script must be run as root" + exit 1 +fi + +# Ubuntu Baseline Packages + +# update repos + system +apt get update && apt upgrade + +# install env packages +apt install git fish fzf pyenv pyenv-virtualenv + +# install docker stable, if this failes try below +curl -fsSL https://get.docker.com | sh +#curl -fsSL https://test.docker.com | sh + +# install docker-compose +curl -L https://github.com/docker/compose/releases/download/1.25.4/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose +chmod +x /usr/local/bin/docker-compose + +usermod -aG docker t31m + +# install nvm +curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh | bash + -sudo apt-get update