Skip to content

sort

frequenz.repo.config.cli.version.mike.sort ¤

Sort mike's version.json file with a custom order.

Functions¤

frequenz.repo.config.cli.version.mike.sort.main ¤

main() -> None

Sort mike's version.json file with a custom order.

The versions are sorted using sort_versions().

If no arguments are given, then the contents are read from stdin and the sorted versions are printed to stdout.

If one argument is given, then the contents of the file are replaced with the sorted versions.

If more than one argument is given, then an error is printed to stderr and the program exits with a non-zero exit code.

Source code in frequenz/repo/config/cli/version/mike/sort.py
def main() -> None:
    """Sort `mike`'s `version.json` file with a custom order.

    The versions are sorted using `sort_versions()`.

    If no arguments are given, then the contents are read from stdin and the sorted
    versions are printed to stdout.

    If one argument is given, then the contents of the file are replaced with the sorted
    versions.

    If more than one argument is given, then an error is printed to stderr and the
    program exits with a non-zero exit code.
    """
    github.configure_logging()

    match len(sys.argv):
        case 1:
            _dump_versions_to(_load_and_sort_versions_from(sys.stdin), sys.stdout)
        case 2:
            with open(sys.argv[1], "r", encoding="utf8") as stream_in:
                versions = _load_and_sort_versions_from(stream_in)
            with open(sys.argv[1], "w", encoding="utf8") as stream_out:
                _dump_versions_to(versions, stream_out)

        case _:
            print(
                f"""\
Usage: {sys.argv[0]} [<versions.json>]

If <versions.json> is given, the contents will be replaced with the sorted versions.
Otherwise, the contents are read from stdin and the sorted versions are printed to stdout.
""",
                file=sys.stderr,
            )
            sys.exit(2)