-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/persist #4
Feat/persist #4
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import json | ||
import typer | ||
import os | ||
import sys | ||
from prettytable import PrettyTable | ||
|
||
from arlas.cli.settings import Configuration, Resource | ||
from arlas.cli.service import Service | ||
from arlas.cli.variables import variables | ||
|
||
persist = typer.Typer() | ||
|
||
|
||
@persist.callback() | ||
def configuration(config: str = typer.Option(help="Name of the ARLAS configuration to use from your configuration file ({}).".format(variables["configuration_file"]))): | ||
variables["arlas"] = config | ||
|
||
|
||
@persist.command(help="Add an entry, returns its ID", name="add") | ||
def add( | ||
file: str = typer.Argument(help="File path"), | ||
zone: str = typer.Argument(help="zone"), | ||
name: str = typer.Option(help="name", default="none"), | ||
reader: list[str] = typer.Option(help="Readers", default=[]), | ||
writer: list[str] = typer.Option(help="writers", default=[]), | ||
encode: bool = typer.Option(help="Encode in BASE64", default=False) | ||
): | ||
config = variables["arlas"] | ||
id = Service.persistence_add_file(config, Resource(location=file), zone=zone, name=name, readers=reader, encode=encode) | ||
print(id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better to add a more meaningful message There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually it is the id to be used for different usages. I thought that if I do not put text around, it would be easier to use in a script (e.g. to get the ID and put it in a different command line). I can add more details, but it will make the work of the developper a bit more complex to extract the id. Your opinion? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't have it in mind, I think it is a good idea to do it like you proposed, otherwise it would have to be on multiple lines to be able to grab the id from the last line maybe, but it makes things complicated. If other methods are to be integrated in scripts such as this one and they have a more complicated output, a "dev" or "short answer" mode with a flag could maybe allow for a more verbose answer as well as a direct one for scripts There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes this (short answer mode flag) is a good idea. For now, I release. |
||
|
||
|
||
@persist.command(help="Delete an entry", name="delete") | ||
def delete( | ||
id: str = typer.Argument(help="entry identifier") | ||
): | ||
config = variables["arlas"] | ||
if not Configuration.settings.arlas.get(config).allow_delete: | ||
print("Error: delete on \"{}\" is not allowed. To allow delete, change your configuration file ({}).".format(config, variables["configuration_file"]), file=sys.stderr) | ||
exit(1) | ||
|
||
if typer.confirm("You are about to delete the entry '{}' on '{}' configuration.\n".format(id, config), | ||
prompt_suffix="Do you want to continue (del {} on {})?".format(id, config), | ||
default=False, ): | ||
if config != "local" and config.find("test") < 0: | ||
if typer.prompt("WARNING: You are not on a test environment. To delete {} on {}, type the name of the configuration ({})".format(id, config, config)) != config: | ||
print("Error: delete on \"{}\" cancelled.".format(config), file=sys.stderr) | ||
exit(1) | ||
|
||
Service.persistence_delete(config, id=id) | ||
print("Resource {} deleted.".format(id)) | ||
|
||
|
||
@persist.command(help="Retrieve an entry", name="get") | ||
def get( | ||
id: str = typer.Argument(help="entry identifier") | ||
): | ||
config = variables["arlas"] | ||
print(Service.persistence_get(config, id=id).get("doc_value")) | ||
|
||
|
||
@persist.command(help="List entries within a zone", name="zone") | ||
def zone( | ||
zone: str = typer.Argument(help="Zone name") | ||
): | ||
config = variables["arlas"] | ||
table = Service.persistence_zone(config, zone=zone) | ||
tab = PrettyTable(table[0], sortby="name", align="l") | ||
tab.add_rows(table[1:]) | ||
print(tab) | ||
|
||
|
||
@persist.command(help="List groups allowed to access a zone", name="groups") | ||
def groups( | ||
zone: str = typer.Argument(help="Zone name") | ||
): | ||
config = variables["arlas"] | ||
table = Service.persistence_groups(config, zone=zone) | ||
tab = PrettyTable(table[0], sortby="group", align="l") | ||
tab.add_rows(table[1:]) | ||
print(tab) | ||
|
||
|
||
@persist.command(help="Describe an entry", name="describe") | ||
def describe( | ||
id: str = typer.Argument(help="entry identifier") | ||
): | ||
config = variables["arlas"] | ||
table = Service.persistence_describe(config, id=id) | ||
tab = PrettyTable(table[0], sortby="metadata", align="l") | ||
tab.add_rows(table[1:]) | ||
print(tab) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trailing spaces