Files
metalcheck-cli/cli/commands/export.py
2024-12-11 12:14:25 +00:00

37 lines
1.2 KiB
Python

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}")