Create basic command execution driver
This commit is contained in:
parent
2b6cbee91d
commit
c16543e3d3
|
@ -0,0 +1,52 @@
|
||||||
|
class CmdNotFound(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class NoApiKey(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class FeatureMissing(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class Command:
|
||||||
|
FTR_PRFX = "vultron_"
|
||||||
|
|
||||||
|
def __init__(self, cmd, api_key):
|
||||||
|
self.cmd = cmd
|
||||||
|
self.uses_api = True
|
||||||
|
self.default = "help"
|
||||||
|
|
||||||
|
self.init()
|
||||||
|
|
||||||
|
if self.uses_api and api_key is None:
|
||||||
|
raise NoApiKey()
|
||||||
|
|
||||||
|
def init(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def find(self, name):
|
||||||
|
needle = "{prfx}{name}".format(prfx=self.FTR_PRFX, name=name.lower())
|
||||||
|
haystack = dir(self)
|
||||||
|
|
||||||
|
for legume in haystack:
|
||||||
|
if legume.lower().startswith(needle):
|
||||||
|
fn = getattr(self, legume)
|
||||||
|
|
||||||
|
if callable(fn):
|
||||||
|
return fn
|
||||||
|
else:
|
||||||
|
raise FeatureMissing(legume)
|
||||||
|
|
||||||
|
raise CmdNotFound(name)
|
||||||
|
|
||||||
|
def vultron_help(self, *args):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def find_cmd(needle):
|
||||||
|
needle = needle.lower()
|
||||||
|
haystack = Command.__subclasses__()
|
||||||
|
|
||||||
|
for legume in haystack:
|
||||||
|
if legume.__name__.lower().startswith(needle):
|
||||||
|
return legume
|
||||||
|
|
||||||
|
raise CmdNotFound(needle)
|
|
@ -1,4 +1,49 @@
|
||||||
import vultron.api
|
import click
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import vultron.cmd
|
||||||
|
|
||||||
def main():
|
class Help(vultron.cmd.Command):
|
||||||
api = vultron.api.Client()
|
def init(self):
|
||||||
|
self.uses_api = False
|
||||||
|
|
||||||
|
def vultron_help(self, *args):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class Account(vultron.cmd.Command):
|
||||||
|
def init(self):
|
||||||
|
self.default = "info"
|
||||||
|
|
||||||
|
def vultron_info(self, *args):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@click.command()
|
||||||
|
@click.option("--help", is_flag=True)
|
||||||
|
@click.option("--api-key", type=str)
|
||||||
|
@click.argument("args", nargs=-1)
|
||||||
|
def app(help, api_key, 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)
|
||||||
|
|
||||||
|
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:
|
||||||
|
print(err)
|
||||||
|
|
Loading…
Reference in New Issue