80 lines
1.9 KiB
Python
80 lines
1.9 KiB
Python
import click
|
|
import os
|
|
import sys
|
|
import vultron.cmd
|
|
import vultron.display
|
|
|
|
class Help(vultron.cmd.Command):
|
|
def init(self):
|
|
self.uses_api = False
|
|
|
|
def vultron_help(self, *args):
|
|
# FIXME: print global help message
|
|
pass
|
|
|
|
class Account(vultron.cmd.Command):
|
|
"""Query for account information."""
|
|
|
|
def vultron_info(self, *args):
|
|
"""Display your account details."""
|
|
|
|
account = self.api.get("account")
|
|
display = vultron.display.render(self.out, [account["account"]])
|
|
|
|
print(display)
|
|
|
|
class VPC(vultron.cmd.Command):
|
|
"""Query for information on VPCs."""
|
|
|
|
def vultron_list(self, *args):
|
|
"""Display all VPCs."""
|
|
|
|
vpcs = self.api.get("vpcs")
|
|
display = vultron.display.render(self.out, vpcs["vpcs"])
|
|
|
|
print(display)
|
|
|
|
class Region(vultron.cmd.Command):
|
|
"""Query for information on Vultr regions."""
|
|
|
|
def vultron_list(self, *args):
|
|
regions = self.api.get("regions")
|
|
display = vultron.display.render(self.out, regions["regions"])
|
|
|
|
print(display)
|
|
|
|
@click.command()
|
|
@click.option("--help", is_flag=True)
|
|
@click.option("--api-key", type=str)
|
|
@click.option("--out", type=click.Choice(vultron.display.TYPES), default="tbl")
|
|
@click.argument("args", nargs=-1)
|
|
# FIXME: click should not show any help messages
|
|
def app(help, api_key, out, args):
|
|
env_key = os.getenv("VULTR_API_KEY")
|
|
|
|
if api_key is None and env_key:
|
|
api_key = env_key
|
|
|
|
shortcut = "help"
|
|
|
|
if len(args):
|
|
shortcut = args[0]
|
|
args = args[1:]
|
|
|
|
try:
|
|
vultron_cmd = vultron.cmd.find_cmd(shortcut)
|
|
vultron_api = vultron_cmd(sys.argv[0], api_key=api_key, out=out)
|
|
|
|
if len(args):
|
|
shortcut = args[0]
|
|
args = args[1:]
|
|
else:
|
|
shortcut = vultron_api.default
|
|
|
|
vultron_fn = vultron_api.find(shortcut)
|
|
vultron_fn(*args)
|
|
except vultron.cmd.CmdNotFound as err:
|
|
# FIXME: Base Vultron error class
|
|
print(err, file=sys.stderr)
|
|
# FIXME: Activate help command
|