diff --git a/dev-env b/dev-env index a717cac..64efc69 100755 --- a/dev-env +++ b/dev-env @@ -109,10 +109,11 @@ update_dirs $DEV_ENV/env/.local $HOME/.local # SCRIPTS mkdir -p ~/.local/scripts/{hypr,utils} >/dev/null 2>&1 -mkdir -p ~/.local/scripts/utils/kanatactl >/dev/null 2>&1 +mkdir -p ~/.local/scripts/utils/{kanatactl,hpa} >/dev/null 2>&1 update_dirs $DEV_ENV/env/.local/scripts/hypr $HOME/.local/scripts/hypr copy_files $DEV_ENV/env/.local/scripts/hypr $HOME/.local/scripts/hypr copy_files "$DEV_ENV/env/.local/scripts/utils/kanatactl" "$HOME/.local/scripts/utils/kanatactl" +copy_files "$DEV_ENV/env/.local/scripts/utils/hpa" "$HOME/.local/scripts/utils/hpa" copy_files $DEV_ENV/env/.local/scripts $HOME/.local/scripts # SYSTEMD diff --git a/env/.local/scripts/hpa b/env/.local/scripts/hpa index d1924a7..c7433fa 100755 --- a/env/.local/scripts/hpa +++ b/env/.local/scripts/hpa @@ -27,10 +27,6 @@ CONSULT_TEMPLATE_URL="ssh://git@git.housh.dev:2222/hhe/consult-template.git" HPA_PLAYBOOK_URL="ssh://git@git.housh.dev:2222/michael/ansible-hpa-playbook.git" HPA_VAULT_SECRET_KEY="${HPA_VAULT_SECRET_KEY:-vault-pass}" -echoerr() { - echo -e "\e[31m[ERROR]:\e[0m $*" -} - generate-completion() { local output_dir output type type=${1:-"zsh"} @@ -71,40 +67,6 @@ run() { "$HPA_DOCKER_IMAGE:$HPA_DOCKER_TAG" "$@" } -# Helper to generate the project directory properly. -# -# This allows project to be created using `hpa create Customer`, and the project -# will be created with an actual directory named '25.11.05.Customer'. -# -# This also handles setting parent directory to the `/consults` directory inside -# the container, otherwise the project fails to get created. -# -# We also suppress all output and then generate the actual directory path on the local -# file system. -# -# This is generally called by using the 'hpa-create' script, that shows a spinner and -# cleans up the output. Which is generally used like: `cd $(hpa-create Foo)` to automatically -# cd into the generated project and get to work. -create() { - local project project_container_name dir_name - project=${1:-""} - - [[ -z $project ]] && echoerr "Must supply a project name." && exit 1 - - project_container_name="$(run create --quiet "/consults/$(date '+%Y.%m.%d').$project")" - dir_name="$CONSULTS_DIR/$(basename "$project_container_name")" - - # Initialize git repo in the project and the initial commit. - pushd "$(echo "$dir_name" | tr -d '\r' | head -1)" &>/dev/null || exit 1 - ( - git init - git add . - git commit --all --message="Initial commit" - ) &>/dev/null - popd &>/dev/null - echo "$dir_name" -} - ############################## MAIN ############################## first_arg=${1:-""} @@ -112,9 +74,6 @@ first_arg=${1:-""} if [[ $first_arg == "init" ]]; then shift init "$@" -elif [[ $first_arg == "create" ]]; then - shift - echo "$(create "$@")" else run "$@" fi diff --git a/env/.local/scripts/hpa-create b/env/.local/scripts/hpa-create index e79eb3a..475034b 100755 --- a/env/.local/scripts/hpa-create +++ b/env/.local/scripts/hpa-create @@ -1,31 +1,10 @@ #!/usr/bin/env bash -# NB: This wrapper script is required for the spinner to work, otherwise if declared in -# the 'hpa' (container wrapper script) then it doesn't work. -# https://github.com/charmbracelet/gum/issues/419 - -usage() { - cat <<'EOF' - -Create a home performance assesment project directory. This handles creating the project using -the preferred directory name (example '25.11.05.McTestface'). - -It show's a spinner while the project is being created and supresses all output except for the -generated directory. This allows the command to be called using `cd $(hpa-create McTestface)` to -cd into the directory once it's created and get to work! - -USAGE: - $ hpa-create - -FLAGS: - -h | --help: Show this help page. - -EOF -} - +title="Creating project..." +script="${SCRIPTS:-$HOME/.local/scripts}/utils/hpa/hpa-create" first_arg=${1:-""} if [[ $first_arg == "-h" ]] || [[ $first_arg == "--help" ]]; then - usage && exit 0 + . "$script" $* else - gum spin --show-output --title "Creating project..." -- bash -c "hpa create $*" | tr -d '\r' | head -1 + gum spin --show-output --title "$title" -- bash -c "$script $*" | tr -d '\r' | head -1 fi diff --git a/env/.local/scripts/utils/hpa/hpa-create b/env/.local/scripts/utils/hpa/hpa-create new file mode 100755 index 0000000..36dc785 --- /dev/null +++ b/env/.local/scripts/utils/hpa/hpa-create @@ -0,0 +1,138 @@ +#!/usr/bin/env bash + +set -e +set -o nounset +set -o pipefail + +# NOTE: This script is required so that we can use 'gum spin' during creation +# of the project, otherwise the spinner doesn't actually show up when +# the utility functions live in the same file. +# https://github.com/charmbracelet/gum/issues/419 + +SCRIPTS=${SCRIPTS:-$HOME/.local/scripts} +THIS_FILE=${BASH_SOURCE[0]} +LOG_LABEL=$(basename "$THIS_FILE") +THIS=${THIS:-$LOG_LABEL} +LOG_FILE=${LOG_FILE:-"$LOG_LABEL.log"} +CONSULTS_DIR=${CONSULTS_DIR:-$HOME/work/consults} +CONSULT_ORIGIN_BASE_URL=${CONSULT_ORIGIN_BASE_URL:-"ssh://git@git.housh.dev:2222/consults"} + +declare no_git_flag no_push_flag +no_git_flag="0" +no_push_flag="0" + +usage() { + cat < + +FLAGS: + -h | --help: Show this help page. + -g | --no-git: Do not initialize a git repository for the project. + -p | --no-push: Do not push project to remote repository. + +ENVIRONMENT: + CONSULTS_DIR: Sets the location of new porjects + (default: ~/work/consults) + + CONSULT_ORIGIN_BASE_URL: Sets the base url for the project git origin. + (default: ssh://git@git.housh.dev:2222/consults) + +EOF +} + +# Logging utility function, use in place of echo. +log() { + logging log --source "$THIS_FILE" "$@" +} + +create() { + local customer container_dir + customer=${1:-""} + + [[ -z $customer ]] && + log --error "Must supply a customer name for the project" && + exit 1 + + [[ ! -f $SCRIPTS/hpa ]] && + log --error "Unable to find the 'hpa' script." && + exit 1 + + log "Generating project for: '$customer'" + + container_dir=$( + "$SCRIPTS/hpa" create --quiet "/consults/$(date '+%Y.%m.%m').$customer" + ) + echo "$CONSULTS_DIR/$(basename "$container_dir")" +} + +initialize-git() { + local dir + read -r dir + + dir=$(echo "$dir" | tr -d '\r' | head -1) + + if [[ $no_git_flag == "0" ]]; then + + [[ ! -d $dir ]] && + log --error "Could not initialize git directory not found: '$dir'" && + exit 1 + + log "Initializing git repository" + + pushd "$dir" &>/dev/null || exit 1 + ( + git init + git add . + git commit --all --message="Initial commit" + git remote add origin "$CONSULT_ORIGIN_BASE_URL/$(basename "$dir")" + if [[ $no_push_flag == "0" ]]; then + git push --set-upstream origin main + fi + ) &>/dev/null + popd &>/dev/null + else + log "Skipping git initialization" + fi + + echo "$dir" +} + +################################################################################ +# MAIN +################################################################################ + +# Setup logging file and label. +source "$SCRIPTS/hypr/logging" +setup-logging "$LOG_FILE" "$LOG_LABEL" + +declare customer output + +while [[ $# -gt 0 ]]; do + if [[ $1 == "-h" ]] || [[ $1 == "--help" ]]; then + usage && exit 0 + elif [[ $1 == "-g" ]] || [[ $1 == "--no-git" ]]; then + no_git_flag="1" + elif [[ $1 == "-p" ]] || [[ $1 == "--no-push" ]]; then + no_push_flag="1" + else + customer=$1 + fi + shift +done + +output=$(create "$customer" | initialize-git) +echo "$output"