-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparse-fossa-container-params.sh
More file actions
executable file
·181 lines (155 loc) · 5.92 KB
/
parse-fossa-container-params.sh
File metadata and controls
executable file
·181 lines (155 loc) · 5.92 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/bin/bash
set -euo pipefail
###############################################################################
# FOSSA Container Parameter Parser
#
# Reads fossa-container-params.json and builds CLI arguments from environment variables.
#
# Usage:
# source parse-fossa-container-params.sh
# build_fossa_args "container" "analyze"
# echo "$FOSSA_CLI_ARGS"
#
# Environment:
# FOSSA_PARAMS_CONFIG - Path to fossa-container-params.json
# CONTAINER_FOSSA_* - Various FOSSA container configuration variables
#
# Output:
# FOSSA_CLI_ARGS - Space-separated CLI arguments for FOSSA container commands
###############################################################################
# Default configuration file path
FOSSA_PARAMS_CONFIG="${FOSSA_PARAMS_CONFIG:-$(dirname "${BASH_SOURCE[0]}")/fossa-container-params.json}"
###############################################################################
# build_fossa_args [subcommand] [command]
#
# Reads JSON configuration and builds FOSSA CLI arguments from environment
# variables. Sets the FOSSA_CLI_ARGS variable with the result.
#
# Parameters:
# subcommand - FOSSA subcommand (e.g., "container")
# command - Optional FOSSA command to filter parameters by (e.g., "analyze", "test")
# If not provided, uses all parameters regardless of command.
#
# Returns:
# 0 on success, 1 on error
###############################################################################
build_fossa_args() {
local filter_subcommand="${1:-}"
local filter_command="${2:-}"
local config_file="$FOSSA_PARAMS_CONFIG"
# Validate config file exists
if [ ! -f "$config_file" ]; then
echo "❌ Error: FOSSA parameters config not found: $config_file" >&2
return 1
fi
# Validate JSON syntax
if ! jq empty "$config_file" 2>/dev/null; then
echo "❌ Error: Invalid JSON in $config_file" >&2
return 1
fi
if [ -n "$filter_subcommand" ] && [ -n "$filter_command" ]; then
echo "📋 Loading FOSSA '$filter_subcommand $filter_command' parameter mappings from: $config_file"
else
echo "📋 Loading FOSSA parameter mappings from: $config_file"
fi
# Initialize output variable
FOSSA_CLI_ARGS=""
# Parse JSON and process each parameter
local param_count
param_count=$(jq -r '.parameters | length' "$config_file")
for ((i=0; i<param_count; i++)); do
local env_var cli_flag param_type env_value commands
env_var=$(jq -r ".parameters[$i].env" "$config_file")
cli_flag=$(jq -r ".parameters[$i].flag" "$config_file")
param_type=$(jq -r ".parameters[$i].type" "$config_file")
commands=$(jq -r ".parameters[$i].commands | join(\",\")" "$config_file")
# Filter by command if specified
if [ -n "$filter_command" ]; then
# Check if this parameter supports the requested command
if ! echo "$commands" | grep -q "$filter_command"; then
continue # Skip this parameter
fi
fi
# Get the environment variable value
env_value="${!env_var:-}"
case "$param_type" in
flag)
# Boolean flag - only add if explicitly set to "true"
if [ "$env_value" == "true" ]; then
# Skip if this is an action-specific parameter (empty flag)
if [ -n "$cli_flag" ]; then
FOSSA_CLI_ARGS="$FOSSA_CLI_ARGS $cli_flag"
echo " ✓ Enabled: $cli_flag"
else
echo " ✓ Action parameter set: $env_var"
fi
fi
;;
value)
# Flag with value - only add if value is non-empty
if [ -n "$env_value" ]; then
# Skip if this is an action-specific parameter (empty flag)
if [ -n "$cli_flag" ]; then
FOSSA_CLI_ARGS="$FOSSA_CLI_ARGS $cli_flag $env_value"
echo " ✓ Using $cli_flag: $env_value"
else
echo " ✓ Action parameter set: $env_var=$env_value"
fi
fi
;;
image)
# Special type for container image - only set env var, not CLI arg
# The image is passed directly as a positional argument
if [ -n "$env_value" ]; then
echo " ✓ Container image: $env_value"
fi
;;
multi_value)
# Flag that can be specified multiple times with comma-separated values
if [ -n "$env_value" ]; then
# Split by comma and add each value separately
IFS=',' read -ra VALUES <<< "$env_value"
for val in "${VALUES[@]}"; do
# Trim whitespace
val=$(echo "$val" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
if [ -n "$val" ]; then
FOSSA_CLI_ARGS="$FOSSA_CLI_ARGS $cli_flag $val"
echo " ✓ Using $cli_flag: $val"
fi
done
fi
;;
*)
echo " ⚠️ Unknown parameter type '$param_type' for $env_var" >&2
;;
esac
done
# Trim leading whitespace
FOSSA_CLI_ARGS="${FOSSA_CLI_ARGS# }"
echo "✅ Built FOSSA CLI args: $FOSSA_CLI_ARGS"
# Export for use in calling scripts
export FOSSA_CLI_ARGS
return 0
}
###############################################################################
# print_fossa_config
#
# Pretty-prints the FOSSA parameter configuration for debugging.
###############################################################################
print_fossa_config() {
local config_file="$FOSSA_PARAMS_CONFIG"
if [ ! -f "$config_file" ]; then
echo "❌ Error: Config file not found: $config_file" >&2
return 1
fi
echo "📋 FOSSA Container Parameter Configuration"
echo "=========================================="
echo ""
jq -r '.parameters[] | " \(.env)\n Flag: \(.flag)\n Type: \(.type)\n Desc: \(.description)\n Example: \(.example)\n"' "$config_file"
}
###############################################################################
# If script is executed directly (not sourced), run build_fossa_args
###############################################################################
if [ "${BASH_SOURCE[0]}" == "${0}" ]; then
build_fossa_args "$@"
fi