opdb/opdb/__main__.py

52 lines
1.5 KiB
Python

# This file is part of the Open Parts Database software
# Copyright (C) 2022 Valentin Lorentz
#
# This program is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License version 3, as published by the
# Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
"""
CLI entrypoint.
"""
import sys
import typing
def error(msg: str) -> typing.NoReturn:
"""Prints the message on stderr and exits with code 1."""
print(msg, file=sys.stderr)
sys.exit(1)
def main() -> None:
"""CLI entrypoint"""
try:
(executable, subcommand, *args) = sys.argv
except ValueError:
error(f"Syntax: {sys.argv[0]} <subcommand> [<arg1> [<arg2> [...]]]")
if subcommand == "initdb":
from opdb.db import Db # pylint: disable=import-outside-toplevel
try:
(dsn,) = args
except ValueError:
error(f"Syntax: {executable} initdb <libpq DSN>")
with Db.open(dsn) as db:
db.init()
else:
error(f"Unknown subcommand: {subcommand}")
if __name__ == "__main__":
main()