add -q parameter to query

This commit is contained in:
chris 2021-07-19 12:38:44 +02:00
parent d0c7622dc6
commit c86f25f562

View file

@ -9,7 +9,7 @@ from tabulate import tabulate
from prompt_toolkit import print_formatted_text as print
from prompt_toolkit import HTML
from prompt_toolkit import prompt
from prompt_toolkit.completion import ThreadedCompleter, FuzzyWordCompleter
from prompt_toolkit.completion import FuzzyWordCompleter
from prompt_toolkit.lexers import PygmentsLexer
from prompt_toolkit.styles import style_from_pygments_cls
from pygments.lexers.promql import PromQLLexer
@ -77,15 +77,18 @@ def cli(ctx, user, password, promhost, use_netrc):
@cli.command()
@click.option("-q", "--query")
@click.pass_context
def query(ctx):
def query(ctx, query):
promclio = ctx.obj['promcli']
if not query:
query = prompt(
"query: ",
completer=FuzzyWordCompleter(promclio.get_series()),
lexer=PygmentsLexer(PromQLLexer),
style=style_from_pygments_cls(SolarizedLightStyle),
)
if '{' not in query:
params = prompt(
"{ ",
completer=FuzzyWordCompleter(promclio.get_labels(query)),
@ -93,8 +96,9 @@ def query(ctx):
lexer=PygmentsLexer(PromQLLexer),
style=style_from_pygments_cls(SolarizedLightStyle),
)
print(HTML(f"Querying <lightblue>{query}{{{params}}}</lightblue>"))
print(promclio.format_query(promclio.get_query(f"{query}{{{params}}}")))
query = f"{query}{{{params}}}"
print(HTML(f"Querying <lightblue>{query}</lightblue>"))
print(promclio.format_query(promclio.get_query(f"{query}")))
@cli.command()