mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-04-25 08:26:21 +02:00
More config cli (#466)
* Extra config CLI tech spec * Describe packaging * Added CLI commands * Add tests
This commit is contained in:
parent
5e71d0cadb
commit
28190fea8a
14 changed files with 1361 additions and 0 deletions
61
trustgraph-cli/trustgraph/cli/delete_config_item.py
Normal file
61
trustgraph-cli/trustgraph/cli/delete_config_item.py
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
"""
|
||||
Deletes a configuration item
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import os
|
||||
from trustgraph.api import Api
|
||||
from trustgraph.api.types import ConfigKey
|
||||
|
||||
default_url = os.getenv("TRUSTGRAPH_URL", 'http://localhost:8088/')
|
||||
|
||||
def delete_config_item(url, config_type, key):
|
||||
|
||||
api = Api(url).config()
|
||||
|
||||
config_key = ConfigKey(type=config_type, key=key)
|
||||
api.delete([config_key])
|
||||
|
||||
print(f"Configuration item deleted: {config_type}/{key}")
|
||||
|
||||
def main():
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
prog='tg-delete-config-item',
|
||||
description=__doc__,
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--type',
|
||||
required=True,
|
||||
help='Configuration type',
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--key',
|
||||
required=True,
|
||||
help='Configuration key',
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'-u', '--api-url',
|
||||
default=default_url,
|
||||
help=f'API URL (default: {default_url})',
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
|
||||
delete_config_item(
|
||||
url=args.api_url,
|
||||
config_type=args.type,
|
||||
key=args.key,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
|
||||
print("Exception:", e, flush=True)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
78
trustgraph-cli/trustgraph/cli/get_config_item.py
Normal file
78
trustgraph-cli/trustgraph/cli/get_config_item.py
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
"""
|
||||
Gets a specific configuration item
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import os
|
||||
import json
|
||||
from trustgraph.api import Api
|
||||
from trustgraph.api.types import ConfigKey
|
||||
|
||||
default_url = os.getenv("TRUSTGRAPH_URL", 'http://localhost:8088/')
|
||||
|
||||
def get_config_item(url, config_type, key, format_type):
|
||||
|
||||
api = Api(url).config()
|
||||
|
||||
config_key = ConfigKey(type=config_type, key=key)
|
||||
values = api.get([config_key])
|
||||
|
||||
if not values:
|
||||
raise Exception(f"Configuration item not found: {config_type}/{key}")
|
||||
|
||||
value = values[0].value
|
||||
|
||||
if format_type == "json":
|
||||
print(json.dumps(value))
|
||||
else:
|
||||
print(value)
|
||||
|
||||
def main():
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
prog='tg-get-config-item',
|
||||
description=__doc__,
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--type',
|
||||
required=True,
|
||||
help='Configuration type',
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--key',
|
||||
required=True,
|
||||
help='Configuration key',
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--format',
|
||||
choices=['text', 'json'],
|
||||
default='text',
|
||||
help='Output format (default: text)',
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'-u', '--api-url',
|
||||
default=default_url,
|
||||
help=f'API URL (default: {default_url})',
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
|
||||
get_config_item(
|
||||
url=args.api_url,
|
||||
config_type=args.type,
|
||||
key=args.key,
|
||||
format_type=args.format,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
|
||||
print("Exception:", e, flush=True)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
65
trustgraph-cli/trustgraph/cli/list_config_items.py
Normal file
65
trustgraph-cli/trustgraph/cli/list_config_items.py
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
"""
|
||||
Lists configuration items for a specified type
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import os
|
||||
import json
|
||||
from trustgraph.api import Api
|
||||
|
||||
default_url = os.getenv("TRUSTGRAPH_URL", 'http://localhost:8088/')
|
||||
|
||||
def list_config_items(url, config_type, format_type):
|
||||
|
||||
api = Api(url).config()
|
||||
|
||||
keys = api.list(config_type)
|
||||
|
||||
if format_type == "json":
|
||||
print(json.dumps(keys))
|
||||
else:
|
||||
for key in keys:
|
||||
print(key)
|
||||
|
||||
def main():
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
prog='tg-list-config-items',
|
||||
description=__doc__,
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--type',
|
||||
required=True,
|
||||
help='Configuration type to list',
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--format',
|
||||
choices=['text', 'json'],
|
||||
default='text',
|
||||
help='Output format (default: text)',
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'-u', '--api-url',
|
||||
default=default_url,
|
||||
help=f'API URL (default: {default_url})',
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
|
||||
list_config_items(
|
||||
url=args.api_url,
|
||||
config_type=args.type,
|
||||
format_type=args.format,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
|
||||
print("Exception:", e, flush=True)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
80
trustgraph-cli/trustgraph/cli/put_config_item.py
Normal file
80
trustgraph-cli/trustgraph/cli/put_config_item.py
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
"""
|
||||
Sets a configuration item
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import os
|
||||
import sys
|
||||
from trustgraph.api import Api
|
||||
from trustgraph.api.types import ConfigValue
|
||||
|
||||
default_url = os.getenv("TRUSTGRAPH_URL", 'http://localhost:8088/')
|
||||
|
||||
def put_config_item(url, config_type, key, value):
|
||||
|
||||
api = Api(url).config()
|
||||
|
||||
config_value = ConfigValue(type=config_type, key=key, value=value)
|
||||
api.put([config_value])
|
||||
|
||||
print(f"Configuration item set: {config_type}/{key}")
|
||||
|
||||
def main():
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
prog='tg-put-config-item',
|
||||
description=__doc__,
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--type',
|
||||
required=True,
|
||||
help='Configuration type',
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--key',
|
||||
required=True,
|
||||
help='Configuration key',
|
||||
)
|
||||
|
||||
value_group = parser.add_mutually_exclusive_group(required=True)
|
||||
value_group.add_argument(
|
||||
'--value',
|
||||
help='Configuration value',
|
||||
)
|
||||
|
||||
value_group.add_argument(
|
||||
'--stdin',
|
||||
action='store_true',
|
||||
help='Read configuration value from standard input',
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'-u', '--api-url',
|
||||
default=default_url,
|
||||
help=f'API URL (default: {default_url})',
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
|
||||
if args.stdin:
|
||||
value = sys.stdin.read()
|
||||
else:
|
||||
value = args.value
|
||||
|
||||
put_config_item(
|
||||
url=args.api_url,
|
||||
config_type=args.type,
|
||||
key=args.key,
|
||||
value=value,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
|
||||
print("Exception:", e, flush=True)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue