fix(bms): improve logging and response parsing
- bms-logging.sh: fixed brace expansion bug, JSON compaction, dynamic log path, single-line JSONL entries
- bms-tickets.sh: support {success:true,result:{...}} responses; fixed ID extraction for create/note; updated update fetch
Tested full lifecycle with logging: create → note → delete note → close → delete
This commit is contained in:
40
scripts/bms-logging.sh
Executable file → Normal file
40
scripts/bms-logging.sh
Executable file → Normal file
@@ -12,48 +12,52 @@ BMS_LOG_DIR="${BMS_LOG_DIR:-$HOME/.bms-actions}"
|
||||
# Ensure log directory exists
|
||||
mkdir -p "$BMS_LOG_DIR"
|
||||
|
||||
# Current log file (by date, UTC)
|
||||
BMS_LOG_FILE="$BMS_LOG_DIR/$(date -u +%Y-%m-%d).jsonl"
|
||||
# Compute log file dynamically based on current BMS_LOG_DIR
|
||||
|
||||
# Sanitize arguments: strip any sensitive values from a JSON object
|
||||
# Usage: sanitized=$(sanitize_args '{"password":"secret","token":"abc"}')
|
||||
sanitize_args() {
|
||||
local input="$1"
|
||||
# Remove known sensitive keys; preserve structure
|
||||
jq 'del(.["BMS_PASSWORD"], .["BMS_MFA_CODE"], .["BMS_CLIENT_SECRET"], .["access_token"], .["refresh_token"], .["token"], .["Authorization"])' 2>/dev/null <<<"$input" || echo "$input"
|
||||
# Remove known sensitive keys; preserve structure; output compact JSON to avoid newline issues
|
||||
jq -c 'del(.["BMS_PASSWORD"], .["BMS_MFA_CODE"], .["BMS_CLIENT_SECRET"], .["access_token"], .["refresh_token"], .["token"], .["Authorization"])' 2>/dev/null <<<"$input" || echo "$input"
|
||||
}
|
||||
|
||||
# Log an action
|
||||
# Arguments: command, args_json, result_json, status (success|error)
|
||||
log_action() {
|
||||
local command="$1"
|
||||
local args_json="${2:-{}}"
|
||||
local result_json="${3:-{}}"
|
||||
local args_json="${2:-{\}}"
|
||||
local result_json="${3:-{\}}"
|
||||
local status="${4:-success}"
|
||||
|
||||
# Ensure we have valid JSON; if pretty-printed, re-compact to a single line
|
||||
local args_compact result_compact
|
||||
args_compact=$(echo "$args_json" | jq -c . 2>/dev/null || echo "$args_json")
|
||||
result_compact=$(echo "$result_json" | jq -c . 2>/dev/null || echo "$result_json")
|
||||
|
||||
local timestamp
|
||||
timestamp=$(date -u +%Y-%m-%dT%H:%M:%SZ)
|
||||
|
||||
# Sanitize args and result
|
||||
local safe_args safe_result
|
||||
safe_args=$(sanitize_args "$args_json")
|
||||
safe_result=$(sanitize_args "$result_json")
|
||||
# Compute log file path dynamically
|
||||
local log_dir="${BMS_LOG_DIR:-$HOME/.bms-actions}"
|
||||
mkdir -p "$log_dir" 2>/dev/null
|
||||
local log_file="$log_dir/$(date -u +%Y-%m-%d).jsonl"
|
||||
|
||||
# Build log entry as single JSON line
|
||||
# Use --arg to pass JSON as string, then parse with fromjson inside jq
|
||||
local entry
|
||||
entry=$(jq -n \
|
||||
entry=$(jq -nc \
|
||||
--arg ts "$timestamp" \
|
||||
--arg cmd "$command" \
|
||||
--argjson args "$safe_args" \
|
||||
--argjson result "$safe_result" \
|
||||
--arg args "$args_compact" \
|
||||
--arg result "$result_compact" \
|
||||
--arg stat "$status" \
|
||||
'{timestamp: $ts, command: $cmd, args: $args, result: $result, status: $stat}')
|
||||
'{timestamp: $ts, command: $cmd, args: ($args|fromjson), result: ($result|fromjson), status: $stat}')
|
||||
|
||||
# Append atomically
|
||||
echo "$entry" >> "$BMS_LOG_FILE"
|
||||
echo "$entry" >> "$log_file"
|
||||
}
|
||||
|
||||
# Get current log file path
|
||||
get_log_path() {
|
||||
echo "$BMS_LOG_FILE"
|
||||
local log_dir="${BMS_LOG_DIR:-$HOME/.bms-actions}"
|
||||
echo "$log_dir/$(date -u +%Y-%m-%d).jsonl"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user