Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from pilosus/cli-tool
Piny CLI tool added (#35)
- Loading branch information
Showing
14 changed files
with
217 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
Sphinx==2.1.1 | ||
sphinx-rtd-theme==0.4.3 | ||
sphinx-click==2.2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from .errors import ConfigError, LoadingError, ValidationError | ||
from .loaders import YamlLoader | ||
from .loaders import YamlLoader, YamlStreamLoader | ||
from .matchers import Matcher, MatcherWithDefaults, StrictMatcher | ||
from .validators import MarshmallowValidator, PydanticValidator, TrafaretValidator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from typing import Any | ||
|
||
import click | ||
import yaml | ||
|
||
from .loaders import YamlStreamLoader | ||
from .matchers import MatcherWithDefaults, StrictMatcher | ||
|
||
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"]) | ||
|
||
|
||
@click.command(context_settings=CONTEXT_SETTINGS) | ||
@click.argument("input", required=False, type=click.File("r")) | ||
@click.argument("output", required=False, type=click.File("w")) | ||
@click.option( | ||
"--strict/--no-strict", default=True, help="Enable or disable strict matcher" | ||
) | ||
def cli(input, output, strict) -> Any: | ||
""" | ||
Substitute environment variables with their values. | ||
Read INPUT, find environment variables in it, | ||
substitute them with their values and write to OUTPUT. | ||
INPUT and OUTPUT can be files or standard input and output respectively. | ||
With no INPUT, or when INPUT is -, read standard input. | ||
With no OUTPUT, or when OUTPUT is -, write to standard output. | ||
Examples: | ||
\b | ||
piny input.yaml output.yaml | ||
piny - output.yaml | ||
piny input.yaml - | ||
tail -n 12 input.yaml | piny > output.yaml | ||
""" | ||
stdin = click.get_text_stream("stdin") | ||
stdout = click.get_text_stream("stdout") | ||
|
||
config = YamlStreamLoader( | ||
stream=input or stdin, matcher=StrictMatcher if strict else MatcherWithDefaults | ||
).load() | ||
|
||
yaml.dump(config, output or stdout) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# Package dependencies | ||
PyYAML==5.1.1 | ||
Click==7.0 | ||
marshmallow==2.19.4 | ||
pydantic==0.28 | ||
trafaret==1.2.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from unittest import mock | ||
|
||
import pytest | ||
import yaml | ||
from click.testing import CliRunner | ||
|
||
from piny import LoadingError | ||
from piny.cli import cli | ||
|
||
from . import config_directory | ||
|
||
|
||
def test_cli_input_stdin_output_stdout(): | ||
runner = CliRunner() | ||
with mock.patch("piny.matchers.StrictMatcher.constructor") as expand_mock: | ||
expand_mock.return_value = "MySecretPassword" | ||
result = runner.invoke(cli, input="password: ${DB_PASSWORD}") | ||
|
||
assert result.exit_code == 0 | ||
assert result.stdout == "password: MySecretPassword\n" | ||
|
||
|
||
def test_cli_input_file_output_file(): | ||
runner = CliRunner() | ||
with open(config_directory.joinpath("db.yaml"), "r") as f: | ||
input_lines = f.readlines() | ||
|
||
with runner.isolated_filesystem(): | ||
with open("input.yaml", "w") as input_fd: | ||
input_fd.writelines(input_lines) | ||
|
||
with mock.patch("piny.matchers.StrictMatcher.constructor") as expand_mock: | ||
expand_mock.return_value = "MySecretPassword" | ||
result = runner.invoke(cli, ["input.yaml", "output.yaml"]) | ||
|
||
with open("output.yaml", "r") as output_fd: | ||
output_lines = output_fd.readlines() | ||
|
||
assert result.exit_code == 0 | ||
assert "password: MySecretPassword" in map( | ||
lambda x: x.strip(), output_lines | ||
) | ||
|
||
|
||
def test_cli_fail(): | ||
runner = CliRunner() | ||
with mock.patch("piny.loaders.yaml.load") as loader_mock: | ||
loader_mock.side_effect = yaml.YAMLError("Oops!") | ||
result = runner.invoke(cli, input="password: ${DB_PASSWORD}") | ||
assert result.exit_code == 1 | ||
assert type(result.exception) == LoadingError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters