Skip to content

Commit

Permalink
Merge pull request #493 from mattbennett/config-command
Browse files Browse the repository at this point in the history
add a command to output parsed config after env var substitution
  • Loading branch information
mattbennett committed Nov 29, 2017
2 parents fd8ffdd + c19a77d commit 80bc4e3
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
25 changes: 25 additions & 0 deletions nameko/cli/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,31 @@ def main(args):
main(args)


class ShowConfig(Command):
""" Output as YAML string the configuration that would be passed to a
service.
Useful for viewing config files that load values from environement
variables.
"""

name = 'show-config'

@staticmethod
def init_parser(parser):

parser.add_argument(
'--config', default='config.yaml',
help='The YAML configuration file')

return parser

@staticmethod
def main(args):
from .show_config import main
main(args)


class Run(Command):
"""Run nameko services. Given a python path to a module containing one or
more nameko services, will host and run them. By default this will try to
Expand Down
11 changes: 11 additions & 0 deletions nameko/cli/show_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from __future__ import print_function

import yaml


def main(args):

with open(args.config) as fle:
config = yaml.load(fle)

print(yaml.dump(config, default_flow_style=False))
41 changes: 41 additions & 0 deletions test/cli/test_show_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from textwrap import dedent

from mock import patch

from nameko.cli.commands import ShowConfig
from nameko.cli.main import setup_parser, setup_yaml_parser


@patch('nameko.cli.main.os')
def test_main(mock_os, tmpdir, capsys):

config = tmpdir.join('config.yaml')
config.write("""
FOO: ${FOO:foobar}
BAR: ${BAR}
""")

parser = setup_parser()
setup_yaml_parser()
args = parser.parse_args([
'show-config',
'--config',
config.strpath,
])

mock_os.environ = {
'BAR': '[1,2,3]'
}

ShowConfig.main(args)
out, _ = capsys.readouterr()

expected = dedent("""
BAR:
- 1
- 2
- 3
FOO: foobar
""").strip()

assert out.strip() == expected

0 comments on commit 80bc4e3

Please sign in to comment.