def config(ctx, check, sync, verbose): """Validate default configuration files.""" repo_choice = ctx.obj['repo_choice'] if check: checks = [check] elif repo_choice == 'agent': checks = ['agent'] else: checks = sorted(get_valid_checks()) files_failed = {} files_warned = {} file_counter = [] echo_waiting('Validating default configuration files...') for check in checks: check_display_queue = [] spec_path = get_config_spec(check) if not file_exists(spec_path): validate_config_legacy(check, check_display_queue, files_failed, files_warned, file_counter) if verbose: check_display_queue.append( lambda: echo_warning('No spec found', indent=True)) if check_display_queue: echo_info(f'{check}:') for display in check_display_queue: display() continue file_counter.append(None) # source is the default file name if check == 'agent': display_name = 'Datadog Agent' source = 'datadog' version = None else: display_name = load_manifest(check).get('display_name', check) source = check version = get_version_string(check) spec = ConfigSpec(read_file(spec_path), source=source, version=version) spec.load() if spec.errors: files_failed[spec_path] = True for error in spec.errors: check_display_queue.append(lambda error=error, **kwargs: echo_failure(error, **kwargs)) else: if spec.data['name'] != display_name: files_failed[spec_path] = True check_display_queue.append(lambda **kwargs: echo_failure( f"Spec name `{spec.data['name']}` should be `{display_name}`", **kwargs)) example_location = get_data_directory(check) example_consumer = ExampleConsumer(spec.data) for example_file, (contents, errors) in example_consumer.render().items(): file_counter.append(None) example_file_path = path_join(example_location, example_file) if errors: files_failed[example_file_path] = True for error in errors: check_display_queue.append( lambda error=error, **kwargs: echo_failure( error, **kwargs)) else: if not file_exists(example_file_path) or read_file( example_file_path) != contents: if sync: echo_info( f"Writing config file to `{example_file_path}`" ) write_file(example_file_path, contents) else: files_failed[example_file_path] = True check_display_queue.append( lambda example_file=example_file, **kwargs: echo_failure( f'File `{example_file}` is not in sync, run "ddev validate config -s"', **kwargs)) if check_display_queue or verbose: echo_info(f'{check}:') if verbose: check_display_queue.append( lambda **kwargs: echo_info('Valid spec', **kwargs)) for display in check_display_queue: display(indent=True) num_files = len(file_counter) files_failed = len(files_failed) files_warned = len(files_warned) files_passed = num_files - (files_failed + files_warned) if files_failed or files_warned: click.echo() if files_failed: echo_failure(f'Files with errors: {files_failed}') if files_warned: echo_warning(f'Files with warnings: {files_warned}') if files_passed: if files_failed or files_warned: echo_success(f'Files valid: {files_passed}') else: echo_success(f'All {num_files} configuration files are valid!') if files_failed: abort()
def config(ctx, check, sync): """Validate default configuration files.""" repo_choice = ctx.obj['repo_choice'] if check: checks = [check] elif repo_choice == 'agent': checks = ['agent'] else: checks = sorted(get_valid_checks()) files_failed = {} files_warned = {} file_counter = [] echo_waiting('Validating default configuration files...') for check in checks: check_display_queue = [] spec_path = get_config_spec(check) if not file_exists(spec_path): validate_config_legacy(check, check_display_queue, files_failed, files_warned, file_counter) for display in check_display_queue: display() continue # Just use six to make it easier to search for occurrences of text we need to remove when we drop Python 2 if PY2: check_display_queue.append(lambda **kwargs: echo_failure( 'Dictionary key order is only guaranteed in Python 3.7.0+', ** kwargs)) file_counter.append(None) # source is the default file name if check == 'agent': display_name = 'Datadog Agent' source = 'datadog' version = None else: display_name = load_manifest(check).get('display_name', check) source = check version = get_version_string(check) spec = ConfigSpec(read_file(spec_path), source=source, version=version) spec.load() if spec.errors: files_failed[spec_path] = True for error in spec.errors: check_display_queue.append(lambda error=error, **kwargs: echo_failure(error, **kwargs)) else: if spec.data['name'] != display_name: files_failed[spec_path] = True check_display_queue.append(lambda **kwargs: echo_failure( 'Spec name `{}` should be `{}`'.format( spec.data['name'], display_name), **kwargs)) example_location = get_data_directory(check) example_consumer = ExampleConsumer(spec.data) for example_file, (contents, errors) in example_consumer.render().items(): file_counter.append(None) example_file_path = path_join(example_location, example_file) if errors: files_failed[example_file_path] = True for error in errors: check_display_queue.append( lambda error=error, **kwargs: echo_failure( error, **kwargs)) else: if not file_exists(example_file_path) or read_file( example_file_path) != contents: if sync: write_file(example_file_path, contents) else: files_failed[example_file_path] = True check_display_queue.append( lambda example_file=example_file, **kwargs: echo_failure( 'File `{}` needs to be synced'.format( example_file), **kwargs)) if check_display_queue: echo_info('{}:'.format(check)) for display in check_display_queue: display(indent=True) num_files = len(file_counter) files_failed = len(files_failed) files_warned = len(files_warned) files_passed = num_files - (files_failed + files_warned) if files_failed or files_warned: click.echo() if files_failed: echo_failure('Files with errors: {}'.format(files_failed)) if files_warned: echo_warning('Files with warnings: {}'.format(files_warned)) if files_passed: if files_failed or files_warned: echo_success('Files valid: {}'.format(files_passed)) else: echo_success( 'All {} configuration files are valid!'.format(num_files)) if files_failed: abort()
def get_spec(text, **kwargs): kwargs.setdefault('source', 'test') return ConfigSpec(normalize_yaml(text), **kwargs)