-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CLI functionality and base pyproject.toml
- Loading branch information
1 parent
60ffe87
commit dac8759
Showing
7 changed files
with
138 additions
and
28 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,68 @@ | ||
"""Placeholder for packaging.""" | ||
|
||
import sys | ||
|
||
import click | ||
|
||
from monitor.main import start # noqa: F401 | ||
|
||
version = "0.0.0-a" | ||
|
||
|
||
@click.command() | ||
@click.argument("start", required=False) | ||
@click.argument("run", required=False) | ||
@click.option("--version", "-V", is_flag=True, help="Prints the version.") | ||
@click.option("--help", "-H", is_flag=True, help="Prints the help section.") | ||
@click.option( | ||
"--env", | ||
"-E", | ||
type=click.Path(exists=True), | ||
help="Environment configuration filepath.", | ||
) | ||
def commandline(*args, **kwargs) -> None: | ||
"""Starter function to invoke service-monitor via CLI commands. | ||
**Flags** | ||
- ``--version | -V``: Prints the version. | ||
- ``--help | -H``: Prints the help section. | ||
- ``--env | -E``: Environment configuration filepath. | ||
**Commands** | ||
``start | run``: Initiates the backup process. | ||
""" | ||
assert sys.argv[0].endswith("monitor"), "Invalid commandline trigger!!" | ||
options = { | ||
"--version | -V": "Prints the version.", | ||
"--help | -H": "Prints the help section.", | ||
"--env | -E": "Environment configuration filepath.", | ||
"start | run": "Initiates the backup process.", | ||
} | ||
# weird way to increase spacing to keep all values monotonic | ||
_longest_key = len(max(options.keys())) | ||
_pretext = "\n\t* " | ||
choices = _pretext + _pretext.join( | ||
f"{k} {'·' * (_longest_key - len(k) + 8)}→ {v}".expandtabs() | ||
for k, v in options.items() | ||
) | ||
if kwargs.get("version"): | ||
click.echo(f"service-monitor {version}") | ||
sys.exit(0) | ||
if kwargs.get("help"): | ||
click.echo( | ||
f"\nUsage: monitor [arbitrary-command]\nOptions (and corresponding behavior):{choices}" | ||
) | ||
sys.exit(0) | ||
trigger = kwargs.get("start") or kwargs.get("run") | ||
if trigger and trigger.lower() in ("start", "run"): | ||
# Click doesn't support assigning defaults like traditional dictionaries, so kwargs.get("max", 100) won't work | ||
start(env_file=kwargs.get("env")) | ||
sys.exit(0) | ||
elif trigger: | ||
click.secho(f"\n{trigger!r} - Invalid command", fg="red") | ||
else: | ||
click.secho("\nNo command provided", fg="red") | ||
click.echo( | ||
f"Usage: monitor [arbitrary-command]\nOptions (and corresponding behavior):{choices}" | ||
) | ||
sys.exit(1) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,24 @@ | ||
import os | ||
import platform | ||
|
||
import uvicorn | ||
from fastapi import FastAPI | ||
|
||
from monitor.routes import routes | ||
from monitor.squire import settings | ||
from monitor import router, squire | ||
|
||
|
||
def start() -> None: | ||
def start(env_file: str = None) -> None: | ||
"""Starter function for the API, which uses uvicorn server as trigger.""" | ||
squire.settings = squire.Settings().from_env_file( | ||
env_file=env_file | ||
or os.environ.get("env_file") | ||
or os.environ.get("ENV_FILE") | ||
or ".env" | ||
) | ||
app = FastAPI( | ||
routes=routes, | ||
routes=router.routes, | ||
title=f"Service monitor for {platform.uname().node}", | ||
) | ||
uvicorn.run(host=settings.monitor_host, port=settings.monitor_port, app=app) | ||
uvicorn.run( | ||
host=squire.settings.monitor_host, port=squire.settings.monitor_port, app=app | ||
) |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
[project] | ||
name = "service-monitor" | ||
dynamic = ["version", "dependencies"] | ||
description = "OS agnostic service monitoring API" | ||
readme = "README.md" | ||
authors = [{ name = "Vignesh Rao", email = "[email protected]" }] | ||
license = { file = "LICENSE" } | ||
classifiers = [ | ||
"License :: OSI Approved :: MIT License", | ||
"Programming Language :: Python :: 3", | ||
"Development Status :: 5 - Production/Stable", | ||
"Operating System :: MacOS :: MacOS X", | ||
"Operating System :: Microsoft :: Windows", | ||
"Operating System :: POSIX :: Linux", | ||
] | ||
keywords = ["service-monitor"] | ||
requires-python = ">=3.10" | ||
|
||
[tool.setuptools] | ||
packages = ["monitor"] | ||
|
||
[tool.setuptools.dynamic] | ||
version = {attr = "monitor.version"} | ||
dependencies = { file = ["requirements.txt"] } | ||
|
||
[project.optional-dependencies] | ||
dev = ["sphinx==5.1.1", "pre-commit", "recommonmark", "gitverse"] | ||
|
||
[project.scripts] | ||
# sends all the args to commandline function, where the arbitary commands as processed accordingly | ||
monitor = "monitor:commandline" | ||
|
||
[build-system] | ||
requires = ["setuptools", "wheel"] | ||
build-backend = "setuptools.build_meta" | ||
|
||
[project.urls] | ||
Homepage = "https://github.com/thevickypedia/service-monitor" | ||
Docs = "https://thevickypedia.github.io/service-monitor" | ||
Source = "https://github.com/thevickypedia/service-monitor" | ||
"Bug Tracker" = "https://github.com/thevickypedia/service-monitor/issues" | ||
"Release Notes" = "https://github.com/thevickypedia/service-monitor/blob/main/release_notes.rst" |