-
Notifications
You must be signed in to change notification settings - Fork 4
/
cli.py
executable file
·162 lines (116 loc) · 4.46 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#! /usr/bin/env python3
import argparse
import configparser
import requests
def parse_config() -> configparser.SectionProxy:
config = configparser.ConfigParser()
config.read("cli.ini")
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")
if "debug" not in config["frogress"]:
config["frogress"]["debug"] = "false"
return config["frogress"]
def debug(msg: str) -> None:
if dbg:
print(msg)
def create_version(args: argparse.Namespace) -> None:
url = f"{domain}/projects/{args.project}/{args.slug}/"
name = args.name or args.slug
data = {
"api_key": api_key,
"name": name,
}
debug("POST " + url)
response = requests.post(url, json=data)
print(response.text)
def delete_version(args: argparse.Namespace) -> None:
url = f"{domain}/projects/{args.project}/{args.slug}/"
data = {"api_key": api_key}
debug("DELETE " + url)
response = requests.delete(url, json=data)
print(response.text)
def create_category(args: argparse.Namespace) -> None:
url = f"{domain}/projects/{args.project}/{args.version}/{args.slug}/"
name = args.name or args.slug
data = {
"api_key": api_key,
"name": name,
}
debug("POST " + url)
response = requests.post(url, json=data)
print(response.text)
def delete_category(args: argparse.Namespace) -> None:
url = f"{domain}/projects/{args.project}/{args.version}/{args.slug}/"
data = {"api_key": api_key}
debug("DELETE " + url)
response = requests.delete(url, json=data)
print(response.text)
def main() -> None:
parser = argparse.ArgumentParser()
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
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)
# Create 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 version"
)
create_category_parser.add_argument("version", help="the slug for the version")
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_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
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)
# Delete 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 version"
)
delete_category_parser.add_argument("version", help="the slug for the version")
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.func(args)
config = parse_config()
dbg = bool(config["debug"])
domain = config["domain"]
api_key = config["api_key"]
if __name__ == "__main__":
main()