Files
metalcheck-cli/cli/commands/export.py
2024-12-11 12:56:18 +00:00

39 lines
1.2 KiB
Python

import click
import requests
@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))
@click.pass_context
def export_data(ctx, 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.
"""
base_url = ctx.obj["BASE_URL"]
export_url = f"{base_url}/export"
try:
response = requests.get(f"{export_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}")