Magic command line

This commit is contained in:
jpic 2020-02-16 21:34:41 +01:00
parent 33c37f8e44
commit 41ec8db301
5 changed files with 45 additions and 16 deletions

View File

@ -5,7 +5,7 @@ setup(
name='shlax', name='shlax',
versioning='dev', versioning='dev',
setup_requires='setupmeta', setup_requires='setupmeta',
install_requires=['cli2'], install_requires=['cli2>=1.1.6'],
extras_require=dict( extras_require=dict(
full=[ full=[
'pyyaml', 'pyyaml',

View File

@ -10,6 +10,7 @@ import asyncio
import cli2 import cli2
import copy import copy
import inspect import inspect
import importlib
import glob import glob
import os import os
import sys import sys
@ -24,9 +25,32 @@ class ConsoleScript(cli2.ConsoleScript):
self.shlaxfile = None self.shlaxfile = None
shlaxfile = sys.argv.pop(1) if len(sys.argv) > 1 else '' shlaxfile = sys.argv.pop(1) if len(sys.argv) > 1 else ''
if shlaxfile: if shlaxfile:
if os.path.exists(shlaxfile): if not os.path.exists(shlaxfile):
try: # missing shlaxfile, what are we gonna do !!
mod = importlib.import_module('shlax.repo.' + shlaxfile)
except ImportError:
print('Could not find ' + shlaxfile)
self.exit_code = 1
return
shlaxfile = mod.__file__
self.shlaxfile = Shlaxfile() self.shlaxfile = Shlaxfile()
self.shlaxfile.parse(shlaxfile) self.shlaxfile.parse(shlaxfile)
if len(self.shlaxfile.actions) == 1 and 'main' in self.shlaxfile.actions:
action = self.shlaxfile.actions['main']
self._doc = inspect.getdoc(action)
for name, doc in self.shlaxfile.actions['main'].doc.items():
self[name] = cli2.Callable(
name,
action.callable(),
options={
k: cli2.Option(name=k, **v)
for k, v in action.options.items()
},
color=getattr(action, 'color', cli2.YELLOW),
doc=doc,
)
else:
for name, action in self.shlaxfile.actions.items(): for name, action in self.shlaxfile.actions.items():
self[name] = cli2.Callable( self[name] = cli2.Callable(
name, name,
@ -37,24 +61,13 @@ class ConsoleScript(cli2.ConsoleScript):
}, },
color=getattr(action, 'color', cli2.YELLOW), color=getattr(action, 'color', cli2.YELLOW),
) )
else:
try:
mod = importlib.import_module('shlax.repo.' + shlaxfile)
except ImportError:
print('Could not find ' + shlaxfile)
self.exit_code = 1
return
else: else:
from shlax import repo from shlax import repo
path = repo.__path__._path[0] path = repo.__path__._path[0]
for shlaxfile in glob.glob(os.path.join(path, '*.py')): for shlaxfile in glob.glob(os.path.join(path, '*.py')):
name = shlaxfile.split('/')[-1].split('.')[0] name = shlaxfile.split('/')[-1].split('.')[0]
import importlib
mod = importlib.import_module('shlax.repo.' + name) mod = importlib.import_module('shlax.repo.' + name)
for k, v in mod.__dict__.items(): self[name] = cli2.Callable(name, mod)
if callable(v):
break
self[name] = cli2.Callable(name, v, doc='lol')
return super().__call__(*args, **kwargs) return super().__call__(*args, **kwargs)

View File

@ -1,4 +1,8 @@
#!/usr/bin/env shlax #!/usr/bin/env shlax
"""
Manage a traefik container maintained by Shlax community.
"""
from shlax import * from shlax import *
main = Container( main = Container(

View File

@ -20,6 +20,7 @@ class Shlaxfile:
self.actions[name] = value self.actions[name] = value
elif callable(value) and getattr(value, '__name__', '').startswith('test_'): elif callable(value) and getattr(value, '__name__', '').startswith('test_'):
self.tests[value.__name__] = value self.tests[value.__name__] = value
self.paths.append(path) self.paths.append(path)
@property @property

View File

@ -3,6 +3,17 @@ from .script import Script
class Container(Script): class Container(Script):
"""
Wolcome to crazy container control cli
Such wow
"""
doc = dict(
build='Execute the Build script',
test='Execute the Test script (if any)',
push='Push the container (if Build script defines commit kwarg)',
)
async def call(self, *args, **kwargs): async def call(self, *args, **kwargs):
if not args or 'build' in args: if not args or 'build' in args:
await self.kwargs['build'](**kwargs) await self.kwargs['build'](**kwargs)