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