Ejemplo n.º 1
0
def main():
    """Main code."""
    args = processcli()
    log = ConsolePrinter(args)
    validateargs(args, log)

    # For the remainder of processing, overwrite overwrites output
    if args.overwrite:
        args.output = args.overwrite

    # Merge all input files
    yaml_editor = Parsers.get_yaml_editor()
    merge_config = MergerConfig(log, args)
    exit_state = 0
    consumed_stdin = False
    mergers: List[Merger] = []
    merge_count = 0
    for yaml_file in args.yaml_files:
        if yaml_file.strip() == '-':
            consumed_stdin = True

        log.debug(
            "yaml_merge::main:  Processing file, {}".format(
                "STDIN" if yaml_file.strip() == "-" else yaml_file))

        if len(mergers) < 1:
            (mergers, mergers_loaded) = get_doc_mergers(
                log, yaml_editor, merge_config, yaml_file)
            if not mergers_loaded:
                exit_state = 4
                break
        else:
            # Merge RHS into LHS
            exit_state = merge_docs(
                log, yaml_editor, merge_config, mergers, yaml_file)
            if not exit_state == 0:
                break
            merge_count += 1

    # Check for a waiting STDIN document
    if (exit_state == 0
        and not consumed_stdin
        and not args.nostdin
        and not sys.stdin.isatty()
    ):
        exit_state = merge_docs(log, yaml_editor, merge_config, mergers, "-")
        merge_count += 1

    # When no merges have occurred, check for a single-doc merge request
    if (exit_state == 0
        and merge_count == 0
        and merge_config.get_multidoc_mode() is MultiDocModes.CONDENSE_ALL
    ):
        exit_state = merge_condense_all(log, mergers, [])

    # Output the final document
    if exit_state == 0:
        write_output_document(args, log, yaml_editor, mergers)

    sys.exit(exit_state)
Ejemplo n.º 2
0
def merge_docs(
    log: ConsolePrinter, yaml_editor: YAML, config: MergerConfig,
    lhs_docs: List[Merger], rhs_file: str
) -> int:
    """Merge RHS into LHS."""
    return_state = 0
    merge_mode = config.get_multidoc_mode()
    (rhs_docs, rhs_loaded) = get_doc_mergers(
        log, yaml_editor, config, rhs_file)
    if not rhs_loaded:
        # Failed to load any RHS documents
        return 3

    if merge_mode is MultiDocModes.CONDENSE_ALL:
        return_state = merge_condense_all(log, lhs_docs, rhs_docs)

    elif merge_mode is MultiDocModes.MERGE_ACROSS:
        return_state = merge_across(log, lhs_docs, rhs_docs)

    elif merge_mode is MultiDocModes.MATRIX_MERGE:
        return_state = merge_matrix(log, lhs_docs, rhs_docs)

    return return_state
Ejemplo n.º 3
0
 def test_get_multidoc_mode_cli(self, quiet_logger):
     mc = MergerConfig(quiet_logger,
                       SimpleNamespace(multi_doc_mode="matrix_merge"))
     assert mc.get_multidoc_mode() == MultiDocModes.MATRIX_MERGE
Ejemplo n.º 4
0
 def test_get_multidoc_mode_default(self, quiet_logger):
     mc = MergerConfig(quiet_logger, SimpleNamespace())
     assert mc.get_multidoc_mode() == MultiDocModes.CONDENSE_ALL