eBPF-based security platform
bpm: list the subcommands of config, register, repo and tree
Daniel Borkmannc0f8c49567e9
diff --git a/builtin-bpm.c b/builtin-bpm.cindex 7c0a49fa..d1b6a831 100644--- a/builtin-bpm.c+++ b/builtin-bpm.c@@ -1503,7 +1503,20 @@ static const struct opt_spec bpm_register_list_opts[] = {"print the registered sources as JSON" },};+static const struct help_item bpm_config_subcmds[] = {+ { "register", "register, list and remove OCI repositories" },+ { "update", "build a config update package (operator)" },+};++static const struct help_item bpm_register_actions[] = {+ { "add", "register a source and its certificate" },+ { "list", "list the registered sources" },+ { "remove", "remove a source and its certificate" },+};+static const struct help_section bpm_config_sections[] = {+ HELP_ITEMS("subcommands:", bpm_config_subcmds),+ HELP_ITEMS("register actions:", bpm_register_actions),HELP_OPTIONS("register add options:", bpm_register_add_opts),HELP_OPTIONS("register list options:", bpm_register_list_opts),};@@ -1679,6 +1692,11 @@ static int cmd_bpm_config(int argc, const char **argv)print_help(&bpm_config_help);return argc < 2 ? EXIT_USAGE : 0;}+ if (is_list_cmds(argv[1])) {+ print_subcmd_list(bpm_config_subcmds,+ ARRAY_SIZE(bpm_config_subcmds));+ return 0;+ }if (strcmp(argv[1], "register")) {pr_err("bpm config: '%s' is not a subcommand", argv[1]);return EXIT_USAGE;@@ -1687,6 +1705,11 @@ static int cmd_bpm_config(int argc, const char **argv)print_help(&bpm_config_help);return action ? 0 : EXIT_USAGE;}+ if (is_list_cmds(action)) {+ print_subcmd_list(bpm_register_actions,+ ARRAY_SIZE(bpm_register_actions));+ return 0;+ }if (!strcmp(action, "list"))return bpm_register_list(argc, argv);if (!strcmp(action, "add"))@@ -3118,7 +3141,14 @@ static const struct opt_spec bpm_tree_sync_opts[] = {"write the JSON report there" },};+static const struct help_item bpm_tree_subcmds[] = {+ { "sync", "apply the configured tree on a target" },+ { "append", "publish a package's tree entries (operator)" },+ { "show", "print a verified publication (operator)" },+};+static const struct help_section bpm_tree_sections[] = {+ HELP_ITEMS("subcommands:", bpm_tree_subcmds),HELP_OPTIONS("sync options:", bpm_tree_sync_opts),};@@ -3146,10 +3176,20 @@ static const struct help bpm_pull_help = {" shirudo bpm pull oci://quay.io/namespace/package:tag",};+static const struct help_item bpm_repo_subcmds[] = {+ { "update", "refresh the signed index of a source, or of all" },+};++static const struct help_section bpm_repo_sections[] = {+ HELP_ITEMS("subcommands:", bpm_repo_subcmds),+};+static const struct help bpm_repo_help = {- .usage = "shirudo bpm repo update [<source>]",- .summary = "Refresh the signed index of one registered source, or of "- "all of them.",+ .usage = "shirudo bpm repo update [<source>]",+ .summary = "Refresh the signed index of one registered source, or "+ "of all of them.",+ .sections = bpm_repo_sections,+ .n_sections = ARRAY_SIZE(bpm_repo_sections),};static const struct help bpm_install_help = {@@ -3236,6 +3276,10 @@ static int bpm_tree_cmd(int argc, const char **argv)print_help(&bpm_tree_help);return argc < 3 ? EXIT_USAGE : 0;}+ if (is_list_cmds(argv[2])) {+ print_subcmd_list(bpm_tree_subcmds, ARRAY_SIZE(bpm_tree_subcmds));+ return 0;+ }if (!strcmp(argv[2], "append") || !strcmp(argv[2], "show"))return bpm_exec_operator("tree", argc, argv);if (strcmp(argv[2], "sync")) {@@ -3292,6 +3336,10 @@ static int bpm_repo_cmd(int argc, const char **argv)print_help(&bpm_repo_help);return argc < 3 ? EXIT_USAGE : 0;}+ if (is_list_cmds(argv[2])) {+ print_subcmd_list(bpm_repo_subcmds, ARRAY_SIZE(bpm_repo_subcmds));+ return 0;+ }if (strcmp(argv[2], "update")) {pr_err("bpm repo: '%s' is not a subcommand", argv[2]);return EXIT_USAGE;diff --git a/testing/functional/build/t1011-completion.sh b/testing/functional/build/t1011-completion.shnew file mode 100755index 00000000..4cbcc272--- /dev/null+++ b/testing/functional/build/t1011-completion.sh@@ -0,0 +1,62 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t1011 -- the bash completion completes from what the binary lists.++cd "$(dirname "$0")/../.."+. ./test-lib.sh++SHIRUDO=$(cd .. && pwd)/shirudo+COMPLETION=$(cd .. && pwd)/completion/shirudo.bash+W=$(mktemp -d) || exit 1+trap 'rm -rf "$W"' EXIT++# What the completion offers for the words given, the last the one being+# completed, one a line.+complete_words() {+ bash -c '+ . "$1"+ shift+ COMP_WORDS=("$@")+ COMP_CWORD=$(($# - 1))+ _shirudo+ printf "%s\n" "${COMPREPLY[@]}"+ ' complete "$COMPLETION" "$SHIRUDO" "$@"+}++test_expect_success 'commands and subcommands come from --list-cmds' '+ complete_words up >"$W/out" &&+ grep -qx upatch "$W/out" &&+ complete_words upatch lo >"$W/out" &&+ grep -qx load "$W/out" &&+ complete_words bpm config register "" >"$W/out" &&+ grep -qx add "$W/out" && grep -qx list "$W/out" &&+ grep -qx remove "$W/out"+'++test_expect_success 'options come from the command'"'"'s --list-opts' '+ complete_words upatch load --exp >"$W/out" &&+ grep -qx -- --expect-chain "$W/out" &&+ grep -qx -- --expect-version-id "$W/out" &&+ complete_words attest verify --ce >"$W/out" &&+ grep -qx -- --cert "$W/out" &&+ complete_words status -- >"$W/out" &&+ "$SHIRUDO" status --list-opts | cut -f1 | sort >"$W/want" &&+ sort "$W/out" | cmp -s - "$W/want"+'++test_expect_success 'a value gets what its placeholder asks for' '+ complete_words upatch map --js >"$W/out" &&+ grep -qx -- --json= "$W/out" &&+ mkdir "$W/keys" &&+ (cd "$W" && complete_words genkey --keys-dir k) >"$W/out" &&+ grep -qx keys "$W/out"+'++test_expect_success 'a leaf command lists no subcommands and logs nothing' '+ "$SHIRUDO" upatch load --list-cmds >"$W/out" 2>"$W/err" &&+ test ! -s "$W/out" && test ! -s "$W/err"+'++test_done