Replace dashes and colons when using bash formatter

When a resource property has dashes and colons and
the bash formatter is used, it actually breaks with
`command not found`.

For example, when doing:
  eval `osc network show public --format shell`

The network resource has a property:
  provider:network_type="vxlan"

Which results in:
  "bash: provider:network_type=vxlan: command not found"

Change-Id: I57ca08c9dfd7ef022ab91d50ec42a98a6df08b09
Closes-Bug: #1616323
This commit is contained in:
Steve Martinelli 2016-11-09 12:28:04 -05:00
parent 2a3a8c081f
commit a89dbbb2fe
2 changed files with 20 additions and 0 deletions

View File

@ -55,5 +55,11 @@ class ShellFormatter(base.SingleFormatter):
else value)
if isinstance(value, six.string_types):
value = value.replace('"', '\\"')
if isinstance(name, six.string_types):
# Colons and dashes may appear as a resource property but
# are invalid to use in a shell, replace them with an
# underscore.
name = name.replace(':', '_')
name = name.replace('-', '_')
stdout.write('%s%s="%s"\n' % (parsed_args.prefix, name, value))
return

View File

@ -80,3 +80,17 @@ def test_shell_formatter_with_non_string_values():
sf.emit_one(c, d, output, args)
actual = output.getvalue()
assert expected == actual
def test_shell_formatter_with_non_bash_friendly_values():
sf = shell.ShellFormatter()
c = ('a', 'foo-bar', 'provider:network_type')
d = (True, 'baz', 'vxlan')
expected = 'a="True"\nfoo_bar="baz"\nprovider_network_type="vxlan"\n'
output = six.StringIO()
args = mock.Mock()
args.variables = ['a', 'foo-bar', 'provider:network_type']
args.prefix = ''
sf.emit_one(c, d, output, args)
actual = output.getvalue()
assert expected == actual