Skip to content

Commit 4885a89

Browse files
committed
Added list and creation resource CLI commands
1 parent b24c7f9 commit 4885a89

2 files changed

Lines changed: 106 additions & 1 deletion

File tree

brood/resources/cli.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
"""
2+
Brood resources CLI
3+
"""
4+
import argparse
5+
import json
6+
7+
from .models import Resource
8+
from ..external import SessionLocal
9+
10+
11+
def resources_list_handler(args: argparse.Namespace) -> None:
12+
session = SessionLocal()
13+
try:
14+
query = session.query(Resource)
15+
if args.application is not None:
16+
query = query.filter(Resource.application_id == args.application)
17+
resources = query.all()
18+
19+
print(
20+
json.dumps(
21+
{
22+
"resources": [
23+
{
24+
"id": str(resource.id),
25+
"application_id": str(resource.application_id),
26+
"resource_data": resource.resource_data,
27+
"created_at": str(resource.created_at),
28+
"updated_at": str(resource.updated_at),
29+
}
30+
for resource in resources
31+
]
32+
}
33+
)
34+
)
35+
finally:
36+
session.close()
37+
38+
39+
def resources_create_handler(args: argparse.Namespace) -> None:
40+
session = SessionLocal()
41+
try:
42+
resource = Resource(
43+
application_id=args.application, resource_data=json.loads(args.data),
44+
)
45+
session.add(resource)
46+
session.commit()
47+
48+
print(
49+
json.dumps(
50+
{
51+
"id": str(resource.id),
52+
"application_id": str(resource.application_id),
53+
"resource_data": resource.resource_data,
54+
"created_at": str(resource.created_at),
55+
"updated_at": str(resource.updated_at),
56+
}
57+
)
58+
)
59+
finally:
60+
session.close()
61+
62+
63+
def main() -> None:
64+
parser = argparse.ArgumentParser(description="Brood resources CLI")
65+
parser.set_defaults(func=lambda _: parser.print_help())
66+
subcommands = parser.add_subparsers(description="Brood resources commands")
67+
68+
parser_resources = subcommands.add_parser(
69+
"resources", description="Brood resources"
70+
)
71+
parser_resources.set_defaults(func=lambda _: parser_resources.print_help())
72+
subcommands_resources = parser_resources.add_subparsers(
73+
description="Brood user commands"
74+
)
75+
76+
# Resources command parser
77+
parser_resources_list = subcommands_resources.add_parser(
78+
"list", description="List Brood resources"
79+
)
80+
parser_resources_list.add_argument(
81+
"-a", "--application", help="Application ID filter",
82+
)
83+
parser_resources_list.set_defaults(func=resources_list_handler)
84+
parser_resources_create = subcommands_resources.add_parser(
85+
"create", description="Create Brood resources"
86+
)
87+
parser_resources_create.add_argument(
88+
"-a", "--application", help="Application ID filter",
89+
)
90+
parser_resources_create.add_argument(
91+
"-d", "--data", help="Resource data, as ex: '{\"age\": 23}'",
92+
)
93+
parser_resources_create.set_defaults(func=resources_create_handler)
94+
95+
args = parser.parse_args()
96+
args.func(args)
97+
98+
99+
if __name__ == "__main__":
100+
main()

setup.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,10 @@
3333
"Programming Language :: Python",
3434
],
3535
url="https://github.com/simiotics/brood",
36-
entry_points={"console_scripts": ["brood=brood.cli:main"]},
36+
entry_points={
37+
"console_scripts": [
38+
"brood=brood.cli:main",
39+
"resources=brood.resources.cli:main",
40+
]
41+
},
3742
)

0 commit comments

Comments
 (0)