176 lines
4.7 KiB
Python
Executable File
176 lines
4.7 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import os
|
|
import signal
|
|
import subprocess
|
|
import sys
|
|
|
|
|
|
def signal_handler(sig, frame):
|
|
print("Exiting.")
|
|
sys.exit(0)
|
|
|
|
|
|
signal.signal(signal.SIGINT, signal_handler)
|
|
|
|
# Script constants
|
|
DOTFILES_ROOT = os.path.expanduser("~/.dotfiles")
|
|
DOTFILES_BIN = os.path.join(DOTFILES_ROOT, "bin")
|
|
DOTFILES_PATH = DOTFILES_ROOT # For compatibility with the original scripts
|
|
|
|
# Import helper functions
|
|
sys.path.append(DOTFILES_BIN)
|
|
from helpers.functions import printfe, ensure_dependencies
|
|
|
|
ensure_dependencies()
|
|
|
|
|
|
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
|
|
|
|
result = subprocess.run(
|
|
[script_path] + args, env={**os.environ, "DOTFILES_PATH": DOTFILES_PATH}
|
|
)
|
|
return result.returncode
|
|
|
|
|
|
def update(args):
|
|
"""Run the update action"""
|
|
return run_script(f"{DOTFILES_BIN}/actions/update.py", args)
|
|
|
|
|
|
def hello(args):
|
|
"""Run the hello action"""
|
|
return run_script(f"{DOTFILES_BIN}/actions/hello.py", args)
|
|
|
|
|
|
def help(args):
|
|
"""Run the help action"""
|
|
return run_script(f"{DOTFILES_BIN}/actions/help.py", args)
|
|
|
|
|
|
def service(args):
|
|
"""Run the service/docker action"""
|
|
return run_script(f"{DOTFILES_BIN}/actions/service.py", args)
|
|
|
|
|
|
def lint(args):
|
|
"""Run the lint action"""
|
|
return run_script(f"{DOTFILES_BIN}/actions/lint.py", args)
|
|
|
|
|
|
def timers(args):
|
|
"""Run the timers action"""
|
|
return run_script(f"{DOTFILES_BIN}/actions/timers.py", args)
|
|
|
|
|
|
def source(args):
|
|
"""Run the source action"""
|
|
return run_script(f"{DOTFILES_BIN}/actions/source.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 not os.path.isdir(target_link):
|
|
printfe(
|
|
"red", f"Error: Git hooks source directory does not exist: {target_link}"
|
|
)
|
|
return 1
|
|
|
|
# Handle existing symlink
|
|
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
|
|
|
|
# Handle existing directory
|
|
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
|
|
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
|
|
|
|
|
|
def main():
|
|
# Ensure we're in the correct directory
|
|
if not os.path.isdir(DOTFILES_ROOT):
|
|
printfe("red", "Error: Dotfiles directory not found")
|
|
return 1
|
|
|
|
# Setup git hooks
|
|
if ensure_git_hooks() != 0:
|
|
return 1
|
|
|
|
# Parse commands
|
|
command = sys.argv[1] if len(sys.argv) > 1 else "help"
|
|
args = sys.argv[2:]
|
|
|
|
commands = {
|
|
"update": update,
|
|
"help": help,
|
|
"hello": hello,
|
|
"service": service,
|
|
"lint": lint,
|
|
"timers": timers,
|
|
"source": source,
|
|
}
|
|
|
|
if command in commands:
|
|
return commands[command](args)
|
|
else:
|
|
# For invalid commands, show error after logo
|
|
if command != "help":
|
|
from helpers.functions import logo
|
|
|
|
logo(continue_after=True)
|
|
print()
|
|
printfe("red", f"✗ Error: Unknown command '{command}'")
|
|
|
|
# Provide helpful hints for common mistakes
|
|
if command == "ls":
|
|
printfe("yellow", " Hint: Did you mean 'dotf service ls'?")
|
|
elif command == "list":
|
|
printfe("yellow", " Hint: Did you mean 'dotf service list'?")
|
|
|
|
print()
|
|
# Now print help text without logo
|
|
dotfiles_path = os.environ.get(
|
|
"DOTFILES_PATH", os.path.expanduser("~/.dotfiles")
|
|
)
|
|
try:
|
|
with open(
|
|
f"{dotfiles_path}/bin/resources/help.txt", "r", encoding="utf-8"
|
|
) as f:
|
|
print(f.read())
|
|
except OSError as e:
|
|
printfe("red", f"Error reading help file: {e}")
|
|
return 1
|
|
return 1
|
|
return help([])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|