Skip to content

Commit

Permalink
Add cli options "check" and "in-place"
Browse files Browse the repository at this point in the history
  • Loading branch information
pappasam committed Aug 19, 2019
1 parent 13de36f commit ea0d25e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 28 deletions.
20 changes: 4 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,13 @@ This project can be used as either a command line utility or a Python library.

### Command line interface

Print detailed help
Stdin -> Disk : cat input.toml | toml-sort

toml-sort --help
Disk -> Disk : toml-sort -o output.toml input.toml

Read from stdin, write to stdout:
Linting : toml-sort --check input.toml

cat input.toml | toml-sort

Read from file on disk, write to file on disk:

toml-sort -o output.toml input.toml

Read from file on disk, write to stdout

toml-sort input.toml

Read from stdin, write to file on disk

cat input.toml | toml-sort -o output.toml
Inplace Disk : toml-sort --in-place input.toml

## Example

Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.masonry.api"

[tool.poetry]
name = "toml-sort"
version = "0.15.1"
version = "0.16.0"
description = "Toml sorting library"
authors = ["Sam Roeca <[email protected]>"]
readme = "README.md"
Expand Down Expand Up @@ -44,7 +44,7 @@ commands =
poetry run black --check --diff toml_sort tests
poetry run mypy toml_sort
poetry run pylint toml_sort tests
poetry run pytest
poetry run pytest -vv
"""

[tool.black]
Expand Down
82 changes: 72 additions & 10 deletions toml_sort/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,33 @@

from . import TomlSort

# The standard stream
STD_STREAM = "-"


def read_file(path: str) -> str:
"""Read contents from a file"""
if path == STD_STREAM:
return sys.stdin.read()
with open(path, "r") as fileobj:
return fileobj.read()


def write_file(path: str, content: str) -> None:
"""Write content to a path"""
if path == STD_STREAM:
click.echo(content, nl=False)
return
with open(path, "w") as fileobj:
fileobj.write(content)


@click.command()
@click.option(
"-o",
"--output",
type=click.File("w"),
default="-",
type=click.Path(file_okay=True, writable=True, allow_dash=True),
default=STD_STREAM,
show_default=True,
help="The output filepath. Choose stdout with '-'.",
)
Expand All @@ -25,27 +45,54 @@
"Default is to only sort non-inline 'tables and arrays of tables'."
),
)
@click.option(
"-i",
"--in-place",
is_flag=True,
help=(
"Makes changes to the original input file. "
"Note: you cannot redirect from a file to itself in Bash. "
"POSIX shells process redirections first, then execute the command."
),
)
@click.option(
"--no-header",
is_flag=True,
help="Do not keep a document's leading comments.",
)
@click.argument("filename", type=click.File("r"), default="-")
@click.option(
"--check",
is_flag=True,
help=(
"Check if an original file is changed by the formatter. "
"Return code 0 means it would not change. "
"Return code 1 means it would change. "
),
)
@click.argument(
"filename",
type=click.Path(
exists=True, file_okay=True, readable=True, allow_dash=True
),
default=STD_STREAM,
)
@click.version_option()
def cli(output, _all, no_header, filename) -> None:
def cli(output, _all, in_place, no_header, check, filename) -> None:
"""Sort toml file FILENAME, saving results to a file, or stdout (default)
FILENAME a filepath or standard input (-)
Examples (non-exhaustive list):
Read from stdin, write to stdout: cat input.toml | toml-sort
Stdin -> Disk : cat input.toml | toml-sort
Disk -> Disk : toml-sort -o output.toml input.toml
Read from disk, write to disk : toml-sort -o output.toml input.toml
Linting : toml-sort --check input.toml
Read from disk, write to stdout : toml-sort input.toml
Inplace Disk : toml-sort --in-place input.toml
"""
if filename.isatty():
if filename == "-" and sys.stdin.isatty():
error_message_if_terminal = """
toml-sort: missing FILENAME, and no stdin
Usage: toml-sort [OPTIONS] [FILENAME]
Expand All @@ -54,9 +101,24 @@ def cli(output, _all, no_header, filename) -> None:
""".strip()
click.echo(error_message_if_terminal, err=True)
sys.exit(1)
elif in_place and filename == STD_STREAM:
click.echo("Cannot format stdin in-place", err=True)
sys.exit(1)
elif in_place and output != STD_STREAM:
click.echo("Cannot specify output file with in-place", err=True)
sys.exit(1)
original_toml = read_file(filename)
sorted_toml = TomlSort(
input_toml=filename.read(),
input_toml=original_toml,
only_sort_tables=not bool(_all),
no_header=bool(no_header),
).sorted()
output.write(sorted_toml)
if check:
if original_toml != sorted_toml:
click.echo("File would be re-formatted", err=True)
sys.exit(1)
return
if in_place:
write_file(filename, sorted_toml)
return
write_file(output, sorted_toml)

0 comments on commit ea0d25e

Please sign in to comment.