[cli] fix domain command and add config_file argument (as json)
This commit is contained in:
parent
cbb40f03ab
commit
30b3b07afc
|
@ -8,6 +8,8 @@ import sys
|
||||||
import importlib
|
import importlib
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
import json
|
||||||
|
|
||||||
import click
|
import click
|
||||||
import orjson
|
import orjson
|
||||||
|
|
||||||
|
@ -120,9 +122,10 @@ def list_api_routes():
|
||||||
@click.option('--create',default=False, is_flag=True)
|
@click.option('--create',default=False, is_flag=True)
|
||||||
@click.option('--update',default=False, is_flag=True)
|
@click.option('--update',default=False, is_flag=True)
|
||||||
@click.option('--delete',default=False, is_flag=True)
|
@click.option('--delete',default=False, is_flag=True)
|
||||||
|
@click.argument('config_file', type=click.File(mode='rb'), required=False)
|
||||||
@click.argument('domain',default=None, required=False)
|
@click.argument('domain',default=None, required=False)
|
||||||
@cli.command()
|
@cli.command()
|
||||||
def domain(domain, delete, update, create, read): #, domains, read, create, update, delete):
|
def domain(domain, config_file, delete, update, create, read): #, domains, read, create, update, delete):
|
||||||
"""
|
"""
|
||||||
The "halfapi domain" command
|
The "halfapi domain" command
|
||||||
|
|
||||||
|
@ -147,17 +150,14 @@ def domain(domain, delete, update, create, read): #, domains, read, create, upd
|
||||||
from ..conf import CONFIG
|
from ..conf import CONFIG
|
||||||
from ..halfapi import HalfAPI
|
from ..halfapi import HalfAPI
|
||||||
|
|
||||||
try:
|
if config_file:
|
||||||
config_domain = CONFIG.pop('domain').get(domain, {})
|
CONFIG = json.loads(''.join(
|
||||||
except KeyError:
|
[ line.decode() for line in config_file.readlines() ]
|
||||||
config_domain = {}
|
))
|
||||||
|
|
||||||
halfapi = HalfAPI(CONFIG)
|
halfapi = HalfAPI(CONFIG)
|
||||||
|
|
||||||
half_domain = halfapi.add_domain(domain, config=config_domain)
|
|
||||||
|
|
||||||
click.echo(orjson.dumps(
|
click.echo(orjson.dumps(
|
||||||
half_domain.schema(),
|
halfapi.domains[domain].schema(),
|
||||||
option=orjson.OPT_NON_STR_KEYS,
|
option=orjson.OPT_NON_STR_KEYS,
|
||||||
default=ORJSONResponse.default_cast)
|
default=ORJSONResponse.default_cast)
|
||||||
)
|
)
|
||||||
|
|
|
@ -122,12 +122,12 @@ class HalfAPI(Starlette):
|
||||||
|
|
||||||
domain_key = domain.get('name', key)
|
domain_key = domain.get('name', key)
|
||||||
|
|
||||||
self.add_domain(
|
add_domain_args = {
|
||||||
domain_key,
|
**domain,
|
||||||
domain.get('module'),
|
'path': path
|
||||||
domain.get('router'),
|
}
|
||||||
domain.get('acl'),
|
|
||||||
path)
|
self.add_domain(**add_domain_args)
|
||||||
|
|
||||||
schemas.append(self.__domains[domain_key].schema())
|
schemas.append(self.__domains[domain_key].schema())
|
||||||
|
|
||||||
|
@ -246,28 +246,26 @@ class HalfAPI(Starlette):
|
||||||
def domains(self):
|
def domains(self):
|
||||||
return self.__domains
|
return self.__domains
|
||||||
|
|
||||||
def add_domain(self, name, module=None, router=None, acl=None, path='/', config=None):
|
def add_domain(self, **kwargs):
|
||||||
|
|
||||||
# logger.debug('HalfApi.add_domain %s %s %s %s %s',
|
if not kwargs.get('enabled'):
|
||||||
# name,
|
raise Exception(f'Domain not enabled ({kwargs})')
|
||||||
# module,
|
|
||||||
# router,
|
|
||||||
# acl,
|
|
||||||
# path,
|
|
||||||
# config)
|
|
||||||
|
|
||||||
if config:
|
name = kwargs['name']
|
||||||
self.config['domain'][name] = config
|
|
||||||
|
|
||||||
if not module:
|
self.config['domain'][name] = kwargs.get('config', {})
|
||||||
|
|
||||||
|
if not kwargs.get('module'):
|
||||||
module = name
|
module = name
|
||||||
|
else:
|
||||||
|
module = kwargs.get('module')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.__domains[name] = HalfDomain(
|
self.__domains[name] = HalfDomain(
|
||||||
name,
|
name,
|
||||||
module=importlib.import_module(module),
|
module=importlib.import_module(module),
|
||||||
router=router,
|
router=kwargs.get('router'),
|
||||||
acl=acl,
|
acl=kwargs.get('acl'),
|
||||||
app=self
|
app=self
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -279,6 +277,6 @@ class HalfAPI(Starlette):
|
||||||
))
|
))
|
||||||
raise exc
|
raise exc
|
||||||
|
|
||||||
self.mount(path, self.__domains[name])
|
self.mount(kwargs.get('path', name), self.__domains[name])
|
||||||
|
|
||||||
return self.__domains[name]
|
return self.__domains[name]
|
||||||
|
|
|
@ -11,6 +11,7 @@ from ..cli.cli import cli
|
||||||
from ..halfapi import HalfAPI
|
from ..halfapi import HalfAPI
|
||||||
from ..half_domain import HalfDomain
|
from ..half_domain import HalfDomain
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
|
import tempfile
|
||||||
|
|
||||||
class TestDomain(TestCase):
|
class TestDomain(TestCase):
|
||||||
@property
|
@property
|
||||||
|
@ -53,6 +54,7 @@ class TestDomain(TestCase):
|
||||||
'name': self.DOMAIN,
|
'name': self.DOMAIN,
|
||||||
'router': self.ROUTERS,
|
'router': self.ROUTERS,
|
||||||
'acl': self.ACL,
|
'acl': self.ACL,
|
||||||
|
'module': self.MODULE,
|
||||||
'prefix': False,
|
'prefix': False,
|
||||||
'enabled': True,
|
'enabled': True,
|
||||||
'config': {
|
'config': {
|
||||||
|
@ -60,6 +62,10 @@ class TestDomain(TestCase):
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_, self.config_file = tempfile.mkstemp()
|
||||||
|
with open(self.config_file, 'w') as fh:
|
||||||
|
fh.write(json.dumps(self.halfapi_conf))
|
||||||
|
|
||||||
self.halfapi = HalfAPI(self.halfapi_conf)
|
self.halfapi = HalfAPI(self.halfapi_conf)
|
||||||
|
|
||||||
self.client = TestClient(self.halfapi.application)
|
self.client = TestClient(self.halfapi.application)
|
||||||
|
@ -77,7 +83,7 @@ class TestDomain(TestCase):
|
||||||
try:
|
try:
|
||||||
result = self.runner.invoke(cli, '--version')
|
result = self.runner.invoke(cli, '--version')
|
||||||
self.assertEqual(result.exit_code, 0)
|
self.assertEqual(result.exit_code, 0)
|
||||||
result = self.runner.invoke(cli, ['domain', self.DOMAIN])
|
result = self.runner.invoke(cli, ['domain', self.DOMAIN, self.config_file])
|
||||||
self.assertEqual(result.exit_code, 0)
|
self.assertEqual(result.exit_code, 0)
|
||||||
result_d = json.loads(result.stdout)
|
result_d = json.loads(result.stdout)
|
||||||
result = self.runner.invoke(cli, ['run', '--help'])
|
result = self.runner.invoke(cli, ['run', '--help'])
|
||||||
|
|
Loading…
Reference in New Issue