Establish pattern for click cli interface commands
Set up the pattern of having command groups in packages under the cli package. Added lambdas to retrieve environment values as default values for --url and --token. Triggered messages other than stack dump to user of cli if the values are not resolved.
This commit is contained in:
parent
fb0ba7fb23
commit
8737b8ef4c
|
@ -15,35 +15,34 @@ import os
|
|||
|
||||
import click
|
||||
|
||||
from .design import commands as design
|
||||
|
||||
@click.group()
|
||||
@click.option('--debug/--no-debug', default=False)
|
||||
@click.option('--token')
|
||||
@click.option('--url')
|
||||
@click.option('--token', default=lambda: os.environ.get('DD_TOKEN', None))
|
||||
@click.option('--url' , default=lambda: os.environ.get('DD_URL', None))
|
||||
@click.pass_context
|
||||
def drydock(ctx, debug, token, url):
|
||||
""" Base cli command. Peforms validations and sets default values.
|
||||
"""
|
||||
if not ctx.obj:
|
||||
ctx.obj = {}
|
||||
|
||||
ctx.obj['DEBUG'] = debug
|
||||
|
||||
option_validation_error = False
|
||||
|
||||
if not token:
|
||||
ctx.obj['TOKEN'] = os.environ['DD_TOKEN']
|
||||
else:
|
||||
ctx.obj['TOKEN'] = token
|
||||
click.echo("Error: Token must be specified either by "
|
||||
"--token or DD_TOKEN from the environment")
|
||||
option_validation_error = True
|
||||
|
||||
if not url:
|
||||
ctx.obj['URL'] = os.environ['DD_URL']
|
||||
else:
|
||||
ctx.obj['URL'] = url
|
||||
click.echo("Error: URL must be specified either by "
|
||||
"--url or DD_URL from the environment")
|
||||
option_validation_error = True
|
||||
|
||||
@drydock.group()
|
||||
def create():
|
||||
pass
|
||||
if option_validation_error:
|
||||
ctx.exit()
|
||||
|
||||
@drydock.group()
|
||||
def list()
|
||||
pass
|
||||
|
||||
@drydock.group()
|
||||
def show():
|
||||
pass
|
||||
|
||||
@create.command()
|
||||
def design
|
||||
drydock.add_command(design.design)
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
# Copyright 2017 AT&T Intellectual Property. All other rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import click
|
||||
|
||||
@click.group()
|
||||
def design():
|
||||
click.echo('design invoked')
|
||||
|
||||
@design.command()
|
||||
def create():
|
||||
click.echo('create invoked')
|
||||
|
||||
@design.command()
|
||||
def list():
|
||||
click.echo('list invoked')
|
||||
|
||||
@design.command()
|
||||
def show():
|
||||
click.echo('design invoked.')
|
5
setup.py
5
setup.py
|
@ -40,7 +40,9 @@ setup(name='drydock_provisioner',
|
|||
'drydock_provisioner.drivers.node',
|
||||
'drydock_provisioner.drivers.node.maasdriver',
|
||||
'drydock_provisioner.drivers.node.maasdriver.models',
|
||||
'drydock_provisioner.control'],
|
||||
'drydock_provisioner.control',
|
||||
'drydock_provisioner.cli',
|
||||
'drydock_provisioner.cli.design'],
|
||||
install_requires=[
|
||||
'PyYAML',
|
||||
'pyghmi>=1.0.18',
|
||||
|
@ -55,6 +57,7 @@ setup(name='drydock_provisioner',
|
|||
],
|
||||
entry_points={
|
||||
'oslo.config.opts': 'drydock_provisioner = drydock_provisioner.config:list_opts',
|
||||
'console_scripts': 'drydock = drydock_provisioner.cli.commands:drydock'
|
||||
}
|
||||
)
|
||||
|
||||
|
|
Loading…
Reference in New Issue