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
49 changes: 49 additions & 0 deletions cfbs/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1854,6 +1854,55 @@ def _place(path):
return copied_files


@cfbs_command("remove-input")
@commit_after_command("Removed input for module%s", [PLURAL_S])
def remove_input_command(args, input_from="cfbs remove-input"):
config = CFBSConfig.get_instance()
validate_config_raise_exceptions(config, empty_build_list_ok=True)
do_commit = False
files_to_remove = []
for module_name in args:
module = config.get_module_from_build(module_name)
if not module:
print("Skipping module '%s', module not found" % module_name)
continue
if "input" not in module:
print("Skipping module '%s', no input exists" % module_name)
continue

input_path = os.path.join(".", module_name, "input.json")
removed_input_files = _remove_file_input(input_path)

files_to_remove.append(input_path)
files_to_remove.extend(removed_input_files)

for filepath in files_to_remove:
rm(filepath)

do_commit = True
config.save()
return CFBSCommandGitResult(0, do_commit, None, files_to_remove)


def _remove_file_input(input_path):
input_data = read_json(input_path) or []
files_to_remove = []
for value in input_data:
if value["type"] == "file":
files_to_remove.append(value["response"])
elif value["type"] == "list":
file_keys = [
sub["key"]
for sub in (value.get("subtype") or [])
if sub.get("type") == "file" and sub.get("key")
]
for row in value["response"]:
for key in file_keys:
if key in row:
files_to_remove.append(row[key])
return [os.path.join(".", f) for f in files_to_remove]


@cfbs_command("set-input")
@commit_after_command("Set input for module %s", [FIRST_ARG])
def set_input_command(name, infile):
Expand Down
2 changes: 2 additions & 0 deletions cfbs/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ def _main() -> int:
return commands.update_command(args.args)
if args.command == "input":
return commands.input_command(args.args)
if args.command == "remove-input":
return commands.remove_input_command(args.args)
if args.command in ("set-input", "get-input"):
filename = "stdin" if args.command == "set-input" else "stdout"
if len(args.args) <= 0:
Expand Down
33 changes: 33 additions & 0 deletions tests/shell/065_remove_input_w_files.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
set -e
set -x
cd tests/
mkdir -p ./tmp/
cd ./tmp/
rm -f cfbs.json
rm -rf .git
rm -rf copy-files
cp ../shell/065_remove_input_w_files/example-cfbs.json cfbs.json

srcdir=$(mktemp -d)
cleanup() {
rm -rf "$srcdir"
}
trap cleanup EXIT QUIT TERM

echo "one" > "$srcdir/one.txt"
echo "two" > "$srcdir/two.txt"

printf "$srcdir/one.txt\nyes\n$srcdir/two.txt\nno\n" | cfbs input copy-files

test -f copy-files/one.txt
test -f copy-files/two.txt
test -f copy-files/input.json

cfbs remove-input copy-files

if test -f copy-files/one.txt; then exit 1; fi
if test -f copy-files/two.txt; then exit 1; fi
if test -f copy-files/input.json; then exit 1; fi

rm -rf "$srcdir/one.txt" "$srcdir/two.txt" copy-files

32 changes: 32 additions & 0 deletions tests/shell/065_remove_input_w_files/example-cfbs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "Example",
"type": "policy-set",
"description": "Example description",
"git": false,
"build": [
{
"name": "copy-files",
"description": "Copy files, with options per file.",
"steps": ["input ./input.json def.json"],
"input": [
{
"type": "list",
"variable": "files",
"namespace": "cfbs",
"bundle": "copy_files",
"label": "Files",
"subtype": [
{
"key": "path",
"type": "file",
"label": "Path",
"question": "Which file should be copied?",
"filetype": [".txt", ".log"]
}
],
"while": "Do you want to copy another file?"
}
]
}
]
}
1 change: 1 addition & 0 deletions tests/shell/all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ run_test tests/shell/061_set_input_file.sh
run_test tests/shell/062_input_file_in_list_with_keys.sh
run_test tests/shell/063_input_string_multiline_in_list.sh
run_test tests/shell/064_input_file_check_mpf.sh
run_test tests/shell/065_remove_input_w_files.sh

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