Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion cfbs/cfbs_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
is_module_local,
is_module_absolute,
)
from cfbs.prompts import prompt_user, prompt_user_yesno
from cfbs.prompts import prompt_user, prompt_user_yesno, prompt_user_multiline
from cfbs.validate import validate_single_module


Expand Down Expand Up @@ -565,6 +565,15 @@ def _input_string(input_data):
)
return response

def _input_multiline_string(input_data):
_check_keys(["question"], input_data)
response = prompt_user_multiline(
self.non_interactive,
input_data["question"],
default=input_data.get("default"),
)
return response

def _input_elements(subtype):
result = OrderedDict()
for element in subtype:
Expand Down Expand Up @@ -615,6 +624,8 @@ def _input_list(input_data):

if definition["type"] == "string":
definition["response"] = _input_string(definition)
elif definition["type"] == "string-multiline":
definition["response"] = _input_multiline_string(definition)
elif definition["type"] == "list":
definition["response"] = _input_list(definition)
else:
Expand Down
34 changes: 34 additions & 0 deletions cfbs/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,40 @@ def prompt_user(non_interactive: bool, prompt: str, choices=None, default=None):
return answer


def prompt_user_multiline(non_interactive: bool, prompt: str, default=None):
if non_interactive:
if default is None:
raise ValueError(
"Missing default value for prompt '%s' in non-interactive mode" % prompt
)
return default

print(prompt)
print(
"(Enter one or more lines of text, then finish with double newline or Ctrl+D"
" (Ctrl+Z followed by Enter on Windows))"
)
Comment thread
SimonThalvorsen marked this conversation as resolved.

lines = []
while True:
try:
inp = input()
if len(lines) > 0 and inp == "" == lines[-1]:
break
lines.append(inp)
except EOFError:
break
except KeyboardInterrupt:
print("\nOperation cancelled by user")
exit(1)

answer = "\n".join(lines)
if answer == "" and default is not None:
answer = default

return answer


def prompt_user_yesno(non_interactive: bool, prompt: str, default="yes"):
"""Returns `True` if the answer is yes, and `False` otherwise."""

Expand Down
4 changes: 2 additions & 2 deletions cfbs/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,10 +682,10 @@ def _validate_module_input(name, module):
% field,
)

if input_element["type"] not in ("string", "list"):
if input_element["type"] not in ("string", "list", "string-multiline"):
raise CFBSValidationError(
name,
'The input "type" must be "string" or "list", not "%s"'
'The input "type" must be "string", "string-multiline", or "list", not "%s"'
% input_element["type"],
)
if not re.fullmatch(r"[a-z_]+", input_element["variable"]):
Expand Down
17 changes: 17 additions & 0 deletions tests/shell/059_input_multilinestring.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
set -e
set -x
cd tests/
mkdir -p ./tmp/
cd ./tmp/
touch cfbs.json && rm cfbs.json
rm -rf .git
rm -rf create-single-file-with-content
cp ../shell/059_input_multilinestring/example-cfbs.json cfbs.json

cfbs --non-interactive input create-single-file-with-content
grep '"type": "string-multiline"' create-single-file-with-content/input.json
grep '"default": "Hello CFEngine!\\nBye CFEngine!"' create-single-file-with-content/input.json
grep '"response": "Hello CFEngine!\\nBye CFEngine!"' create-single-file-with-content/input.json

cfbs render-input create-single-file-with-content create-single-file-with-content/input.json actual.output
diff actual.output ../shell/059_input_multilinestring/expected-augment.json
33 changes: 33 additions & 0 deletions tests/shell/059_input_multilinestring/example-cfbs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "Example",
"type": "policy-set",
"description": "Example description",
"git": false,
"build": [
{
"name": "create-single-file-with-content",
"description": "Create a single file with content.",
"steps": ["input ./input.json def.json"],
"input": [
{
"type": "string",
"variable": "filename",
"namespace": "cfbs",
"bundle": "create_single_file_with_content",
"label": "Filename",
"question": "What file should this module create?",
"default": "/tmp/create-single-file-with-content.txt"
},
{
"type": "string-multiline",
"variable": "content",
"namespace": "cfbs",
"bundle": "create_single_file_with_content",
"label": "Content",
"question": "What content should this file have?",
"default": "Hello CFEngine!\nBye CFEngine!"
}
]
}
]
}
12 changes: 12 additions & 0 deletions tests/shell/059_input_multilinestring/expected-augment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"variables": {
"cfbs:create_single_file_with_content.filename": {
"value": "/tmp/create-single-file-with-content.txt",
"comment": "Added by 'cfbs input'"
},
"cfbs:create_single_file_with_content.content": {
"value": "Hello CFEngine!\nBye CFEngine!",
"comment": "Added by 'cfbs input'"
}
}
}
1 change: 1 addition & 0 deletions tests/shell/all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ run_test tests/shell/055_render_input_two_variables.sh
run_test tests/shell/056_render_input_list.sh
run_test tests/shell/057_render_input_no_response.sh
run_test tests/shell/058_render_input_fail.sh
run_test tests/shell/059_input_multilinestring.sh

# Summary
_suite_end=$(date +%s)
Expand Down
Loading