Skip to content

Commit 78a5ee3

Browse files
Licenserampcode-com
andcommitted
refactor: extract range_to_rfc3339 into shared scripts/lib-time
Removes duplicated ~35-line function from axiom-metrics-query and axiom-metrics-discover. Both now source scripts/lib-time instead. Addresses Cursor Bugbot review feedback on PR #6. Thread: https://ampcode.com/threads/T-019c50fb-ca56-7198-b6aa-7e14a4b01273 Co-authored-by: Amp <amp@ampcode.com> Amp-Thread-ID: https://ampcode.com/threads/T-019c50fb-ca56-7198-b6aa-7e14a4b01273
1 parent aad2754 commit 78a5ee3

File tree

3 files changed

+44
-74
lines changed

3 files changed

+44
-74
lines changed

scripts/axiom-metrics-discover

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -74,43 +74,8 @@ shift
7474

7575
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
7676

77-
# Compute RFC3339 start/end from --range if provided
78-
# range_to_rfc3339 converts a human range (e.g. 1h, 24h, 7d) to an RFC3339 timestamp that many seconds ago
79-
range_to_rfc3339() {
80-
local range="$1"
81-
local value="${range%[hdm]}"
82-
local suffix="${range: -1}"
83-
84-
if ! [[ "$value" =~ ^[0-9]+$ ]]; then
85-
echo "Error: Invalid range value '$range'. Expected number + suffix (h/d/m)." >&2
86-
return 1
87-
fi
88-
89-
case "$suffix" in
90-
h) local label="hour" ;;
91-
d) local label="day" ;;
92-
m) local label="minute" ;;
93-
*)
94-
echo "Error: Invalid range suffix '$suffix'. Use h (hours), d (days), or m (minutes)." >&2
95-
return 1
96-
;;
97-
esac
98-
99-
# Try GNU date first (linux, or gdate on macOS), then fall back to macOS date
100-
if date -u -d "1 hour ago" +%Y-%m-%dT%H:%M:%SZ &>/dev/null; then
101-
# GNU date
102-
date -u -d "$value $label ago" +%Y-%m-%dT%H:%M:%SZ
103-
else
104-
# macOS date: -v flag with uppercase suffix
105-
local date_flag
106-
case "$suffix" in
107-
h) date_flag="-v-${value}H" ;;
108-
d) date_flag="-v-${value}d" ;;
109-
m) date_flag="-v-${value}M" ;;
110-
esac
111-
date -u "$date_flag" +%Y-%m-%dT%H:%M:%SZ
112-
fi
113-
}
77+
# shellcheck disable=SC1091
78+
source "$SCRIPT_DIR/lib-time"
11479

11580
# Validate time arguments
11681
if [[ -n "$RANGE" && ( -n "$START_TIME" || -n "$END_TIME" ) ]]; then

scripts/axiom-metrics-query

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -104,43 +104,8 @@ if [[ -t 0 ]]; then
104104
exit 1
105105
fi
106106

107-
# Compute RFC3339 start/end from --range if provided
108-
# range_to_rfc3339 converts a human range (e.g. 1h, 24h, 7d) to an RFC3339 timestamp that many seconds ago
109-
range_to_rfc3339() {
110-
local range="$1"
111-
local value="${range%[hdm]}"
112-
local suffix="${range: -1}"
113-
114-
if ! [[ "$value" =~ ^[0-9]+$ ]]; then
115-
echo "Error: Invalid range value '$range'. Expected number + suffix (h/d/m)." >&2
116-
return 1
117-
fi
118-
119-
case "$suffix" in
120-
h) local label="hour" ;;
121-
d) local label="day" ;;
122-
m) local label="minute" ;;
123-
*)
124-
echo "Error: Invalid range suffix '$suffix'. Use h (hours), d (days), or m (minutes)." >&2
125-
return 1
126-
;;
127-
esac
128-
129-
# Try GNU date first (linux, or gdate on macOS), then fall back to macOS date
130-
if date -u -d "1 hour ago" +%Y-%m-%dT%H:%M:%SZ &>/dev/null; then
131-
# GNU date
132-
date -u -d "$value $label ago" +%Y-%m-%dT%H:%M:%SZ
133-
else
134-
# macOS date: -v flag with uppercase suffix
135-
local date_flag
136-
case "$suffix" in
137-
h) date_flag="-v-${value}H" ;;
138-
d) date_flag="-v-${value}d" ;;
139-
m) date_flag="-v-${value}M" ;;
140-
esac
141-
date -u "$date_flag" +%Y-%m-%dT%H:%M:%SZ
142-
fi
143-
}
107+
# shellcheck disable=SC1091
108+
source "$SCRIPT_DIR/lib-time"
144109

145110
# Validate time arguments
146111
if [[ -n "$RANGE" && ( -n "$START_TIME" || -n "$END_TIME" ) ]]; then

scripts/lib-time

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env bash
2+
# Shared time utilities for Gilfoyle scripts
3+
# Source this file: source "$SCRIPT_DIR/lib-time"
4+
5+
# range_to_rfc3339 converts a human range (e.g. 1h, 24h, 7d) to an RFC3339 timestamp that many seconds ago
6+
range_to_rfc3339() {
7+
local range="$1"
8+
local value="${range%[hdm]}"
9+
local suffix="${range: -1}"
10+
11+
if ! [[ "$value" =~ ^[0-9]+$ ]]; then
12+
echo "Error: Invalid range value '$range'. Expected number + suffix (h/d/m)." >&2
13+
return 1
14+
fi
15+
16+
case "$suffix" in
17+
h) local label="hour" ;;
18+
d) local label="day" ;;
19+
m) local label="minute" ;;
20+
*)
21+
echo "Error: Invalid range suffix '$suffix'. Use h (hours), d (days), or m (minutes)." >&2
22+
return 1
23+
;;
24+
esac
25+
26+
# Try GNU date first (linux, or gdate on macOS), then fall back to macOS date
27+
if date -u -d "1 hour ago" +%Y-%m-%dT%H:%M:%SZ &>/dev/null; then
28+
# GNU date
29+
date -u -d "$value $label ago" +%Y-%m-%dT%H:%M:%SZ
30+
else
31+
# macOS date: -v flag with uppercase suffix
32+
local date_flag
33+
case "$suffix" in
34+
h) date_flag="-v-${value}H" ;;
35+
d) date_flag="-v-${value}d" ;;
36+
m) date_flag="-v-${value}M" ;;
37+
esac
38+
date -u "$date_flag" +%Y-%m-%dT%H:%M:%SZ
39+
fi
40+
}

0 commit comments

Comments
 (0)