Skip to content

Commit 910851e

Browse files
committed
Harden utility script args and health-check HTTP handling
1 parent a81cf1b commit 910851e

4 files changed

Lines changed: 36 additions & 13 deletions

File tree

scripts/franchise-sort.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
python3 franchise-sort.py # uses env vars
1313
PLEX_URL=http://localhost:32400 PLEX_TOKEN=xxx python3 franchise-sort.py
1414
python3 franchise-sort.py --url http://localhost:32400 --token xxx
15+
python3 franchise-sort.py --path ~/Media/logs # custom log dir
1516
python3 franchise-sort.py --section 2 # TV library
1617
python3 franchise-sort.py --dry-run # preview only
1718
"""
@@ -34,18 +35,24 @@ def parse_args():
3435
global PLEX_URL, PLEX_TOKEN, SECTION_ID, LOG_DIR, DRY_RUN
3536
args = sys.argv[1:]
3637
i = 0
38+
def require_arg(flag: str) -> str:
39+
nonlocal i
40+
if i + 1 >= len(args) or args[i + 1].startswith("--"):
41+
print(f"Missing value for {flag}")
42+
sys.exit(1)
43+
return args[i + 1]
3744
while i < len(args):
38-
if args[i] == "--url" and i + 1 < len(args):
39-
PLEX_URL = args[i + 1]
45+
if args[i] == "--url":
46+
PLEX_URL = require_arg("--url")
4047
i += 2
41-
elif args[i] == "--token" and i + 1 < len(args):
42-
PLEX_TOKEN = args[i + 1]
48+
elif args[i] == "--token":
49+
PLEX_TOKEN = require_arg("--token")
4350
i += 2
44-
elif args[i] == "--section" and i + 1 < len(args):
45-
SECTION_ID = args[i + 1]
51+
elif args[i] == "--section":
52+
SECTION_ID = require_arg("--section")
4653
i += 2
47-
elif args[i] == "--log-dir" and i + 1 < len(args):
48-
LOG_DIR = args[i + 1]
54+
elif args[i] in ("--log-dir", "--path"):
55+
LOG_DIR = require_arg(args[i])
4956
i += 2
5057
elif args[i] == "--dry-run":
5158
DRY_RUN = True

scripts/health-check.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ FAIL=0
1313
check_service() {
1414
local name="$1" url="$2"
1515
status=$(curl -s -o /dev/null -w "%{http_code}" --max-time 5 "$url" 2>/dev/null)
16-
if [[ "$status" =~ ^(200|301|302)$ ]]; then
16+
if [[ "$status" =~ ^[23][0-9][0-9]$ ]]; then
1717
echo -e " ${GREEN}OK${NC} $name"
1818
((PASS++))
1919
else

scripts/log-prune.sh

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,20 @@ usage() {
1818
exit 0
1919
}
2020

21+
require_arg() {
22+
local flag="$1"
23+
local value="${2:-}"
24+
if [[ -z "$value" || "$value" == --* ]]; then
25+
echo "Missing value for $flag" >&2
26+
exit 1
27+
fi
28+
echo "$value"
29+
}
30+
2131
while [[ $# -gt 0 ]]; do
2232
case "$1" in
23-
--path) MEDIA_DIR="$2"; shift 2 ;;
24-
--days) RETENTION_DAYS="$2"; shift 2 ;;
33+
--path) MEDIA_DIR="$(require_arg --path "${2:-}")"; shift 2 ;;
34+
--days) RETENTION_DAYS="$(require_arg --days "${2:-}")"; shift 2 ;;
2535
-h|--help) usage ;;
2636
*) echo "Unknown option: $1"; exit 1 ;;
2737
esac

scripts/music-cleanup.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,18 @@ def parse_args():
4444
global MUSIC_ROOT, DRY_RUN
4545
args = sys.argv[1:]
4646
i = 0
47+
def require_arg(flag: str) -> str:
48+
nonlocal i
49+
if i + 1 >= len(args) or args[i + 1].startswith("--"):
50+
print(f"Missing value for {flag}")
51+
sys.exit(1)
52+
return args[i + 1]
4753
while i < len(args):
4854
if args[i] == "--apply":
4955
DRY_RUN = False
5056
i += 1
51-
elif args[i] == "--path" and i + 1 < len(args):
52-
MUSIC_ROOT = args[i + 1]
57+
elif args[i] == "--path":
58+
MUSIC_ROOT = require_arg("--path")
5359
i += 2
5460
elif args[i] in ("-h", "--help"):
5561
print(__doc__.strip())

0 commit comments

Comments
 (0)