Skip to content

Instantly share code, notes, and snippets.

@haryp2309
Last active November 1, 2024 13:24
Show Gist options
  • Save haryp2309/9750c4db0b41471ccda2219d90c9c31b to your computer and use it in GitHub Desktop.
Save haryp2309/9750c4db0b41471ccda2219d90c9c31b to your computer and use it in GitHub Desktop.
My Just Script
#! /bin/env python3
from dataclasses import dataclass
import argparse
from subprocess import run, check_output
from pathlib import Path
from typing import Optional
import logging
from os import environ
SCRIPTS_DIR = Path(environ.get("MY_SCRIPTS_DIR") or "")
EDITOR = Path(environ.get("EDITOR") or "")
SHELL = ["fish", "-c"]
@dataclass
class Command:
sh: str
help: str
cwd: Optional[Path] = None
def get_help_str(self):
sh_str = self.sh
if len(sh_str) > 40:
sh_str = sh_str[:37] + "..."
post_help = f" (Shortcut for: '{sh_str}')" if len(sh_str) <= 40 else ""
return self.help + post_help
commands = {
"start-podman": Command(
sh="systemctl --user start podman.socket",
help="Start the user-level socket for Podman, so apps like Pods can connect to it.",
),
"stop-podman": Command(
sh="systemctl --user stop podman.socket",
help="Stop the user-level socket for Podman.",
),
"update": Command(
sh="./update_packages.py",
cwd=SCRIPTS_DIR,
help="Update all global packages and apps on your system which are not updatable via Gnome Software.",
),
"fishconfig": Command(
sh=f"{EDITOR} ~/.config/fish/config.fish",
help="Edit fish config.",
),
}
def main():
parser = argparse.ArgumentParser(
prog="just",
description="Shortcuts for often used commands",
)
command_parser = parser.add_subparsers(
metavar="COMMAND", required=True, dest="command"
)
for key, command in commands.items():
command_parser.add_parser(
name=key,
help=command.get_help_str(),
)
command_parser.add_parser(
name="help",
help="Show this help message and exit.",
)
args = parser.parse_args()
command = args.command
if command == "help":
parser.print_help()
return
command_sh = commands[command]
run([*SHELL, command_sh.sh], cwd=command_sh.cwd)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
logging.error("Keyboard interrupt...")
exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment