Skip to content

Commit

Permalink
breka out properties into own file
Browse files Browse the repository at this point in the history
  • Loading branch information
joelmccracken committed Dec 15, 2024
1 parent 1b05fc5 commit 2db0038
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 77 deletions.
106 changes: 39 additions & 67 deletions ws_tool/lib/bootstrap_doctor.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,6 @@
# bootstrapping and doctoring is closely related enough for now
# decide to keep these in the same file.

prop_ws_settings_file_check() {
if [[ -f "$WORKSTATION_SETTINGS_FILE" ]] ; then
echo "settings file exists";
else
echo "no settings file found (expected at $WORKSTATION_SETTINGS_FILE)" 2>&1
fi
}

prop_ws_settings_file_fix() {
if [[ -f "$WORKSTATION_SETTINGS_FILE" ]] ; then
echo "settings file exists";
else
echo "no settings file found (expected at $WORKSTATION_SETTINGS_FILE)" 2>&1
fi
}

prop_ws_check_workstation_dir() {
if [ -d "$WORKSTATION_DIR" ]; then
echo "WORKSTATION_DIR exists"
if [ -x "$WORKSTATION_DIR/ws_tool/ws" ]; then
echo "WORKSTATION_DIR contains ws executable"
return 0
else
echo "$WORKSTATION_DIR does not contain the ws tool" 1>&2
return 2
fi
else
echo "$WORKSTATION_DIR (WORKSTATION_DIR) is absent" 1>&2
echo "(is workstation installed to a custom location? set WORKSTATION_DIR=path/to/workstation)" 1>&2
return 1
fi
}

prop_ws_check_workstation_dir_fix() {
# TODO this is basically a copy/paste of ws_install.sh
# somehow figure out another way to do this?
TMPDIR=$(mktemp -d "/tmp/ws-install-XXXXXX")

# installer of ws tool/project
cd "$TMPDIR"
curl -L https://github.com/joelmccracken/workstation/archive/${WORKSTATION_VERSION}.tar.gz | tar zx

mkdir -p "$WORKSTATION_DIR"
mv "${TMPDIR}"/workstation-*/{,.[^.]}* "$WORKSTATION_DIR"
}

prop_ws_check_workstation_repo() {
if [ -d "$WORKSTATION_DIR/.git" ]; then
echo "WORKSTATION_DIR git directory exists"
return 0
else
echo "$WORKSTATION_DIR/.git directory is absent" 1>&2
return 1
fi
}

prop_ws_check_workstation_repo_fix() {
cd "$WORKSTATION_DIR"
git init .
git remote add origin "$WORKSTATION_REPO_GIT_ORIGIN"
git fetch
git reset --mixed "origin/$WORKSTATION_VERSION"
}

doctor_command() {
echo "doctor!";
Expand Down Expand Up @@ -100,15 +37,16 @@ bootstrap_command_setup() {
}

bootstrap_command() {
echo "bootstrapping '$WORKSTATION_NAME'"


echo "bootstrapping base properties"

ensure_props "${bootstrap_props[@]}"

echo "bootstrapping workstation properties for ${WORKSTATION_NAME}"
}

bootstrap_props=(
prop_ws_check_workstation_dir
prop_ws_check_has_git
prop_ws_check_workstation_repo
)

Expand All @@ -126,7 +64,7 @@ ensure_props () {
else
echo "checking: $current ... FAIL"
echo "fixing: $current ..."
"${current}_fix"
interact "${current}_fix"
fix_result="$?"
if (( fix_result == 0 )); then
echo "fixing: $current .... OK"
Expand All @@ -146,3 +84,37 @@ ensure_props () {
fi
done
}

: "${interact_always_continue:=0}"

interact() {
local has_continue=0 the_command="$1"

if [ "$WORKSTATION_INTERACTIVE" != "true" ]; then
"$the_command";
return 0;
fi

if [ "$interact_always_continue" == "1" ]; then
has_continue=1
fi

while [ "$has_continue" != "1" ]; do
read -e -n 1 -p "About to run '$1', continue? (c/q/!/p/?):" response
case "$response" in
c) has_continue=1;;
q) echo "quitting..."; exit 0;;
\!) interact_always_continue=1; has_continue=1;;
p) type "$the_command";;
?) interact_help;;
*) echo "unrecognized response '$response'."
esac
done

# if we're here, we must have gotten continue
"$the_command"
}

interact_help() {
echo "c=continue, q=quit, !=always continue, p=print function definition, ?=print this help";
}
3 changes: 2 additions & 1 deletion ws_tool/lib/logging.bash
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ log() {

log_level_num "$this_lvl"
this_lvl_num="$__ret";
msg="$this_lvl $(date): $@"
if (( this_lvl_num <= global_lvl_num )); then
echo "$this_lvl $(date): $@" 1>&2
echo "$msg" 1>&2
fi
}

Expand Down
65 changes: 65 additions & 0 deletions ws_tool/lib/properties.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env bash

prop_ws_settings_file_check() {
if [[ -f "$WORKSTATION_SETTINGS_FILE" ]] ; then
echo "settings file exists";
else
echo "no settings file found (expected at $WORKSTATION_SETTINGS_FILE)" 2>&1
fi
}

prop_ws_settings_file_fix() {
if [[ -f "$WORKSTATION_SETTINGS_FILE" ]] ; then
echo "settings file exists";
else
echo "no settings file found (expected at $WORKSTATION_SETTINGS_FILE)" 2>&1
fi
}

prop_ws_check_workstation_dir() {
if [ -d "$WORKSTATION_DIR" ]; then
echo "WORKSTATION_DIR exists"
if [ -x "$WORKSTATION_DIR/ws_tool/ws" ]; then
echo "WORKSTATION_DIR contains ws executable"
return 0
else
echo "$WORKSTATION_DIR does not contain the ws tool" 1>&2
return 2
fi
else
echo "$WORKSTATION_DIR (WORKSTATION_DIR) is absent" 1>&2
echo "(is workstation installed to a custom location? set WORKSTATION_DIR=path/to/workstation)" 1>&2
return 1
fi
}

prop_ws_check_workstation_dir_fix() {
# TODO this is basically a copy/paste of ws_install.sh
# somehow figure out another way to do this?
TMPDIR=$(mktemp -d "/tmp/ws-install-XXXXXX")

# installer of ws tool/project
cd "$TMPDIR"
curl -L https://github.com/joelmccracken/workstation/archive/${WORKSTATION_VERSION}.tar.gz | tar zx

mkdir -p "$WORKSTATION_DIR"
mv "${TMPDIR}"/workstation-*/{,.[^.]}* "$WORKSTATION_DIR"
}

prop_ws_check_workstation_repo() {
if [ -d "$WORKSTATION_DIR/.git" ]; then
echo "WORKSTATION_DIR git directory exists"
return 0
else
echo "$WORKSTATION_DIR/.git directory is absent" 1>&2
return 1
fi
}

prop_ws_check_workstation_repo_fix() {
cd "$WORKSTATION_DIR"
git init .
git remote add origin "$WORKSTATION_REPO_GIT_ORIGIN"
git fetch
git reset --mixed "origin/$WORKSTATION_VERSION"
}
1 change: 1 addition & 0 deletions ws_tool/lib/settings.bash
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ WORKSTATION_NAME_ARG=
: "${WORKSTATION_DIR:="$WORKSTATION_CONFIG_DIR/src"}"
: "${WORKSTATION_REPO_GIT_ORIGIN:="https://github.com/joelmccracken/workstation.git"}"
: "${WORKSTATION_VERSION:=master}"
: "${WORKSTATION_INTERACTIVE:=false}"

export WORKSTATION_NAME # META:workstation_setting
export WORKSTATION_VERBOSE # META:workstation_setting
Expand Down
1 change: 1 addition & 0 deletions ws_tool/test/bootstrap_doctor.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
setup (){
load 'test_helper/helper'
_setup_common
. "$PROJECT_ROOT/ws_tool/lib/properties.bash"
. "$PROJECT_ROOT/ws_tool/lib/bootstrap_doctor.bash"
}

Expand Down
17 changes: 8 additions & 9 deletions ws_tool/ws
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ WS_SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd

. "${WS_SCRIPT_DIR}/lib/settings.bash"
. "${WS_SCRIPT_DIR}/lib/logging.bash"
. "${WS_SCRIPT_DIR}/lib/properties.bash"
. "${WS_SCRIPT_DIR}/lib/bootstrap_doctor.bash"

__ret= # hack to use return values
Expand Down Expand Up @@ -43,13 +44,7 @@ print_usage() {
echo "-n NAME, --name NAME : Specify the name of this workstation."
echo "-s PATH, --settings PATH : Path to settings file to use."
echo "-c PATH, --config PATH : Path to configuration file."
}

print_workstation_names() {
echo "Possible workstation names are:"
echo " - glamdring (primary laptop)"
echo " - belthronding (cloud VM)"
echo " - aeglos (work computer)"
echo "-i, --interactive : Interactive mode."
}

process_cli_args() {
Expand Down Expand Up @@ -79,6 +74,9 @@ process_cli_args() {
-h|--help|help)
usage_and_quit 0;
;;
-i|--interactive)
WORKSTATION_INTERACTIVE=true;
;;
-*|--*)
echo "Unknown option ${args[i]}";
exit 1;
Expand All @@ -90,12 +88,13 @@ process_cli_args() {
WS_COMMAND="$current";
;;
*)
echo UNKNOWN CASE;
error "unknown option or subcommand '$current'";
exit 10;
;;
esac
done

[ -n "$WORKSTATION_NAME_ARG" ] && WORKSTATION_NAME="$WORKSTATION_NAME_ARG"
[ -n "$WORKSTATION_NAME_ARG" ] && WORKSTATION_NAME="$WORKSTATION_NAME_ARG"

return 0
}
Expand Down

0 comments on commit 2db0038

Please sign in to comment.