messing around with nested commands in click

This commit is contained in:
Brendan Haines 2024-09-13 19:59:01 -06:00
parent 66e0ef1a51
commit 830ec3b3d0
6 changed files with 104 additions and 0 deletions

16
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,16 @@
{
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
},
"editor.formatOnSave": true,
"files.trimTrailingWhitespace": true,
"[python]": {
"diffEditor.ignoreTrimWhitespace": false,
"gitlens.codeLens.symbolScopes": [
"!Module"
],
"editor.formatOnType": false,
"editor.wordBasedSuggestions": "off",
"editor.defaultFormatter": "ms-python.black-formatter"
}
}

View File

@ -6,6 +6,7 @@ name = "pypi"
[packages]
gotify = "*"
click = "*"
toml = "*"
[dev-packages]

66
goat_monitor.py Normal file
View File

@ -0,0 +1,66 @@
# %% imports
import subprocess
from pathlib import Path
from typing import List
import click
import gotify
import toml
__version__ = "v0.0.0"
# %% commands
@click.command()
@click.pass_context
# @click.option(
# "--config",
# default="~/.config/goat_monitor.toml",
# type=click.Path(exists=False, writable=True, path_type=Path),
# help="Specify a .toml configuration file location",
# )
# def init(config: Path):
# with open(config, "w") as f:
# cfg_data = dict()
# toml.dump(cfg_data, f)
def create_config(ctx):
print("init called")
print(ctx)
@click.group(invoke_without_command=True)
@click.argument("command", nargs=-1, required=False, type=str)
@click.option(
"--config",
default="~/.config/goat_monitor.toml",
type=click.Path(path_type=Path),
help="Use a .toml configuration file",
)
@click.option("--init", is_flag=True, default=False, help="Create a configuration file")
@click.pass_context
def wrap(ctx, command: List[str], config: Path, init: bool): # , config: Path):
"""Wrap an arbitrary command with gotify notifications"""
if init:
print("calling init")
create_config(command)
else:
# run the command
result = subprocess.run(
command,
shell=True,
stderr=subprocess.STDOUT,
stdout=subprocess.PIPE,
encoding="utf-8",
)
# TODO: print lines real time as the subprocess runs
print(result.stdout, end="")
return result.returncode
# %% main
if __name__ == "__main__":
wrap()

17
pyproject.toml Normal file
View File

@ -0,0 +1,17 @@
[tool.black]
line-length = 120
exclude = '''
/(
\.eggs
| \.git
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| \.vscode
| _build
| buck-out
| build
| dist
)/
'''

4
setup.cfg Normal file
View File

@ -0,0 +1,4 @@
[flake8]
exclude = **/*__init__.py,.venv/*,tox/*,build/*,dist/*
ignore = C901,E203,E731,W503
max-line-length = 120

0
setup.py Normal file
View File