add(app): create python application
This commit is contained in:
36
cli/commands/export.py
Normal file
36
cli/commands/export.py
Normal file
@@ -0,0 +1,36 @@
|
||||
import click
|
||||
import requests
|
||||
|
||||
BASE_URL = "http://localhost:8000/export" # Backend URL for exporting data
|
||||
|
||||
@click.command()
|
||||
@click.option(
|
||||
"--format",
|
||||
type=click.Choice(["yaml", "json"], case_sensitive=False),
|
||||
default="json",
|
||||
help="Specify the export format: yaml or json. Default is json.",
|
||||
)
|
||||
@click.argument("output", required=False, type=click.Path(writable=True))
|
||||
def export_data(format, output):
|
||||
"""
|
||||
Export Metal Check data in the specified format (yaml or json).
|
||||
|
||||
If an OUTPUT file is provided, the data will be saved to the file.
|
||||
Otherwise, it will be printed to the console.
|
||||
"""
|
||||
try:
|
||||
response = requests.get(f"{BASE_URL}?format={format}")
|
||||
if response.status_code == 200:
|
||||
data = response.text
|
||||
if output:
|
||||
with open(output, "w") as file:
|
||||
file.write(data)
|
||||
click.echo(f"Data successfully exported to {output}.")
|
||||
else:
|
||||
click.echo("\n📤 Exported Data:")
|
||||
click.echo(data)
|
||||
else:
|
||||
click.echo(f"Error: {response.status_code} - {response.text}")
|
||||
except requests.RequestException as e:
|
||||
click.echo(f"Error: {e}")
|
||||
|
||||
Reference in New Issue
Block a user