Idea for combining functions

This commit is contained in:
Elliptic Ellipsis 2022-09-24 23:10:18 +01:00
parent f352000e1e
commit 7f1e3d6737
1 changed files with 71 additions and 19 deletions

90
cli.py
View File

@ -2,7 +2,7 @@
import argparse import argparse
import configparser import configparser
from typing import Any from typing import Any, Union
import requests import requests
import dataclasses import dataclasses
@ -52,12 +52,9 @@ def process_response(response: requests.Response) -> None:
exit(1) exit(1)
def create_version(args: argparse.Namespace) -> None: def creation(url: str, args: argparse.Namespace) -> Union[requests.Response, None]:
url = f"{CONFIG.domain}/projects/{args.project}/{args.slug}/"
name = args.name or args.slug name = args.name or args.slug
json = {
data = {
"api_key": CONFIG.api_key, "api_key": CONFIG.api_key,
"name": name, "name": name,
} }
@ -65,25 +62,49 @@ def create_version(args: argparse.Namespace) -> None:
dprint("POST " + url) dprint("POST " + url)
if args.dryrun: if args.dryrun:
print(data) print(json)
return return None
response = requests.post(url, json=data) else:
process_response(response) return requests.post(url, json=json)
def create_version(args: argparse.Namespace) -> None:
url = f"{CONFIG.domain}/projects/{args.project}/{args.slug}/"
response = creation(url, args)
if response:
process_response(response)
def create_category(args: argparse.Namespace) -> None:
url = f"{CONFIG.domain}/projects/{args.project}/{args.version}/{args.slug}/"
response = creation(url, args)
if response:
process_response(response)
def deletion(url: str, args: argparse.Namespace) -> Union[requests.Response, None]:
json = {"api_key": CONFIG.api_key}
dprint("DELETE " + url)
if args.dryrun:
print(json)
return None
else:
return requests.delete(url, json=json)
def delete_version(args: argparse.Namespace) -> None: def delete_version(args: argparse.Namespace) -> None:
url = f"{CONFIG.domain}/projects/{args.project}/{args.slug}/" url = f"{CONFIG.domain}/projects/{args.project}/{args.slug}/"
response = deletion(url, args)
if response:
process_response(response)
data = {"api_key": CONFIG.api_key}
dprint("DELETE " + url) def delete_category(args: argparse.Namespace) -> None:
url = f"{CONFIG.domain}/projects/{args.project}/{args.version}/{args.slug}/"
if args.dryrun: response = deletion(url, args)
print(data) if response:
return process_response(response)
response = requests.delete(url, json=data)
process_response(response)
def main() -> None: def main() -> None:
@ -101,6 +122,7 @@ def main() -> None:
create_subparsers = create_parser.add_subparsers( create_subparsers = create_parser.add_subparsers(
help="the db layer on which to operate", required=True help="the db layer on which to operate", required=True
) )
# Version
create_version_parser = create_subparsers.add_parser( create_version_parser = create_subparsers.add_parser(
"version", "version",
help="create a new version", help="create a new version",
@ -111,12 +133,27 @@ def main() -> None:
create_version_parser.add_argument("slug", help="the slug for 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.add_argument("--name", help="the name for the version")
create_version_parser.set_defaults(func=create_version) create_version_parser.set_defaults(func=create_version)
# Category
create_category_parser = create_subparsers.add_parser(
"category",
help="create a new category",
)
create_category_parser.add_argument(
"project", help="the project for which to create the category"
)
create_category_parser.add_argument(
"version", help="the version for which to create the category"
)
create_category_parser.add_argument("slug", help="the slug for the category")
create_category_parser.add_argument("--name", help="the name for the category")
create_category_parser.set_defaults(func=create_category)
# Delete # Delete
delete_parser = subparsers.add_parser("delete", help="delete a db object") delete_parser = subparsers.add_parser("delete", help="delete a db object")
delete_subparsers = delete_parser.add_subparsers( delete_subparsers = delete_parser.add_subparsers(
help="the db layer on which to operate", required=True help="the db layer on which to operate", required=True
) )
# Version
delete_version_parser = delete_subparsers.add_parser( delete_version_parser = delete_subparsers.add_parser(
"version", "version",
help="delete a version", help="delete a version",
@ -126,9 +163,24 @@ def main() -> None:
) )
delete_version_parser.add_argument("slug", help="the slug for the version") delete_version_parser.add_argument("slug", help="the slug for the version")
delete_version_parser.set_defaults(func=delete_version) delete_version_parser.set_defaults(func=delete_version)
# Category
delete_category_parser = delete_subparsers.add_parser(
"category",
help="delete a category",
)
delete_category_parser.add_argument(
"project", help="the project for which to delete the category"
)
delete_category_parser.add_argument(
"version", help="the version for which to delete the category"
)
delete_category_parser.add_argument("slug", help="the slug for the category")
delete_category_parser.set_defaults(func=delete_category)
args = parser.parse_args() args = parser.parse_args()
print(args)
global CONFIG global CONFIG
CONFIG = parse_config() CONFIG = parse_config()