2022-09-22 20:40:44 +00:00
|
|
|
#! /usr/bin/env python3
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import configparser
|
2022-09-23 23:19:25 +00:00
|
|
|
from typing import Any
|
2022-09-22 20:40:44 +00:00
|
|
|
import requests
|
2022-09-23 23:19:25 +00:00
|
|
|
import dataclasses
|
2022-09-22 20:40:44 +00:00
|
|
|
|
|
|
|
|
2022-09-23 23:19:25 +00:00
|
|
|
def dprint(*args: object, **kwargs: Any) -> None:
|
|
|
|
if CONFIG.verbosity > 0:
|
|
|
|
print(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
@dataclasses.dataclass
|
|
|
|
class Config:
|
|
|
|
domain: str
|
|
|
|
api_key: str
|
|
|
|
verbosity: int = 0
|
|
|
|
|
|
|
|
|
|
|
|
CONFIG = Config("", "", False)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_config() -> Config:
|
2022-09-22 20:40:44 +00:00
|
|
|
config = configparser.ConfigParser()
|
2022-09-23 23:19:25 +00:00
|
|
|
config.read("cli.ini") # TODO: think about what to do to get this
|
2022-09-22 20:40:44 +00:00
|
|
|
|
|
|
|
if "frogress" not in config.sections():
|
|
|
|
raise Exception("Missing [frogress] section in cli.ini")
|
|
|
|
|
|
|
|
if "domain" not in config["frogress"]:
|
|
|
|
raise Exception("Missing domain in cli.ini")
|
|
|
|
|
|
|
|
if "api_key" not in config["frogress"]:
|
|
|
|
raise Exception("Missing api_key in cli.ini")
|
|
|
|
|
2022-09-23 23:19:25 +00:00
|
|
|
out = Config(
|
|
|
|
domain=str(config["frogress"]["domain"]),
|
|
|
|
api_key=str(config["frogress"]["api_key"]),
|
|
|
|
verbosity=int(config.get("frogress", "verbosity", fallback=0)),
|
|
|
|
)
|
2022-09-22 20:40:44 +00:00
|
|
|
|
2022-09-23 23:19:25 +00:00
|
|
|
return out
|
2022-09-22 20:40:44 +00:00
|
|
|
|
|
|
|
|
2022-09-23 23:19:25 +00:00
|
|
|
def process_response(response: requests.Response) -> None:
|
|
|
|
if not response.ok:
|
|
|
|
print(f"error: HTTP status code {response.status_code}")
|
|
|
|
print(response.text)
|
|
|
|
if not response.ok:
|
|
|
|
exit(1)
|
2022-09-22 20:40:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def create_version(args: argparse.Namespace) -> None:
|
2022-09-23 23:19:25 +00:00
|
|
|
url = f"{CONFIG.domain}/projects/{args.project}/{args.slug}/"
|
2022-09-22 20:40:44 +00:00
|
|
|
|
|
|
|
name = args.name or args.slug
|
|
|
|
|
|
|
|
data = {
|
2022-09-23 23:19:25 +00:00
|
|
|
"api_key": CONFIG.api_key,
|
2022-09-22 20:40:44 +00:00
|
|
|
"name": name,
|
|
|
|
}
|
|
|
|
|
2022-09-23 23:19:25 +00:00
|
|
|
dprint("POST " + url)
|
2022-09-22 20:40:44 +00:00
|
|
|
|
2022-09-23 23:19:25 +00:00
|
|
|
if args.dryrun:
|
|
|
|
print(data)
|
|
|
|
return
|
2022-09-22 20:40:44 +00:00
|
|
|
response = requests.post(url, json=data)
|
2022-09-23 23:19:25 +00:00
|
|
|
process_response(response)
|
2022-09-22 20:40:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def delete_version(args: argparse.Namespace) -> None:
|
2022-09-23 23:19:25 +00:00
|
|
|
url = f"{CONFIG.domain}/projects/{args.project}/{args.slug}/"
|
2022-09-22 20:40:44 +00:00
|
|
|
|
2022-09-23 23:19:25 +00:00
|
|
|
data = {"api_key": CONFIG.api_key}
|
2022-09-22 20:40:44 +00:00
|
|
|
|
2022-09-23 23:19:25 +00:00
|
|
|
dprint("DELETE " + url)
|
|
|
|
|
|
|
|
if args.dryrun:
|
|
|
|
print(data)
|
|
|
|
return
|
2022-09-22 20:40:44 +00:00
|
|
|
|
|
|
|
response = requests.delete(url, json=data)
|
2022-09-23 23:19:25 +00:00
|
|
|
process_response(response)
|
2022-09-22 20:40:44 +00:00
|
|
|
|
|
|
|
|
2022-09-23 19:20:56 +00:00
|
|
|
def main() -> None:
|
2022-09-23 23:19:25 +00:00
|
|
|
parser = argparse.ArgumentParser(add_help=False)
|
|
|
|
parser.add_argument(
|
|
|
|
"--dryrun",
|
|
|
|
help="Show request but do not actually send it",
|
|
|
|
action="store_true",
|
|
|
|
)
|
2022-09-23 19:20:56 +00:00
|
|
|
|
|
|
|
subparsers = parser.add_subparsers(help="the action to perform", required=True)
|
|
|
|
|
|
|
|
# Create
|
|
|
|
create_parser = subparsers.add_parser("create", help="create a new db object")
|
|
|
|
create_subparsers = create_parser.add_subparsers(
|
|
|
|
help="the db layer on which to operate", required=True
|
|
|
|
)
|
|
|
|
create_version_parser = create_subparsers.add_parser(
|
|
|
|
"version",
|
|
|
|
help="create a new version",
|
|
|
|
)
|
|
|
|
create_version_parser.add_argument(
|
|
|
|
"project", help="the project for which to create the version"
|
|
|
|
)
|
|
|
|
create_version_parser.add_argument("slug", help="the slug for the version")
|
|
|
|
create_version_parser.add_argument("--name", help="the name for the version")
|
|
|
|
create_version_parser.set_defaults(func=create_version)
|
|
|
|
|
|
|
|
# Delete
|
|
|
|
delete_parser = subparsers.add_parser("delete", help="delete a db object")
|
|
|
|
delete_subparsers = delete_parser.add_subparsers(
|
|
|
|
help="the db layer on which to operate", required=True
|
|
|
|
)
|
|
|
|
delete_version_parser = delete_subparsers.add_parser(
|
|
|
|
"version",
|
|
|
|
help="delete a version",
|
|
|
|
)
|
|
|
|
delete_version_parser.add_argument(
|
|
|
|
"project", help="the project for which to delete the version"
|
|
|
|
)
|
|
|
|
delete_version_parser.add_argument("slug", help="the slug for the version")
|
|
|
|
delete_version_parser.set_defaults(func=delete_version)
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2022-09-23 23:19:25 +00:00
|
|
|
global CONFIG
|
|
|
|
CONFIG = parse_config()
|
2022-09-23 19:20:56 +00:00
|
|
|
|
2022-09-23 23:19:25 +00:00
|
|
|
args.func(args)
|
2022-09-22 20:40:44 +00:00
|
|
|
|
|
|
|
|
2022-09-23 19:20:56 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|