refactor: use argparse subparser
This commit is contained in:
+1
-1
@@ -26,4 +26,4 @@ RUN uv sync --no-dev
|
||||
COPY main.py ./
|
||||
COPY src ./src/
|
||||
|
||||
ENTRYPOINT [ "uv", "run", "--no-sync", "main.py" ]
|
||||
ENTRYPOINT [ "uv", "run", "--no-sync", "main.py"]
|
||||
@@ -62,7 +62,7 @@ docker-build: ## docker build
|
||||
|
||||
.PHONY: docker-run
|
||||
docker-run: docker-build ## docker run
|
||||
@$(DOCKER) run -it -p $(PORT):8080 -v ./data:/data $(DOCKER_TAG) --token $(TOKEN) --debug
|
||||
@$(DOCKER) run -it -p $(PORT):8080 -v ./data:/data $(DOCKER_TAG) --debug --no-certbot run --token $(TOKEN)
|
||||
|
||||
# ACTIONS
|
||||
|
||||
|
||||
@@ -1,20 +1,27 @@
|
||||
# Stapler
|
||||
|
||||
```txt
|
||||
usage: stapler [-h] [-p PORT] [--host HOST] [-d DATA_DIR] -t TOKEN [--max-size-bytes MAX_SIZE] [-b BIND] [--certbot-conf CERTBOT_CONF] [--certbot-www CERTBOT_WWW]
|
||||
usage: stapler [-h] [--debug | --no-debug] [-d DATA_DIR] [--certificates | --no-certificates] [--certbot | --no-certbot] [--self-signed-path SELF_SIGNED_PATH] [--certbot-conf CERTBOT_CONF]
|
||||
[--certbot-www CERTBOT_WWW]
|
||||
COMMAND ...
|
||||
|
||||
Static pages as simple as a gzip file
|
||||
|
||||
positional arguments:
|
||||
COMMAND
|
||||
run Run Stapler server
|
||||
|
||||
options:
|
||||
-h, --help show this help message and exit
|
||||
-p, --port PORT server port (default: 8080)
|
||||
--host HOST server default host (default: localhost)
|
||||
--debug, --no-debug
|
||||
-d, --data-dir DATA_DIR
|
||||
directory where pages are/will be stored (default: ./data)
|
||||
-t, --token TOKEN secret token for update requests
|
||||
--max-size-bytes MAX_SIZE
|
||||
max size of accepted archives (in bytes) (default: 2000000)
|
||||
-b, --bind BIND server bind address (default: 0.0.0.0)
|
||||
--certificates, --no-certificates
|
||||
Handle certificates (default: true)
|
||||
--certbot, --no-certbot
|
||||
Use Certbot (default: true)
|
||||
--self-signed-path SELF_SIGNED_PATH
|
||||
Self-signed certificates dir (default: ./data/.certificates)
|
||||
--certbot-conf CERTBOT_CONF
|
||||
Certbot config dir (default: /etc/letsencrypt)
|
||||
--certbot-www CERTBOT_WWW
|
||||
@@ -23,6 +30,24 @@ options:
|
||||
(Each option can be supplied with equivalent environment variable.)
|
||||
```
|
||||
|
||||
```txt
|
||||
usage: stapler run [-h] [--host HOST] [-p PORT] [--https | --no-https] -t TOKEN [--max-size-bytes MAX_SIZE] [-b BIND]
|
||||
|
||||
Run Stapler server
|
||||
|
||||
options:
|
||||
-h, --help show this help message and exit
|
||||
--host HOST server default host (default: localhost:8080)
|
||||
-p, --port PORT server port (default: 8080)
|
||||
--https, --no-https Use https (implies --certificates) (default: true)
|
||||
-t, --token TOKEN secret token for update requests
|
||||
--max-size-bytes MAX_SIZE
|
||||
max size of accepted archives (in bytes) (default: 2000000)
|
||||
-b, --bind BIND server bind address (default: 0.0.0.0)
|
||||
|
||||
(Each option can be supplied with equivalent environment variable.)
|
||||
```
|
||||
|
||||
## Endpoints
|
||||
|
||||
### Create/update page
|
||||
|
||||
+65
-53
@@ -4,6 +4,8 @@ import os
|
||||
|
||||
from . import project
|
||||
|
||||
__EPILOG = "(Each option can be supplied with equivalent environment variable.)"
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Parameters:
|
||||
@@ -19,6 +21,7 @@ class Parameters:
|
||||
with_certbot: bool
|
||||
with_certificates: bool
|
||||
https: bool
|
||||
command: str
|
||||
debug: bool
|
||||
|
||||
@classmethod
|
||||
@@ -89,23 +92,11 @@ def parse_parameters() -> Parameters:
|
||||
parser = argparse.ArgumentParser(
|
||||
project.get_name(),
|
||||
description=project.get_description(),
|
||||
epilog="(Each option can be supplied with equivalent environment variable.)",
|
||||
)
|
||||
__add_arg_int(
|
||||
parser,
|
||||
"-p",
|
||||
"--port",
|
||||
env_var="PORT",
|
||||
default=8080,
|
||||
help_txt="server port",
|
||||
)
|
||||
__add_arg_str(
|
||||
parser,
|
||||
"--host",
|
||||
env_var="HOST",
|
||||
default="localhost:8080",
|
||||
help_txt="server default host",
|
||||
epilog=__EPILOG,
|
||||
suggest_on_error=True,
|
||||
)
|
||||
subparsers = parser.add_subparsers(dest="command", required=True, metavar="COMMAND")
|
||||
parser.add_argument("--debug", action=argparse.BooleanOptionalAction)
|
||||
__add_arg_str(
|
||||
parser,
|
||||
"-d",
|
||||
@@ -114,27 +105,26 @@ def parse_parameters() -> Parameters:
|
||||
default="./data",
|
||||
help_txt="directory where pages are/will be stored",
|
||||
)
|
||||
__add_arg_str_required(
|
||||
parser,
|
||||
"-t",
|
||||
"--token",
|
||||
env_var="TOKEN",
|
||||
help_txt="secret token for update requests",
|
||||
parser.add_argument(
|
||||
"--certificates",
|
||||
action=argparse.BooleanOptionalAction,
|
||||
help="Handle certificates (default: true)",
|
||||
default=True,
|
||||
dest="with_certificates",
|
||||
)
|
||||
__add_arg_int(
|
||||
parser,
|
||||
"--max-size-bytes",
|
||||
env_var="MAX_SIZE",
|
||||
default=2_000_000,
|
||||
help_txt="max size of accepted archives (in bytes)",
|
||||
parser.add_argument(
|
||||
"--certbot",
|
||||
action=argparse.BooleanOptionalAction,
|
||||
help="Use Certbot (default: true)",
|
||||
default=True,
|
||||
dest="with_certbot",
|
||||
)
|
||||
__add_arg_str(
|
||||
parser,
|
||||
"-b",
|
||||
"--bind",
|
||||
env_var="BIND",
|
||||
default="0.0.0.0",
|
||||
help_txt="server bind address",
|
||||
"--self-signed-path",
|
||||
env_var="SELF_SIGNED_PATH",
|
||||
default="./data/.certificates",
|
||||
help_txt="Self-signed certificates dir",
|
||||
)
|
||||
__add_arg_str(
|
||||
parser,
|
||||
@@ -150,34 +140,56 @@ def parse_parameters() -> Parameters:
|
||||
default="./data/.certbot",
|
||||
help_txt="Certbot www dir",
|
||||
)
|
||||
|
||||
run_parser = subparsers.add_parser(
|
||||
"run",
|
||||
help="Run Stapler server",
|
||||
description="Run Stapler server",
|
||||
epilog=__EPILOG,
|
||||
)
|
||||
__add_arg_str(
|
||||
parser,
|
||||
"--self-signed-path",
|
||||
env_var="SELF_SIGNED_PATH",
|
||||
default="./data/.certificates",
|
||||
help_txt="Self-signed certificates dir",
|
||||
run_parser,
|
||||
"--host",
|
||||
env_var="HOST",
|
||||
default="localhost:8080",
|
||||
help_txt="server default host",
|
||||
)
|
||||
parser.add_argument("--debug", action=argparse.BooleanOptionalAction)
|
||||
parser.add_argument(
|
||||
"--certbot",
|
||||
action=argparse.BooleanOptionalAction,
|
||||
help="Use Certbot (default: false)",
|
||||
default=False,
|
||||
dest="with_certbot",
|
||||
__add_arg_int(
|
||||
run_parser,
|
||||
"-p",
|
||||
"--port",
|
||||
env_var="PORT",
|
||||
default=8080,
|
||||
help_txt="server port",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--certificates",
|
||||
action=argparse.BooleanOptionalAction,
|
||||
help="Handle certificates (default: true)",
|
||||
default=True,
|
||||
dest="with_certificates",
|
||||
)
|
||||
parser.add_argument(
|
||||
run_parser.add_argument(
|
||||
"--https",
|
||||
action=argparse.BooleanOptionalAction,
|
||||
help="Use https (implies --certificates) (default: true)",
|
||||
default=True,
|
||||
)
|
||||
__add_arg_str_required(
|
||||
run_parser,
|
||||
"-t",
|
||||
"--token",
|
||||
env_var="TOKEN",
|
||||
help_txt="secret token for update requests",
|
||||
)
|
||||
__add_arg_int(
|
||||
run_parser,
|
||||
"--max-size-bytes",
|
||||
env_var="MAX_SIZE",
|
||||
default=2_000_000,
|
||||
help_txt="max size of accepted archives (in bytes)",
|
||||
)
|
||||
__add_arg_str(
|
||||
run_parser,
|
||||
"-b",
|
||||
"--bind",
|
||||
env_var="BIND",
|
||||
default="0.0.0.0",
|
||||
help_txt="server bind address",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
if args.https:
|
||||
args.with_certificates = True
|
||||
|
||||
Reference in New Issue
Block a user