Last active
September 21, 2024 10:35
-
-
Save jefftriplett/e7d4eade12e30001065eed2636010772 to your computer and use it in GitHub Desktop.
Example Justfile Alfred Extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
2021-2024 - Jeff Triplett | |
gist: https://gist.github.com/jefftriplett/e7d4eade12e30001065eed2636010772 | |
pip install typer pydantic | |
Inspired/jumpstarted by: https://github.com/kjaymiller/Bunch_Alfred | |
""" | |
import os | |
import subprocess | |
import typer | |
from pathlib import Path | |
from pydantic import BaseModel | |
class Command(BaseModel): | |
arg: str | |
subtitle: str | |
title: str | |
class CommandContainer(BaseModel): | |
items: list[Command] | |
JUST_BIN_DEFAULT = ( | |
"/opt/homebrew/bin/just" | |
if Path("/opt/homebrew/bin/just").exists() | |
else "/usr/local/bin/just" | |
) | |
JUST_BIN = os.environ.get("JUST_BIN", JUST_BIN_DEFAULT) | |
JUST_CONFIG = os.environ.get("JUST_CONFIG", Path.home() / "justfile") | |
def main(indent: int = None): | |
cmd = subprocess.run( | |
[ | |
f"{JUST_BIN}", | |
f"--justfile={JUST_CONFIG}", | |
"--summary", | |
], | |
capture_output=True, | |
) | |
items = cmd.stdout.decode("utf-8").strip() | |
commands = items.split(" ") | |
if len(commands) == 0: | |
commands = ["--help"] | |
container = CommandContainer( | |
items=[ | |
Command( | |
arg=f"{JUST_BIN} --justfile={JUST_CONFIG} {cmd}", | |
subtitle=f"run the {cmd} command", | |
title=f"{cmd}".strip(), | |
) | |
for cmd in items.split(" ") | |
if cmd | |
] | |
) | |
print(container.model_dump_json(indent=indent)) | |
if __name__ == "__main__": | |
typer.run(main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment