From d5757d231f5a5d62bbc6e7e9c289805387f47b57 Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Sun, 11 Dec 2022 17:30:10 +0200 Subject: [PATCH] Tests, scripts, moved install.sh to scripts folder --- install.sh | 88 ------------------------- local/bin/dotfiles | 40 ++++++----- local/bin/x-backup-mysql-with-prefix.sh | 43 ++++++++++++ local/bin/x-check-git-attributes | 10 +++ local/bin/x-validate-sha256sum.sh | 52 +++++++++++++++ scripts/gh-extensions.sh | 5 ++ scripts/install.sh | 88 +++++++++++++++++++++++++ scripts/settler.sh | 1 + 8 files changed, 224 insertions(+), 103 deletions(-) delete mode 100755 install.sh create mode 100755 local/bin/x-backup-mysql-with-prefix.sh create mode 100644 local/bin/x-validate-sha256sum.sh create mode 100644 scripts/install.sh diff --git a/install.sh b/install.sh deleted file mode 100755 index 87bf178..0000000 --- a/install.sh +++ /dev/null @@ -1,88 +0,0 @@ -#!/bin/sh -# -# Usage: -# -# sh install.sh -# -# Environment variables: VERBOSE, CP, LN, MKDIR, RM, DIRNAME. -# -# env VERBOSE=1 sh install.sh -# -# DO NOT EDIT THIS FILE -# -# This file is generated by rcm(7) as: -# -# rcup -B 0 -g -# -# To update it, re-run the above command. -# -: ${VERBOSE:=0} -: ${CP:=/bin/cp} -: ${LN:=/bin/ln} -: ${MKDIR:=/bin/mkdir} -: ${RM:=/bin/rm} -: ${DIRNAME:=/usr/bin/dirname} -verbose() -{ - if [ "$VERBOSE" -gt 0 ]; then - echo "$@" - fi -} -handle_file_cp() -{ - if [ -e "$2" ]; then - printf "%s " "overwrite $2? [yN]" - read overwrite - case "$overwrite" in - y) - $RM -rf "$2" - ;; - *) - echo "skipping $2" - return - ;; - esac - fi - verbose "'$1' -> '$2'" - $MKDIR -p "$($DIRNAME "$2")" - $CP -R "$1" "$2" -} -handle_file_ln() -{ - if [ -e "$2" ]; then - printf "%s " "overwrite $2? [yN]" - read overwrite - case "$overwrite" in - y) - $RM -rf "$2" - ;; - *) - echo "skipping $2" - return - ;; - esac - fi - verbose "'$1' -> '$2'" - $MKDIR -p "$($DIRNAME "$2")" - $LN -sf "$1" "$2" -} -handle_file_ln "~/.dotfiles/bash_profile" "~/.bash_profile" -handle_file_ln "~/.dotfiles/bashrc" "~/.bashrc" -handle_file_ln "~/.dotfiles/config/alias" "~/.config/alias" -handle_file_ln "~/.dotfiles/config/antigen.zsh" "~/.config/antigen.zsh" -handle_file_ln "~/.dotfiles/config/exports" "~/.config/exports" -handle_file_ln "~/.dotfiles/config/functions" "~/.config/functions" -handle_file_ln "~/.dotfiles/config/gh/config.yml" "~/.config/gh/config.yml" -handle_file_ln "~/.dotfiles/config/git/config" "~/.config/git/config" -handle_file_ln "~/.dotfiles/config/git/gitignore" "~/.config/git/gitignore" -handle_file_ln "~/.dotfiles/config/wtf/config.yml" "~/.config/wtf/config.yml" -handle_file_ln "~/.dotfiles/git_profiles" "~/.git_profiles" -handle_file_ln "~/.dotfiles/huskyrc" "~/.huskyrc" -handle_file_ln "~/.dotfiles/local/bin/dotfiles" "~/.local/bin/dotfiles" -handle_file_ln "~/.dotfiles/local/bin/x-check-git-attributes" "~/.local/bin/x-check-git-attributes" -handle_file_ln "~/.dotfiles/local/bin/x-open-ports" "~/.local/bin/x-open-ports" -handle_file_ln "~/.dotfiles/rcrc" "~/.rcrc" -handle_file_ln "~/.dotfiles/ssh/allowed_signers" "~/.ssh/allowed_signers" -handle_file_ln "~/.dotfiles/ssh/config" "~/.ssh/config" -handle_file_ln "~/.dotfiles/vuerc" "~/.vuerc" -handle_file_ln "~/.dotfiles/zshrc" "~/.zshrc" diff --git a/local/bin/dotfiles b/local/bin/dotfiles index 9de5590..c9612f5 100755 --- a/local/bin/dotfiles +++ b/local/bin/dotfiles @@ -1,29 +1,27 @@ #!/usr/bin/env bash # -# Dotfiles and install helper +# Dotfiles manager and install helper # (c) Ismo Vuorinen 2022 # Licensed under MIT, see LICENSE # -# shellcheck source-path=SCRIPTDIR +# shellcheck source-path=$HOME/.dotfiles/local/bin # -# Helper variables, override with ENVs like `VERBOSE=1 helpers.sh help` +# Helper variables, override with ENVs like `VERBOSE=1 dotfiles help` : "${VERBOSE:=0}" : "${DOTFILES:=$HOME/.dotfiles}" -: "${INSTALL_SCRIPT:=$DOTFILES/install.sh}" +: "${INSTALL_SCRIPT:=$DOTFILES/scripts/install.sh}" : "${BREWFILE:=$DOTFILES/Brewfile}" -function usage -{ - echo $"Usage: $0 [install | brew | dotfiles]" - echo $" All commands have their own subcommands." - echo $" When in doubt run the subcommand to show list." -} +SCRIPT=$(basename "$0") function section_install { - USAGE_PREFIX="Usage: $0 install" + USAGE_PREFIX="-> $SCRIPT install" case "$1" in + all) + bash "$DOTFILES/scripts/settler.sh" && echo "🎉 Done!" + ;; antigen) curl -L git.io/antigen > "$DOTFILES/config/antigen.zsh" && echo "🎉 Done!" ;; @@ -44,7 +42,7 @@ function section_install function section_brew { - USAGE_PREFIX="Usage: $0 brew" + USAGE_PREFIX="-> $SCRIPT brew" if ! command -v brew &> /dev/null; then echo "brew could not be found, please install it first" @@ -79,18 +77,18 @@ function section_brew function section_dotfiles { - USAGE_PREFIX="Usage: $0 dotfiles" + USAGE_PREFIX="-> $SCRIPT dotfiles" case "$1" in link) rcup -B "$HOSTNAME" -v && echo "🎉 Done!" ;; update) - # Updates .dotfiles/install.sh and formats it + # Updates .dotfiles/scripts/install.sh and formats it rcup -B 0 -g \ | tee "$INSTALL_SCRIPT" 1> /dev/null \ && shfmt -w -l "$INSTALL_SCRIPT" \ - && sed -i '' "s|$HOME|\~|g" "$INSTALL_SCRIPT" \ + && sed -i '' "s|$HOME|\$HOME|g" "$INSTALL_SCRIPT" \ && echo "🎉 Done!" ;; shfmt) @@ -123,6 +121,18 @@ function section_dotfiles esac } +function usage +{ + echo $"Usage: $SCRIPT [install | brew | dotfiles]" + echo $" All commands have their own subcommands." + echo "" + section_install + echo "" + section_brew + echo "" + section_dotfiles +} + # The main loop. first keyword after $0 triggers section, or help. case "$1" in install) section_install "$2" ;; diff --git a/local/bin/x-backup-mysql-with-prefix.sh b/local/bin/x-backup-mysql-with-prefix.sh new file mode 100755 index 0000000..8e9472f --- /dev/null +++ b/local/bin/x-backup-mysql-with-prefix.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash +# +# Backup local MySQL Database tables with certain prefix. +# Ismo Vuorinen 2018 +# + +SCRIPT=$(basename "$0") +PREFIX=$1 +FILENAME=$2 +DATABASE=$3 + +: "${VERBOSE:=0}" +: "${DEFAULT_DATABASE:="wordpress"}" + +if [ -z "${PREFIX}" ]; then + echo "(!) TABLE_PREFIX (first argument) is missing" + echo "(>) Usage: $SCRIPT []" + echo " * = database table prefix, e.g. 'wp_'" + echo " * = FILENAME prefix, defaults to table prefix. Use something descriptive e.g. 'wordpress'" + echo " * = [optional] Third argument DATABASE, defaults to '$DEFAULT_DATABASE'." + exit 0 +fi + +if [ -z "${FILENAME}" ]; then + # echo "FILENAME (second argument) is missing, using PREFIX ($PREFIX)" + FILENAME=$PREFIX +fi + +if [ -z "${DATABASE}" ]; then + # echo "DATABASE (third argument) is missing, using default ($DEFAULT_DATABASE)" + DATABASE=$DEFAULT_DATABASE +fi + +TIMESTAMP=$(date "+%Y%m%d_%H%M%S") +FILENAME_TIMESTAMP="${DATABASE}_${FILENAME}_${TIMESTAMP}.sql" + +mysqldump \ + ${DATABASE} \ + "$( + echo "show tables like '${PREFIX}%';" \ + | mysql ${DATABASE} \ + | sed '/Tables_in/d' + )" > "${FILENAME_TIMESTAMP}" diff --git a/local/bin/x-check-git-attributes b/local/bin/x-check-git-attributes index a4da44f..2d191c8 100755 --- a/local/bin/x-check-git-attributes +++ b/local/bin/x-check-git-attributes @@ -1,6 +1,16 @@ #!/usr/bin/env bash +# +# Check git repo's files .gitattributes and are all of them mapped. +# Ismo Vuorinen 2022 +# + +if ! command -v git &> /dev/null; then + echo "git could not be found, please install it first" + exit +fi missing_attributes=$(git ls-files | git check-attr -a --stdin | grep "text: auto") + if [[ "$missing_attributes" ]]; then echo ".gitattributes rule missing for the following files:" echo "$missing_attributes" diff --git a/local/bin/x-validate-sha256sum.sh b/local/bin/x-validate-sha256sum.sh new file mode 100644 index 0000000..191b25a --- /dev/null +++ b/local/bin/x-validate-sha256sum.sh @@ -0,0 +1,52 @@ +#!/usr/bin/env bash +## +# This script contains helper for sha256 validating your downloads +# +# Source: https://gist.github.com/onnimonni/b49779ebc96216771a6be3de46449fa1 +# Author: Onni Hakala +# License: MIT +# +# Updated by Ismo Vuorinen 2022 +## + +if ! command -v sha256 &> /dev/null; then + echo "git could not be found, please install it first" + exit +fi + +# Stop program and give error message +# $1 - error message (string) +function error +{ + echo "(!) ERROR: $1" + exit 1 +} + +# return sha256sum for file +# $1 - filename (string) +function get_sha256sum +{ + sha256sum "$1" | head -c 64 +} + +# Good variable names pls +filename=$1 +file_hash=$2 + +# Check input +if [ -z "$filename" ]; then + error "You need to provide filename in first parameter" +fi + +if [ -z "$file_hash" ]; then + error "You need to provide sha256sum in second parameter" +fi + +# Check if the file is valid +if [ ! -f "$filename" ]; then + error "File $filename doesn't exist" +elif [ "$(get_sha256sum "$filename")" = "$file_hash" ]; then + echo "(*) Success: $filename matches provided sha256sum" +else + error "$filename doesn't match provided sha256sum" +fi diff --git a/scripts/gh-extensions.sh b/scripts/gh-extensions.sh index e4f0b5e..82d5f19 100755 --- a/scripts/gh-extensions.sh +++ b/scripts/gh-extensions.sh @@ -1,6 +1,11 @@ #!/usr/bin/env bash # Install GitHub CLI extensions +if ! command -v gh &> /dev/null; then + echo "gh (GitHub Client) could not be found, please install it first" + exit 1 +fi + extensions=( # GitHub CLI extension for reviewing Dependabot PRs. einride/gh-dependabot diff --git a/scripts/install.sh b/scripts/install.sh new file mode 100644 index 0000000..4775714 --- /dev/null +++ b/scripts/install.sh @@ -0,0 +1,88 @@ +#!/bin/sh +# +# Usage: +# +# sh install.sh +# +# Environment variables: VERBOSE, CP, LN, MKDIR, RM, DIRNAME. +# +# env VERBOSE=1 sh install.sh +# +# DO NOT EDIT THIS FILE +# +# This file is generated by rcm(7) as: +# +# rcup -B 0 -g +# +# To update it, re-run the above command. +# +: ${VERBOSE:=0} +: ${CP:=/bin/cp} +: ${LN:=/bin/ln} +: ${MKDIR:=/bin/mkdir} +: ${RM:=/bin/rm} +: ${DIRNAME:=/usr/bin/dirname} +verbose() +{ + if [ "$VERBOSE" -gt 0 ]; then + echo "$@" + fi +} +handle_file_cp() +{ + if [ -e "$2" ]; then + printf "%s " "overwrite $2? [yN]" + read overwrite + case "$overwrite" in + y) + $RM -rf "$2" + ;; + *) + echo "skipping $2" + return + ;; + esac + fi + verbose "'$1' -> '$2'" + $MKDIR -p "$($DIRNAME "$2")" + $CP -R "$1" "$2" +} +handle_file_ln() +{ + if [ -e "$2" ]; then + printf "%s " "overwrite $2? [yN]" + read overwrite + case "$overwrite" in + y) + $RM -rf "$2" + ;; + *) + echo "skipping $2" + return + ;; + esac + fi + verbose "'$1' -> '$2'" + $MKDIR -p "$($DIRNAME "$2")" + $LN -sf "$1" "$2" +} +handle_file_ln "$HOME/.dotfiles/bash_profile" "$HOME/.bash_profile" +handle_file_ln "$HOME/.dotfiles/bashrc" "$HOME/.bashrc" +handle_file_ln "$HOME/.dotfiles/config/alias" "$HOME/.config/alias" +handle_file_ln "$HOME/.dotfiles/config/antigen.zsh" "$HOME/.config/antigen.zsh" +handle_file_ln "$HOME/.dotfiles/config/exports" "$HOME/.config/exports" +handle_file_ln "$HOME/.dotfiles/config/functions" "$HOME/.config/functions" +handle_file_ln "$HOME/.dotfiles/config/gh/config.yml" "$HOME/.config/gh/config.yml" +handle_file_ln "$HOME/.dotfiles/config/git/config" "$HOME/.config/git/config" +handle_file_ln "$HOME/.dotfiles/config/git/gitignore" "$HOME/.config/git/gitignore" +handle_file_ln "$HOME/.dotfiles/config/wtf/config.yml" "$HOME/.config/wtf/config.yml" +handle_file_ln "$HOME/.dotfiles/git_profiles" "$HOME/.git_profiles" +handle_file_ln "$HOME/.dotfiles/huskyrc" "$HOME/.huskyrc" +handle_file_ln "$HOME/.dotfiles/local/bin/dotfiles" "$HOME/.local/bin/dotfiles" +handle_file_ln "$HOME/.dotfiles/local/bin/x-check-git-attributes" "$HOME/.local/bin/x-check-git-attributes" +handle_file_ln "$HOME/.dotfiles/local/bin/x-open-ports" "$HOME/.local/bin/x-open-ports" +handle_file_ln "$HOME/.dotfiles/rcrc" "$HOME/.rcrc" +handle_file_ln "$HOME/.dotfiles/ssh/allowed_signers" "$HOME/.ssh/allowed_signers" +handle_file_ln "$HOME/.dotfiles/ssh/config" "$HOME/.ssh/config" +handle_file_ln "$HOME/.dotfiles/vuerc" "$HOME/.vuerc" +handle_file_ln "$HOME/.dotfiles/zshrc" "$HOME/.zshrc" diff --git a/scripts/settler.sh b/scripts/settler.sh index 66bce86..fe70b1d 100755 --- a/scripts/settler.sh +++ b/scripts/settler.sh @@ -1,6 +1,7 @@ #!/usr/bin/env bash # # Settler - my macOS setup automator +# Ismo Vuorinen 2018 # Ask for the administrator password upfront sudo -v