feat: remove deprecated shell scripts and add Python alternatives for all of them
Some checks failed
Nix Format Check / check-format (push) Failing after 37s
Some checks failed
Nix Format Check / check-format (push) Failing after 37s
This commit is contained in:
172
bin/dotf
172
bin/dotf
@@ -1,122 +1,108 @@
|
||||
#!/usr/bin/env bash
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# strict mode
|
||||
set -euo pipefail
|
||||
IFS=$'\n\t'
|
||||
import os
|
||||
import sys
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
# Script constants
|
||||
readonly DOTFILES_ROOT="$HOME/.dotfiles"
|
||||
readonly DOTFILES_BIN="$DOTFILES_ROOT/bin"
|
||||
DOTFILES_ROOT = os.path.expanduser("~/.dotfiles")
|
||||
DOTFILES_BIN = os.path.join(DOTFILES_ROOT, "bin")
|
||||
DOTFILES_PATH = DOTFILES_ROOT # For compatibility with the original scripts
|
||||
|
||||
# Source helper functions
|
||||
if [[ ! -f "$DOTFILES_BIN/helpers/functions.sh" ]]; then
|
||||
echo "Error: Required helper functions not found"
|
||||
exit 1
|
||||
fi
|
||||
source "$DOTFILES_BIN/helpers/functions.sh"
|
||||
# Import helper functions
|
||||
sys.path.append(DOTFILES_BIN)
|
||||
from helpers.functions import printfe, logo
|
||||
|
||||
# Command functions
|
||||
update() {
|
||||
local update_script="$DOTFILES_BIN/actions/update.sh"
|
||||
if [[ ! -x "$update_script" ]]; then
|
||||
printfe "%s\n" "red" "Error: Update script not found or not executable"
|
||||
def run_script(script_path, args):
|
||||
"""Run an action script with the given arguments"""
|
||||
if not os.path.isfile(script_path) or not os.access(script_path, os.X_OK):
|
||||
printfe("red", f"Error: Script not found or not executable: {script_path}")
|
||||
return 1
|
||||
fi
|
||||
"$update_script" $@
|
||||
}
|
||||
|
||||
result = subprocess.run([script_path] + args, env={**os.environ, "DOTFILES_PATH": DOTFILES_PATH})
|
||||
return result.returncode
|
||||
|
||||
hello() {
|
||||
local term_script="$DOTFILES_BIN/actions/hello.sh"
|
||||
if [[ ! -x "$term_script" ]]; then
|
||||
printfe "%s\n" "red" "Error: Terminal script not found or not executable"
|
||||
return 1
|
||||
fi
|
||||
"$term_script" "$@"
|
||||
}
|
||||
def update(args):
|
||||
"""Run the update action"""
|
||||
return run_script(f"{DOTFILES_BIN}/actions/update.py", args)
|
||||
|
||||
help() {
|
||||
local help_script="$DOTFILES_BIN/actions/help.sh"
|
||||
if [[ ! -x "$help_script" ]]; then
|
||||
printfe "%s\n" "red" "Error: Help script not found or not executable"
|
||||
return 1
|
||||
fi
|
||||
"$help_script" "$@"
|
||||
}
|
||||
def hello(args):
|
||||
"""Run the hello action"""
|
||||
return run_script(f"{DOTFILES_BIN}/actions/hello.py", args)
|
||||
|
||||
secrets() {
|
||||
local secrets_script="$DOTFILES_BIN/actions/secrets.sh"
|
||||
if [[ ! -x "$secrets_script" ]]; then
|
||||
printfe "%s\n" "red" "Error: Secrets script not found or not executable"
|
||||
return 1
|
||||
fi
|
||||
"$secrets_script" "$@"
|
||||
}
|
||||
def help(args):
|
||||
"""Run the help action"""
|
||||
return run_script(f"{DOTFILES_BIN}/actions/help.py", args)
|
||||
|
||||
auto_start() {
|
||||
local auto_start_script="$DOTFILES_BIN/actions/auto-start.sh"
|
||||
if [[ ! -x "$auto_start_script" ]]; then
|
||||
printfe "%s\n" "red" "Error: Auto-start script not found or not executable"
|
||||
return 1
|
||||
fi
|
||||
"$auto_start_script" "$@"
|
||||
}
|
||||
def secrets(args):
|
||||
"""Run the secrets action"""
|
||||
return run_script(f"{DOTFILES_BIN}/actions/secrets.py", args)
|
||||
|
||||
ensure_git_hooks() {
|
||||
local hooks_dir="$DOTFILES_ROOT/.git/hooks"
|
||||
local target_link="$DOTFILES_BIN/actions/git"
|
||||
def auto_start(args):
|
||||
"""Run the auto-start action"""
|
||||
return run_script(f"{DOTFILES_BIN}/actions/auto-start.py", args)
|
||||
|
||||
def ensure_git_hooks():
|
||||
"""Ensure git hooks are correctly set up"""
|
||||
hooks_dir = os.path.join(DOTFILES_ROOT, ".git/hooks")
|
||||
target_link = os.path.join(DOTFILES_BIN, "actions/git")
|
||||
|
||||
# Validate target directory exists
|
||||
if [[ ! -d "$target_link" ]]; then
|
||||
printfe "%s\n" "red" "Error: Git hooks source directory does not exist: $target_link"
|
||||
if not os.path.isdir(target_link):
|
||||
printfe("red", f"Error: Git hooks source directory does not exist: {target_link}")
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Handle existing symlink
|
||||
if [[ -L "$hooks_dir" ]]; then
|
||||
local current_link
|
||||
current_link=$(readlink "$hooks_dir")
|
||||
if [[ "$current_link" != "$target_link" ]]; then
|
||||
printfe "%s\n" "yellow" "Incorrect git hooks symlink found. Removing and recreating..."
|
||||
rm "$hooks_dir"
|
||||
else
|
||||
if os.path.islink(hooks_dir):
|
||||
current_link = os.readlink(hooks_dir)
|
||||
if current_link != target_link:
|
||||
printfe("yellow", "Incorrect git hooks symlink found. Removing and recreating...")
|
||||
os.remove(hooks_dir)
|
||||
else:
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
|
||||
# Handle existing directory
|
||||
if [[ -d "$hooks_dir" ]]; then
|
||||
printfe "%s\n" "yellow" "Removing existing hooks directory..."
|
||||
rm -rf "$hooks_dir"
|
||||
fi
|
||||
if os.path.isdir(hooks_dir) and not os.path.islink(hooks_dir):
|
||||
printfe("yellow", "Removing existing hooks directory...")
|
||||
import shutil
|
||||
shutil.rmtree(hooks_dir)
|
||||
|
||||
# Create new symlink
|
||||
if ln -s "$target_link" "$hooks_dir"; then
|
||||
printfe "%s\n" "green" "Git hooks successfully configured!"
|
||||
else
|
||||
printfe "%s\n" "red" "Failed to create git hooks symlink"
|
||||
try:
|
||||
os.symlink(target_link, hooks_dir)
|
||||
printfe("green", "Git hooks successfully configured!")
|
||||
return 0
|
||||
except Exception as e:
|
||||
printfe("red", f"Failed to create git hooks symlink: {e}")
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
main() {
|
||||
def main():
|
||||
# Ensure we're in the correct directory
|
||||
if [[ ! -d "$DOTFILES_ROOT" ]]; then
|
||||
printfe "%s\n" "red" "Error: Dotfiles directory not found"
|
||||
exit 1
|
||||
fi
|
||||
if not os.path.isdir(DOTFILES_ROOT):
|
||||
printfe("red", "Error: Dotfiles directory not found")
|
||||
return 1
|
||||
|
||||
# Setup git hooks
|
||||
ensure_git_hooks || exit 1
|
||||
if ensure_git_hooks() != 0:
|
||||
return 1
|
||||
|
||||
# Parse commands
|
||||
case "${1:-help}" in
|
||||
update) shift; update "$@" ;;
|
||||
help) shift; help "$@" ;;
|
||||
hello) shift; hello "$@" ;;
|
||||
secrets) shift; secrets "$@" ;;
|
||||
auto-start) shift; auto_start "$@" ;;
|
||||
*) help ;;
|
||||
esac
|
||||
}
|
||||
command = sys.argv[1] if len(sys.argv) > 1 else "help"
|
||||
args = sys.argv[2:]
|
||||
|
||||
main "$@"
|
||||
commands = {
|
||||
"update": update,
|
||||
"help": help,
|
||||
"hello": hello,
|
||||
"secrets": secrets,
|
||||
"auto-start": auto_start
|
||||
}
|
||||
|
||||
if command in commands:
|
||||
return commands[command](args)
|
||||
else:
|
||||
return help([])
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
|
Reference in New Issue
Block a user