From 181ad4416a34ccd47065c9e35673b7fd6038124b Mon Sep 17 00:00:00 2001 From: Sage Date: Wed, 21 Jan 2026 14:10:08 +0000 Subject: [PATCH] Add hr switch --- modules/home/terminal/hr/default.nix | 4 +- modules/home/terminal/hr/hr.sh | 80 +++++++++++++++++++++++----- 2 files changed, 67 insertions(+), 17 deletions(-) diff --git a/modules/home/terminal/hr/default.nix b/modules/home/terminal/hr/default.nix index 653c43b..a91d6f2 100644 --- a/modules/home/terminal/hr/default.nix +++ b/modules/home/terminal/hr/default.nix @@ -11,11 +11,10 @@ in { options.mods.terminal.hr.enable = lib.mkEnableOption "Hefring (Work Tooling)"; config = lib.mkIf cfg.hr.enable { - home.packages = [hr]; + programs.zsh.initExtra = builtins.readFile ./hr.sh; systemd.user.services = { google-db-proxy-test = { - enable = true; Unit = { Description = "Google Cloud SQL Proxy (Test)"; After = ["network.target"]; @@ -30,7 +29,6 @@ in { }; google-db-proxy-prod = { - enable = true; Unit = { Description = "Google Cloud SQL Proxy (Prod)"; After = ["network.target"]; diff --git a/modules/home/terminal/hr/hr.sh b/modules/home/terminal/hr/hr.sh index 14ee834..ebaa4fd 100644 --- a/modules/home/terminal/hr/hr.sh +++ b/modules/home/terminal/hr/hr.sh @@ -1,8 +1,17 @@ -#!/usr/bin/env bash +# Set default PROJECT_ID if not already set +if [[ -z "$PROJECT_ID" ]]; then + export PROJECT_ID="mk2-test" +fi -set -e +_hr_usage() { + echo "Usage: hr " + echo "Commands:" + echo " switch Switch PROJECT_ID between mk2-test and mk2-prod" + echo " init py Initialize a python devenv environment (git-ignored)" + echo " freeze Freeze dependencies to requirements.txt" +} -if [ "$1" = "init" ] && [ "$2" = "py" ]; then +_hr_init_py() { echo "Initializing python devenv..." # 1. Init devenv @@ -15,7 +24,7 @@ if [ "$1" = "init" ] && [ "$2" = "py" ]; then echo "Direnv allowed" else echo "Error: devenv not found in path." - exit 1 + return 1 fi if [ -f .gitignore.bak ]; then @@ -91,11 +100,12 @@ EOF echo "Direnv allowed" else echo "Error: direnv not found in path." - exit 1 + return 1 fi +} -elif [ "$1" = "freeze" ]; then - extra_index_url="https://europe-west1-python.pkg.dev/mk2-prod/python-packages/simple/" +_hr_freeze() { + local extra_index_url="https://europe-west1-python.pkg.dev/mk2-prod/python-packages/simple/" # Install the auth plugin and keyring CLI uv pip install keyrings.google-artifactregistry-auth==1.1.2 keyring @@ -106,11 +116,53 @@ elif [ "$1" = "freeze" ]; then # Generate requirements.txt echo "--extra-index-url ${extra_index_url}" > requirements.txt uv pip freeze --exclude-editable >> requirements.txt +} -else - echo "Usage: hr " - echo "Commands:" - echo " init py Initialize a python devenv environment (git-ignored)" - echo " freeze Freeze dependencies to requirements.txt" - exit 1 -fi +hr() { + if [[ $# -eq 0 ]]; then + _hr_usage + return 1 + fi + + local command="$1" + shift + + if [[ "$command" == "switch" ]]; then + if [[ -z "$1" ]]; then + # Toggle between test and prod + if [[ "$PROJECT_ID" == "mk2-test" ]]; then + export PROJECT_ID="mk2-prod" + echo "Switched PROJECT_ID to mk2-prod" + else + export PROJECT_ID="mk2-test" + echo "Switched PROJECT_ID to mk2-test" + fi + elif [[ "$1" == "test" ]]; then + export PROJECT_ID="mk2-test" + echo "Set PROJECT_ID to mk2-test" + elif [[ "$1" == "prod" ]]; then + export PROJECT_ID="mk2-prod" + echo "Set PROJECT_ID to mk2-prod" + else + echo "Usage: hr switch [test|prod]" + return 1 + fi + return 0 + fi + + # Run original logic in a subshell to preserve set -e behavior without affecting current shell + ( + set -e + # Restore arguments for processing + set -- "$command" "$@" + + if [ "$1" = "init" ] && [ "$2" = "py" ]; then + _hr_init_py + elif [ "$1" = "freeze" ]; then + _hr_freeze + else + _hr_usage + exit 1 + fi + ) +}