functions.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #!/bin/bash
  2. mbrav_scripts_v="0.1.2"
  3. script_id="mbrav/configs v${mbrav_scripts_v}"
  4. # Load starship prompt if starship is installed
  5. function load_starship () {
  6. if [ -x "$(command -v starship)" ]; then
  7. __main() {
  8. local major="${BASH_VERSINFO[0]}"
  9. local minor="${BASH_VERSINFO[1]}"
  10. if ((major > 4)) || { ((major == 4)) && ((minor >= 1)); }; then
  11. source <(starship init bash --print-full-init)
  12. else
  13. source /dev/stdin <<<"$(starship init bash --print-full-init)"
  14. fi
  15. }
  16. __main
  17. unset -f __main
  18. fi
  19. }
  20. # If not running interactively, don't do anything
  21. # [[ $- = *i* ]] && load_starship || return
  22. # load_starship
  23. function check_sudo () {
  24. [[ $(whoami) != root ]] && error_msg "Please run script as root or sudo" 13
  25. warning_msg "Note: to run sudo and preserve passed env variables run with 'sudo -E'"
  26. }
  27. # Yes no prompt
  28. function yes_no_prompt() {
  29. # $1 - Space separated string for prompt
  30. # Sets $Y_N to:
  31. # 0 - yes
  32. # 1 - no
  33. local yes_no_finish=1
  34. while [ $yes_no_finish -ne 0 ]; do
  35. read -p "${GREEN}${BOLD}${1} ${YELLOW}${BOLD}y/n${CLEAR}:" Y_N
  36. case $Y_N in
  37. y|Y|yes) yes_no_finish=0 && Y_N=0 ;;
  38. n|N|no) yes_no_finish=0 && Y_N=1 ;;
  39. *) echo "${RED}${BOLD}[X] ${Y_N} is not a yes/no option!" ;;
  40. esac
  41. done
  42. }
  43. # Check for passed url
  44. function check_url() {
  45. # 0 - exists
  46. # 1 - does not
  47. if curl --output /dev/null --silent --head --fail "$1"; then
  48. echo 0
  49. else
  50. echo 1
  51. fi
  52. }
  53. function docker-compose-install() {
  54. # $1 - Docker compsoe version, otherwise default
  55. [[ -n $1 ]] && local docker_v=$1 || local docker_v=2.12.2
  56. local docker_url="https://github.com/docker/compose/releases/download/v${docker_v}/docker-compose-$(uname -s)-$(uname -m)"
  57. [[ $(check_url "$docker_url") -ne 0 ]] && error_msg "Bad url" 6
  58. success_msg "Valid url: $docker_url"
  59. sudo curl -L $docker_url -o /usr/local/bin/docker-compose
  60. sudo chmod +x /usr/local/bin/docker-compose
  61. success_msg "gocker compose installed"
  62. docker-compose --version
  63. }
  64. # Replace text and show interactive
  65. function replace-vars() {
  66. # $1 - string to find
  67. # $2 - string to change to
  68. # $3 - File path
  69. [[ $# -ne 3 ]] && error_msg "replace_vars() accepts 3 arguments, not $#" 22
  70. info_msg "Will replace '${1}' with '${2}' in file ${3}"
  71. grep_command=$(grep "${1}" "${3}")
  72. info_msg "Before:\n${BLUE}${grep_command}"
  73. sed -i "s,${1},${2},g" "${3}"
  74. grep_command=$(grep "${2}" "${3}")
  75. info_msg "After:\n${BLUE}${grep_command}"
  76. unset grep_command
  77. }
  78. # Save docker images
  79. function dock-save() {
  80. if [[ $# -lt 1 || $# -gt 2 ]]; then
  81. echo -e $RED"Must provide 2 arguments, docker image name and tar output file name (no exension)"$CLEAR
  82. exit 1
  83. fi
  84. mkdir -p ~/docker-images
  85. echo -e $GREEN"Exporting image $YELLOW$1 $GREEN to ~/docker-images/$YELLOW$2.tar.gz"$CLEAR
  86. docker save $1 | gzip -c > ~/docker-images/$2.tar.gz
  87. }
  88. # Auto completion for docker-save
  89. function _dock-save_completions() {
  90. [ "${#COMP_WORDS[@]}" != "2" ] && return || \
  91. COMPREPLY=($(compgen -W "$(docker images --format "{{.Repository}}:{{.Tag}}")" "${COMP_WORDS[1]}"))
  92. }
  93. complete -F _dock-save_completions dock-save
  94. # Interval command
  95. function do-interval() {
  96. # do-interval 1
  97. # $1 - interval in seconds
  98. # $2-n - command and any number of arguments to execute
  99. [[ $# -lt 2 ]] && error_msg "do-interval accepts no less than 2 arguments, passed $#" 22
  100. [[ $1 == ?(-)+([[:digit:]]) ]] || error_msg "$1 is not a number" 22
  101. watch -n $1 -d=cumulative ${@:2}
  102. }
  103. # Max 7z compression
  104. function 7z-max() {
  105. echo -e $GREEN$BOLD"7z max compression using lzma2"$CLEAR
  106. echo -e $YELLOW$BOLD"arg1$CLEAR - archive name without extension"
  107. echo -e $YELLOW$BOLD"arg2$CLEAR - folder name (optional)"
  108. if [ -z "$1" ]; then
  109. echo -e $RED$BOLD"Please provide at least one argument"$CLEAR
  110. exit 1
  111. elif [ -z "$2" ]; then
  112. FOLDER_NAME=("$1")
  113. echo -e $YELLOW"No folder name provided, using "$CYAN$FOLDER_NAME$CLEAR
  114. else
  115. FOLDER_NAME=("$2")
  116. fi
  117. 7z a -m0=lzma2 -mx $1.7z $FOLDER_NAME
  118. }
  119. # Clean non ascii chars from a file
  120. function ascii-clean() {
  121. local temp_file="$(rand-hex-ssl 6).tmp"
  122. tr -cd '\11\12\15\40-\176' < $1 > $temp_file
  123. mv $temp_file $1
  124. }
  125. function git-cred() {
  126. if [[ $PWD == *"$USER/dev/work"* ]]; then
  127. git-cred-mbrav
  128. else
  129. git-cred-mbrav
  130. fi
  131. echo "User: $(git config user.name)"
  132. echo "Email: $(git config user.email)"
  133. echo "Key: $(git config user.signingkey)"
  134. }
  135. # Attach to tmux session on shell login
  136. function start_tmux() {
  137. if type tmux &> /dev/null; then
  138. #if not inside a tmux session, and if no session is started, start a new session
  139. if [[ -z "$TMUX" && -z $TERMINAL_CONTEXT ]]; then
  140. (tmux -2 attach || tmux -2 new-session)
  141. fi
  142. fi
  143. }
  144. # start_tmux