-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_cli_inventory.sh
More file actions
executable file
·139 lines (118 loc) · 4.83 KB
/
export_cli_inventory.sh
File metadata and controls
executable file
·139 lines (118 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env bash
# SPDX-License-Identifier: MPL-2.0
# =============================================================================
# NFTBan v1.0.0 - CLI Inventory Export Script
# =============================================================================
# meta:name="export_cli_inventory"
# meta:type="script"
# meta:version="1.0.0"
# meta:owner="Antonios Voulvoulis <contact@nftban.com>"
# meta:description="Exports all CLI commands with metadata to various formats"
# meta:inventory.files=""
# meta:inventory.binaries="bash,grep"
# meta:inventory.env_vars="NFTBAN_LIB_DIR"
# meta:inventory.config_files=""
# meta:inventory.systemd_units=""
# meta:inventory.network=""
# meta:inventory.privileges="none"
# =============================================================================
# Usage: ./export_cli_inventory.sh [format]
#
# Formats:
# table - Markdown table (default)
# json - JSON output
# list - Simple command list
# help - Export all help text
# =============================================================================
set -Eeuo pipefail
CLI_DIR="${NFTBAN_LIB_DIR:-/usr/lib/nftban}/cli"
FORMAT="${1:-table}"
# Fallback for development environment: resolve repo-relative cli/lib path
# from the script's own location so this works on any clone.
if [[ ! -d "$CLI_DIR" ]]; then
_self_dir="$(cd "$(dirname "$(readlink -f "$0")")" && pwd)"
CLI_DIR="${_self_dir}/../cli/lib/nftban/cli"
fi
if [[ ! -d "$CLI_DIR" ]]; then
echo "ERROR: CLI directory not found: $CLI_DIR" >&2
exit 1
fi
# Count commands
CMD_COUNT=$(ls "$CLI_DIR"/cmd_*.sh 2>/dev/null | wc -l)
case "$FORMAT" in
list)
# Simple command list
for cmd_file in "$CLI_DIR"/cmd_*.sh; do
basename "$cmd_file" .sh | sed 's/cmd_//' | sed 's/_/-/g'
done | sort
;;
json)
# JSON output
nftban_ver=$(cat "${BASH_SOURCE[0]%/*}/../VERSION" 2>/dev/null | tr -d '[:space:]' || echo "unknown")
echo "{"
echo ' "version": "'"${nftban_ver}"'",'
echo ' "generated": "'"$(date -Iseconds)"'",'
echo ' "total_commands": '$CMD_COUNT','
echo ' "commands": ['
first=true
for cmd_file in "$CLI_DIR"/cmd_*.sh; do
name=$(basename "$cmd_file" .sh | sed 's/cmd_//' | sed 's/_/-/g')
desc=$(grep -oP 'meta:description=\K.*' "$cmd_file" 2>/dev/null | head -1 || echo "")
ver=$(grep -oP 'meta:version=\K[0-9.]+' "$cmd_file" 2>/dev/null | head -1 || echo "unknown")
[[ "$first" != "true" ]] && echo ","
first=false
# Escape quotes in description
desc=$(echo "$desc" | sed 's/"/\\"/g')
printf ' {"name": "%s", "version": "%s", "description": "%s"}' \
"$name" "$ver" "$desc"
done
echo ""
echo " ]"
echo "}"
;;
help)
# Export all help text
OUTPUT_DIR="${2:-/tmp/nftban_help_export}"
mkdir -p "$OUTPUT_DIR"
echo "Exporting help text to: $OUTPUT_DIR"
echo ""
for cmd_file in "$CLI_DIR"/cmd_*.sh; do
cmd_name=$(basename "$cmd_file" .sh | sed 's/cmd_//' | sed 's/_/-/g')
echo " Exporting: nftban $cmd_name"
# Source and extract help
(
export NFTBAN_LIB_DIR="${NFTBAN_LIB_DIR:-/usr/lib/nftban}"
[[ -f /etc/nftban/nftban.conf ]] && source /etc/nftban/nftban.conf 2>/dev/null || true
# shellcheck source=/dev/null
source "$cmd_file" 2>/dev/null || true
# Try different help function patterns
func_name="${cmd_name//-/_}"
for func in "_nftban_${func_name}_help" "show_${func_name}_help" "show_usage"; do
if declare -f "$func" >/dev/null 2>&1; then
"$func" 2>/dev/null
exit 0
fi
done
echo "No help function found"
) > "$OUTPUT_DIR/${cmd_name}.txt" 2>/dev/null || echo " (error)"
done
echo ""
echo "Exported $CMD_COUNT commands to: $OUTPUT_DIR"
;;
table|*)
# Markdown table (default)
echo "# NFTBan CLI Commands"
echo ""
echo "**Total Commands:** $CMD_COUNT"
echo "**Generated:** $(date '+%Y-%m-%d %H:%M:%S')"
echo ""
echo "| Command | Version | Description |"
echo "|---------|---------|-------------|"
for cmd_file in "$CLI_DIR"/cmd_*.sh; do
name=$(basename "$cmd_file" .sh | sed 's/cmd_//' | sed 's/_/-/g')
desc=$(grep -oP 'meta:description=\K.*' "$cmd_file" 2>/dev/null | head -1 || echo "-")
ver=$(grep -oP 'meta:version=\K[0-9.]+' "$cmd_file" 2>/dev/null | head -1 || echo "unknown")
echo "| \`nftban $name\` | $ver | $desc |"
done | sort
;;
esac