Add dcae-cli and component-json-schemas projects
[dcaegen2/platform/cli.git] / dcae-cli / dcae_cli / cli.py
1 # ============LICENSE_START=======================================================
2 # org.onap.dcae
3 # ================================================================================
4 # Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # ============LICENSE_END=========================================================
18 #
19 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
20
21 # -*- coding: utf-8 -*-
22 """
23 Provides entry-level logic for building the CLI. Commands and heavy-lifting logic should be in their own module.
24 """
25 import click
26
27 from dcae_cli import util
28 from dcae_cli.commands.catalog import catalog
29 from dcae_cli.commands.component import component
30 from dcae_cli.commands.data_format import data_format
31 from dcae_cli.commands.profiles import profiles
32 from dcae_cli.catalog import get_catalog
33 from dcae_cli.util.exc import DcaeException
34 from dcae_cli.util.logger import get_logger
35 from dcae_cli.util import config as conf
36 from dcae_cli.util import profiles as prof
37
38
39 log = get_logger('cli')
40
41
42 def _reinit_cli():
43     """Reinit cli"""
44     click.echo("Warning! Reinitializing your dcae-cli configuration")
45     try:
46         conf.reinit_config()
47         prof.reinit_profiles()
48     except Exception as e:
49         raise DcaeException("Failed to reinitialize configuration: {0}".format(e))
50
51 def _reinit_callback(ctx, param, value):
52     """Callback used for the eager --reinit option"""
53     if not value or ctx.resilient_parsing:
54         return
55     _reinit_cli()
56     click.echo("Reinitialize done")
57     ctx.exit()
58
59
60
61 @click.group()
62 @click.option('--verbose', '-v', is_flag=True, default=False, help='Prints INFO-level logs to screen.')
63 # This is following the same pattern as --version
64 # http://click.pocoo.org/5/options/#callbacks-and-eager-options
65 @click.option('--reinit', is_flag=True, callback=_reinit_callback, expose_value=False,
66         is_eager=True, help='Re-initialize dcae-cli configuration')
67 @click.version_option()
68 @click.pass_context
69 def cli(ctx, verbose):
70
71     if ctx.obj is None:
72         ctx.obj = dict()
73
74     if 'config' not in ctx.obj:
75         config = conf.get_config()
76
77         if conf.should_force_reinit(config):
78             if click.confirm("You must reinitialize your dcae-cli configuration. Reinitialize now?",
79                     abort=True):
80                 _reinit_cli()
81
82         ctx.obj['config'] = config
83     else:
84         config = ctx.obj['config']
85
86     if 'catalog' not in ctx.obj:
87         try:
88             ctx.obj['catalog'] = get_catalog(**config)
89         except Exception as e:
90             log.error(e)
91             raise DcaeException("Having issues connecting to the onboarding catalog")
92
93     if verbose:
94         util.logger.set_verbose()
95
96
97 cli.add_command(catalog)
98 cli.add_command(component)
99 cli.add_command(data_format)
100 cli.add_command(profiles)