From 7fe223a91883cfdbbfb56907ef805b007f6e1ff0 Mon Sep 17 00:00:00 2001 From: Simon Halvorsen Date: Mon, 14 Sep 2026 15:56:18 +0200 Subject: [PATCH] Added `remove-input `-command Ticket: CFE-4139 Signed-off-by: Simon Halvorsen --- cfbs/commands.py | 49 +++++++++++++++++++ cfbs/main.py | 2 + tests/shell/065_remove_input_w_files.sh | 33 +++++++++++++ .../example-cfbs.json | 32 ++++++++++++ tests/shell/all.sh | 1 + 5 files changed, 117 insertions(+) create mode 100644 tests/shell/065_remove_input_w_files.sh create mode 100644 tests/shell/065_remove_input_w_files/example-cfbs.json diff --git a/cfbs/commands.py b/cfbs/commands.py index 21a66921..89cab96b 100644 --- a/cfbs/commands.py +++ b/cfbs/commands.py @@ -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): diff --git a/cfbs/main.py b/cfbs/main.py index f50fac06..78d012d8 100644 --- a/cfbs/main.py +++ b/cfbs/main.py @@ -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: diff --git a/tests/shell/065_remove_input_w_files.sh b/tests/shell/065_remove_input_w_files.sh new file mode 100644 index 00000000..a65228d7 --- /dev/null +++ b/tests/shell/065_remove_input_w_files.sh @@ -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 + diff --git a/tests/shell/065_remove_input_w_files/example-cfbs.json b/tests/shell/065_remove_input_w_files/example-cfbs.json new file mode 100644 index 00000000..b0fc64bb --- /dev/null +++ b/tests/shell/065_remove_input_w_files/example-cfbs.json @@ -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?" + } + ] + } + ] +} diff --git a/tests/shell/all.sh b/tests/shell/all.sh index 293dc030..cbc72f4a 100644 --- a/tests/shell/all.sh +++ b/tests/shell/all.sh @@ -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)