#!/usr/bin/python3
import sys
import objectio
import objectio.io
import typer
import yaml

app = typer.Typer()

@app.command()
def cat(url, timeout:float=3600):
    """Cat the given object to stdout.

    The object is opened using the configured cloud command
    and the output is piped directly to standard out.
    """
    pipe = objectio.objopen(url, verb="read", stream=sys.stdout.buffer)
    if pipe: pipe.wait(timeout)

@app.command()
def put(url, timeout:float=3600):
    """Upload stdin to the given location.

    The input is uploaded to the server at the given location.
    Most (but not all) object stores will only update the store
    after a successful update.
    """
    pipe = objectio.objopen(url, verb="write", stream=sys.stdin.buffer)
    if pipe: pipe.wait(timeout)

@app.command()
def list(url, timeout:float=3600):
    """List the objects in the given bucket or at the given path.

    The output is a list of absolute urls that are usable
    with `cat.
    """
    pipe = objectio.objopen(url, verb="list", stream=sys.stdout)
    if pipe: pipe.wait(timeout)

@app.command()
def auth(url, timeout:float=3600):
    """Authenticate to the server for the URL.

    This either runs an interactive command to permit the user
    to authenticate, or prints an explanation of how to authenticate.
    """
    pipe = objectio.objopen(url, verb="auth", stream=sys.stdout)
    if pipe: pipe.wait(timeout)

@app.command()
def buckets(url, timeout:float=3600):
    """List all available buckets for the currently authenticated user.

    Provides a list of toplevel buckets. On file systems, provides a list
    of volumes.
    """
    pipe = objectio.objopen(url, verb="buckets", stream=sys.stdout)
    if pipe: pipe.wait(timeout)

@app.command()
def handler(url:str, verb:str="read"):
    """Output the handlers for a given URL and verb.

    Looks in the configuration file for a handler for the combination
    of the URL and verb and outputs it in YAML format. This is
    mainly intended for debugging configuration file issues.
    """
    handler = objectio.io.get_handler_for(url, verb)
    yaml.dump(handler, sys.stdout, default_flow_style=None)

@app.command()
def config():
    """Output the entire config file."""
    yaml.dump(objectio.io.config, sys.stdout, default_flow_style=None)

if __name__ == "__main__":
    app()
