Skip to content

Instantly share code, notes, and snippets.

@SidIcarus
Last active November 24, 2021 00:40
Show Gist options
  • Save SidIcarus/67b9cb8067b57ea9932319d9e834b914 to your computer and use it in GitHub Desktop.
Save SidIcarus/67b9cb8067b57ea9932319d9e834b914 to your computer and use it in GitHub Desktop.
environment bootstrap scripts
#!/bin/bash
# shellcheck disable=1090,1091,2059
# set ENV default value to 'dev''
ENV="${1:-dev}"
# import (source) utility variables and functions
. "$DIR/bootstrap/utils"
# Terminal output to illustrate progress
printf "${GREEN}[1/4]⏳${GREY} Bootstrapping environment ..."
# Exit if any subcommand fails
set -e
# DEBUG
set -x
# Determine OS (uname) (-s only platform?)
# Currently only intended to support MacOS, *Nix (Ubuntu), & Windows
# thus no need to really check what distro of linux
case "$(uname -s)" in
*Darwin*) OS="MACOS" ;;
*CYGWIN*|*MINGW*|*MSYS*) OS="WINDOWS";;
*Linux*) OS="LINUX" ;;
*) printf "❌${RED} This OS is currently unsupported.\n{NO_COLOR}"; exit
esac
# Save script's current directory
DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# shellcheck disable=SC2154
# ret=last_stdout; test if ret != 0 printf failed to stderr then exit
trap 'ret=$?; [ $ret -ne 0 ] && printf "failed\n\n" >&2; exit $ret' exit
# Load these steps
. "$DIR/bootstrap/check_dependencies" "$OS" "$DIR" "$ENV"
. "$DIR/bootstrap/clone_repository" "$OS" "$DIR" "$ENV"
. "$DIR/bootstrap/install_plugins" "$OS" "$DIR" "$ENV"
# see if local customizations are present in $HOME || $DIR
if -f "$HOME/.laptop.local"; then . "$HOME/.laptop.local";
elif -f "$DIR/.laptop.local"; then . "$DIR/.laptop.local"; fi
set +e
set +x
# Terminal output to illustrate the script has finished
printf "${CLEAR_LINE}[4/4]🎉${GREEN} Finished!\n${NO_COLOR}"
#!/bin/bash
# shellcheck disable=1090,1091,2059
OS="$1"
DIR="$2"
ENV="$3"
# import (source) utility variables and functions
. "$DIR/bootstrap/utils"
SET_SHELL=${3:-"bash"}
case "$SET_SHELL" in
bash) rc="bashrc" ;;
zsh) rc="zshrc" ;;
*) printf "❌${RED} Unsupported shell option.\n{NO_COLOR}"; exit ;;
esac
#
# args
append_to_shellrc() {
local text="$1" "$2"
local rc=".$2"
local skip_new_line="${3:-0}" # default 0
# if $rc.local is a file and writable
if -w "$HOME/$rc.local"
then shellrc="$HOME/$rc.local"
else shellrc="$HOME/$rc"
fi
if ! grep -Fqs "$text" "$shellrc"; then
if ! "$skip_new_line"
then printf "%s\n" "$text" >> "$shellrc"
else printf "\n%s\n" "$text" >> "$shellrc"
fi
fi
}
#
setup_shell() {
# if the dir .bin is not there, make it
[ ! -d "$HOME/.bin/" ] && mkdir "$HOME/.bin"
# if the shell config file isn't present, make it
[ ! -f "$HOME/.$rc" ] && touch "$HOME/.$rc"
# shellcheck disable=SC2016
append_to_shellrc 'export PATH="$HOME/.bin:$PATH"' "$rc"
case "$SET_SHELL" in
bash)
# shellcheck disable=SC2016
append_to_shellrc '. $HOME/.$rc' "bash_profile"
;;
zsh)
case "$SHELL" in
*/zsh) : ;;
*) chsh -s "$(which zsh)" ;;
esac
;;
*) printf "❌${RED} Unsupported shell option.\n{NO_COLOR}"; exit ;;
esac
}
#
check_mac_dep() {
setup_shell
HOMEBREW_PREFIX="/usr/local"
if -d "$HOMEBREW_PREFIX"; then
# if brew_prefix is readable
[ -r "$HOMEBREW_PREFIX" ] && sudo chown -R "$LOGNAME:admin" /usr/local
else
sudo mkdir "$HOMEBREW_PREFIX"
sudo chflags norestricted "$HOMEBREW_PREFIX"
sudo chown -R "$LOGNAME:admin" "$HOMEBREW_PREFIX"
fi
# Check if Homebrew is installed
if ! command brew -v >&-; then
# if xcode CLI tools isn't installed, install it'
if ! command xcode-select -v >&-; then xcode-select --install; fi
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" >&-
append_to_shellrc '# recommended by brew doctor'
# shellcheck disable=SC2016
append_to_shellrc 'export PATH="/usr/local/bin:$PATH"' 1
# if ! grep -Fqs "/usr/local/bin:$PATH"
# export PATH="/usr/local/bin:$PATH"
else # Update if it is
brew update >&-
fi
}
#
check_nix_dep() {
# setup_shell
printf "❌${RED} $OS is currently unsupported.\n{NO_COLOR}"; exit
}
#
check_win_dep() {
printf "❌${RED} $OS is currently unsupported.\n{NO_COLOR}"; exit
}
# ------------------------------- Executable ------------------------------- #
printf "${CLEAR_LINE} ${GREEN}[2/4]⏳${GREY} Checking dependencies ..."
case "$OS" in
MACOS) check_mac_dep ;;
LINUX|UBUNTU) check_nix_dep ;;
WINDOWS) check_win_dep ;;
*) printf "❌${RED} $OS is currently unsupported.\n{NO_COLOR}"; exit ;;
esac
#!/bin/bash
# shellcheck disable=1090,1091,2059
OS="$1"
DIR="$2"
ENV="$3"
# import (source) utility variables and functions
. "$DIR/bootstrap/utils"
#
# args
clone() {
printf "⏳ $1 is being cloned"
cd "$DIR/.."
if ! -d "$1"; then
# don't stop the script if there is an error while cloning
set +e
# if the last command gives a non-zero exit code we should warn the user
git clone "$2" >&- || \
printf "${CLEAR_LINE}❌${RED} $1 failed to clone! You might not have permissions.\n${NO_COLOR}"
set -e
fi
cd "$DIR"
}
#
mac_ini_repo() {
# Get Bitbucket credentials
echo "Please enter your bitbucket"
read -r "Username:" bbuser
read -spr "Password:" bbpw
}
#
nix_ini_repo() {
printf "❌${RED} $OS is currently unsupported.\n{NO_COLOR}"; exit
}
#
win_ini_repo() {
printf "❌${RED} $OS is currently unsupported.\n{NO_COLOR}"; exit
}
# ------------------------------- Executable ------------------------------- #
printf "${CLEAR_LINE} ${GREEN}[2/4]⏳${GREY} Cloning repository ..."
case "$OS" in
MACOS) mac_ini_repo ;;
LINUX|UBUNTU) nix_ini_repo ;;
WINDOWS) win_ini_repo ;;
*) printf "❌${RED} $OS is currently unsupported.\n{NO_COLOR}"; exit ;;
esac
#!/bin/bash
# shellcheck disable=1090,1091,2059
OS="$1"
DIR="$2"
ENV="$3"
USER="$4"
# import (source) utility variables and functions
. "$DIR/bootstrap/utils"
#
install_mac_plugins() {
# need add creating a gpg
gpg_key=1
GIT_CONFIGS=(
"branch.autoSetupRebase always"
"color.ui auto"
"core.autocrlf input"
"core.pager cat"
"credential.helper osxkeychain"
"merge.ff false"
"pull.rebase true"
"push.default simple"
"rebase.autostash true"
"rerere.autoUpdate true"
"rerere.enabled true"
"user.name $USER@adventive.com"
"user.email $USER@adventive.com"
"user.signingkey ${gpg_key}"
)
# Brew package dependencies
BREWS=(
bash
bash-completion
bash-git-prompt
checkbashisms
composer
coreutils
curl
docker-machine
docker-machine-driver-xhyve
git
git-extras
git-ssh
imagemagick
openssl
openvpn
php71
php71-imagick
php71-intl
php71-xdebug
reattach-to-user-namespace
tmux
wget
zsh
zsh-syntax-hightlighting
)
# Brew cask packages, useful tools
CASKS=(
alfred
aptanastudio
beyond-compare
ccleaner
dash
docker
firefox
gitkraken
google-chrome
iterm2
lumen
onyx
shift-it
sourcetree
sublime-text
visual-studio-code
)
fonts=(
font-anonymice-powerline
font-consolas-for-powerline
font-dejavu-sans-mono-for-powerline
font-droid-sans-mono-for-powerline
font-fira-mono-for-powerline
font-inconsolata-dz-for-powerline
font-inconsolata-for-powerline
font-inconsolata-g-for-powerline
font-liberation-mono-for-powerline
font-menlo-for-powerline
font-meslo-lg-for-powerline
font-monofur-for-powerline
font-roboto-mono-for-powerline
font-source-code-pro-for-powerline
font-ubuntu-mono-derivative-powerline
font-droid-sans
font-inconsolata
font-open-sans
font-roboto
font-roboto-condensed
font-roboto-mono
font-roboto-slab
font-robotomono-nerd-font
font-robotomono-nerd-font-mono
font-source-sans-pro
font-ubuntu
)
# Intersection of currently installed brew packages and required brew packages
ibrew=($(comm -13 <(brew list -1) <(echo "${BREWS[@]}" | tr " " "\n")))
install "brew install" "brew" "${ibrew[@]}"
# Intersection of currently installed brew casks and required casks
icask=($(comm -13 <(brew cask list -1) <(echo "${CASKS[@]}" | tr " " "\n")))
install "brew cask install" "cask" "${icask[@]}"
# Installing some fonts
# Need to add a intersection check for previously installed fonts
brew tap caskroom/fonts >&-
install "brew cask install" "${fonts[@]}"
# verify if all is well with brew setup thus far
[[ ! ( $(brew doctor) == "Your system is ready to brew.") ]] || \
printf "${CLEAR_LINE}❌${RED} Issues with brew installation.\n${NO_COLOR}"; exit
# "Setting git defaults ..."
#for config in "${GIT_CONFIGS[@]}"
#do git config --global "${config}"; done
#gpg --keyserver hkp://pgp.mit.edu --recv ${gpg_key}
# Installing mac CLI ..."
#sh -c "$(curl -fsSL https://raw.githubusercontent.com/guarinogabriel/mac-cli/master/mac-cli/tools/install)"
}
#
install_nix_plugins() {
printf "❌${RED} $OS is currently unsupported.\n{NO_COLOR}"; exit
}
#
install_win_plugins() {
printf "❌${RED} $OS is currently unsupported.\n{NO_COLOR}"; exit
}
# ------------------------------- Executable ------------------------------- #
printf "${CLEAR_LINE} ${GREEN}[3/4]⏳${GREY} Installing plugins ..."
case "$OS" in
MACOS) install_nix_plugins ;;
LINUX|UBUNTU) install_mac_plugins ;;
WINDOWS) install_win_plugins ;;
*) printf "❌${RED} $OS is currently unsupported.\n{NO_COLOR}"; exit ;;
esac
#!/bin/bash
# shellcheck disable=1070,1090,1091,2034,2059,2154,2162
# This file isn't intended to be run, just has commands i've found I need for
# setting up our automated emv setup
# Capture into an array
C=($(command))
# To use comm to return the intersection:
comm -1 -2 file1 file2
# remove unique file 1 & common to both - leaving only those unique to file2.
comm -13 FILE1 FILE2
# return intersection of two pipelines (process subsitution)
comm -12 <(ls one) <(ls two)
# set complement
comm -23 <(sort set1) <(sort set2)
# intersection of two lists
sort <(ls one) <(ls two) | uniq -d
# symmetric difference
sort <(ls one) <(ls two) | uniq -u
# cmd output to var
output="$(ls -1)"
# Replace new lines with spaces
echo list_w_lines | tr "\n" " "
# Replaces spaces with new lines
echo list_w_spaces | tr " " "\n"
# Save script's current directory
DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
#cd "${DIR}"
# writes to console the text 'STDOUT'
echo STDOUT
# reads from console to var STDIN
read STDIN
# to access the entire array
"${array_var[@]}"
# There are many tools that don't have silent options
# to fix this, we can redirect the output to
> /dev/null # is equal to > &-
# source is an alias to . (dot)
source # = .
# is the Path for the Working Directory
$PWD
# is an array variable whose members are the source filenames
BASH_SOURCE
# st rips shortest match of $substring from back of $string
${string%substring}
# print system information
uname
# for kernel and more information use the option:
-a
# just the OS string
-s
# --- TPUT cmds --- #
## foreground & background colour commands
# Set the background colour using ANSI escape
tput setab [1-7]
# Set the foreground colour using ANSI escape
tput setaf [1-7]
# ansi colors
Num Colour #define R G B
0 black COLOR_BLACK 0,0,0
1 red COLOR_RED 1,0,0
2 green COLOR_GREEN 0,1,0
3 yellow COLOR_YELLOW 1,1,0
4 blue COLOR_BLUE 0,0,1
5 magenta COLOR_MAGENTA 1,0,1
6 cyan COLOR_CYAN 0,1,1
7 white COLOR_WHITE 1,1,1
## Text mode commands
tput bold # Select bold mode
tput dim # Select dim (half-bright) mode
tput smul # Enable underline mode
tput rmul # Disable underline mode
tput rev # Turn on reverse video mode
tput smso # Enter standout (bold) mode
tput rmso # Exit standout mode
## Cursor movement commands
tput cup Y X # Move cursor to screen postion X,Y (top left is 0,0)
tput cuf N # Move N characters forward (right)
tput cub N # Move N characters back (left)
tput cuu N # Move N lines up
tput ll # Move to last line, first column (if no cup)
tput sc # Save the cursor position
tput rc # Restore the cursor position
tput lines # Output the number of lines of the terminal
tput cols # Output the number of columns of the terminal
## Clear and insert commands
tput ech N # Erase N characters
tput clear # Clear screen and move the cursor to 0,0
tput el 1 # Clear to beginning of line
tput el # Clear to end of line
tput ed # Clear to end of screen
tput ich N # Insert N characters (moves rest of line forward!)
tput il N # Insert N lines
## Other commands
tput sgr0 # Reset text format to the terminal's default
tput bel # Play a bell
## Scripts
# tput accepts scripts containing one command per line, which are executed
# in order before tput exits.
# Avoid temporary files by echoing a multiline string and piping it:
echo -e "setf 7\nsetb 1" | tput -S # set fg white and bg red
# You can seperate the importing of variables from one file to another by
# exiting the file before it executes anything
# example code below
#!/usr/bin/env bash
export var1="/data/share"
export var2='password'
# --- End Definitions Section ---
# check if we are being sourced by another script or shell
[[ "${#BASH_SOURCE[@]}" -gt "1" ]] && { return 0; }
# --- Begin Code Execution Section ---
echo "Hello"
echo $var1
echo $var2
# example code end
# logic operators are always useful to have around, remember 0 = true, 1 = false
# some people forget these things!
$ false && echo howdy!
$ true && echo howdy!
howdy!
$ true || echo howdy!
$ false || echo howdy!
howdy!
#!/bin/bash
# shellcheck disable=SC2034
NO_COLOR='\033[0m'
CLEAR_LINE='\r\033[K'
GREEN='\033[0;32m'
GREY='\e[1;32m'
RED='\033[0;31m'
YELLOW='\033[0;33m'
# Install a given set of packages via the given cmd
# args: "install_cmd" package_type packages_array
install() {
local cmd="$1"
local type="$2"
local packages="${3:-1}" # everything from pos-param until end is included
if "${#packages[@]}" > 0; then
# don't stop the script if there is an error installing the package
set +e
for pkg in "${packages[@]}"; do
"$cmd $pkg" >&- || \
printf "${CLEAR_LINE}❌${RED} Failed to install $type: $pkg${NO_COLOR}\n"
done
set -e
fi
}
# ------------------------ End Definitions Section ------------------------ #
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment