cloudflared-manager 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -160,6 +160,22 @@ dir_exists() {
160
160
  [[ -d "$1" ]]
161
161
  }
162
162
 
163
+ # 判断数组是否至少包含一个元素,兼容 macOS Bash 3 在 set -u 下的空数组行为。
164
+ array_has_items() {
165
+ local array_name="$1"
166
+ eval "[[ \${${array_name}[0]+_} == _ ]]"
167
+ }
168
+
169
+ # 返回数组长度,兼容 macOS Bash 3 在 set -u 下的空数组行为。
170
+ array_length() {
171
+ local array_name="$1"
172
+ if array_has_items "$array_name"; then
173
+ eval "printf '%s\n' \"\${#${array_name}[@]}\""
174
+ else
175
+ printf '0\n'
176
+ fi
177
+ }
178
+
163
179
  # 判断命令是否可用。
164
180
  command_exists() {
165
181
  command -v "$1" >/dev/null 2>&1
@@ -1777,6 +1793,18 @@ cmd_add() {
1777
1793
  local foreground=0
1778
1794
  local activate=0
1779
1795
 
1796
+ if [[ "$name" == "-h" || "$name" == "--help" ]]; then
1797
+ cat <<EOF
1798
+ 用法:
1799
+ $SCRIPT_NAME add NAME [--hostname HOST --service URL] [--ingress HOST=URL ...] [--tunnel-name NAME] [--use-existing] [--overwrite-dns] [--skip-check] [--start] [--foreground] [--activate]
1800
+
1801
+ 说明:
1802
+ - 兼容旧写法:使用一组 --hostname 和 --service
1803
+ - 多 ingress 写法:重复传 --ingress hostname=service
1804
+ - 可以混用,首条规则会作为“主入口”展示
1805
+ EOF
1806
+ return 0
1807
+ fi
1780
1808
  [[ -z "$name" ]] && die "add 需要 NAME"
1781
1809
  shift
1782
1810
 
@@ -1818,7 +1846,11 @@ EOF
1818
1846
  die "受管应用已存在: $name"
1819
1847
  fi
1820
1848
 
1821
- build_requested_ingress_rules "$hostname" "$service" "${ingress_specs[@]}"
1849
+ if array_has_items ingress_specs; then
1850
+ build_requested_ingress_rules "$hostname" "$service" "${ingress_specs[@]}"
1851
+ else
1852
+ build_requested_ingress_rules "$hostname" "$service"
1853
+ fi
1822
1854
  if [[ "$skip_check" == "0" ]]; then
1823
1855
  check_all_ingress_targets
1824
1856
  fi
@@ -1863,6 +1895,17 @@ cmd_adopt() {
1863
1895
  local skip_check=0
1864
1896
  local activate=0
1865
1897
 
1898
+ if [[ "$name" == "-h" || "$name" == "--help" ]]; then
1899
+ cat <<EOF
1900
+ 用法:
1901
+ $SCRIPT_NAME adopt NAME --tunnel-name NAME [--hostname HOST --service URL] [--ingress HOST=URL ...] [--ensure-dns] [--overwrite-dns] [--skip-check] [--activate]
1902
+
1903
+ 说明:
1904
+ - 至少要提供一条 ingress 规则
1905
+ - 如果远端 DNS 已经配好,可不传 --ensure-dns
1906
+ EOF
1907
+ return 0
1908
+ fi
1866
1909
  [[ -z "$name" ]] && die "adopt 需要 NAME"
1867
1910
  shift
1868
1911
 
@@ -1903,7 +1946,11 @@ EOF
1903
1946
  die "受管应用已存在: $name"
1904
1947
  fi
1905
1948
 
1906
- build_requested_ingress_rules "$hostname" "$service" "${ingress_specs[@]}"
1949
+ if array_has_items ingress_specs; then
1950
+ build_requested_ingress_rules "$hostname" "$service" "${ingress_specs[@]}"
1951
+ else
1952
+ build_requested_ingress_rules "$hostname" "$service"
1953
+ fi
1907
1954
  if [[ "$skip_check" == "0" ]]; then
1908
1955
  check_all_ingress_targets
1909
1956
  fi
@@ -2002,7 +2049,7 @@ EOF
2002
2049
  [[ -n "$pid" ]] && was_running=1
2003
2050
  fi
2004
2051
 
2005
- if [[ "${#modify_specs[@]}" -gt 0 ]]; then
2052
+ if array_has_items modify_specs; then
2006
2053
  if [[ -n "$new_hostname" || -n "$new_service" || "$index" != "1" ]]; then
2007
2054
  die "使用 --set 批量修改时,不要再混用 --index / --hostname / --service"
2008
2055
  fi
@@ -2052,7 +2099,7 @@ EOF
2052
2099
  i=$((i + 1))
2053
2100
  done
2054
2101
 
2055
- changed_count="${#changed_indices[@]}"
2102
+ changed_count="$(array_length changed_indices)"
2056
2103
  if [[ "$changed_count" == "0" ]]; then
2057
2104
  info "没有变更。"
2058
2105
  return 0
@@ -2132,13 +2179,13 @@ EOF
2132
2179
  if [[ "$was_running" == "1" && "$no_restart" == "0" ]]; then
2133
2180
  stop_app 0
2134
2181
  start_app 0 "$skip_check"
2135
- if [[ "${#modify_specs[@]}" -gt 0 ]]; then
2182
+ if array_has_items modify_specs; then
2136
2183
  info "已批量修改 ${changed_count} 条 ingress 并重启: $META_NAME"
2137
2184
  else
2138
2185
  info "已修改并重启: $META_NAME"
2139
2186
  fi
2140
2187
  else
2141
- if [[ "${#modify_specs[@]}" -gt 0 ]]; then
2188
+ if array_has_items modify_specs; then
2142
2189
  info "已批量修改 ingress 规则数量: ${changed_count}"
2143
2190
  else
2144
2191
  info "已修改: $META_NAME"
@@ -2226,18 +2273,20 @@ EOF
2226
2273
  requested_services+=("$(normalize_service "$service")")
2227
2274
  fi
2228
2275
 
2229
- for candidate_service in "${ingress_specs[@]}"; do
2230
- parse_ingress_spec "$candidate_service"
2231
- requested_hosts+=("$PARSED_INGRESS_HOSTNAME")
2232
- requested_services+=("$PARSED_INGRESS_SERVICE")
2233
- done
2276
+ if array_has_items ingress_specs; then
2277
+ for candidate_service in "${ingress_specs[@]}"; do
2278
+ parse_ingress_spec "$candidate_service"
2279
+ requested_hosts+=("$PARSED_INGRESS_HOSTNAME")
2280
+ requested_services+=("$PARSED_INGRESS_SERVICE")
2281
+ done
2282
+ fi
2234
2283
 
2235
- if [[ "${#requested_hosts[@]}" == "0" ]]; then
2284
+ if [[ "$(array_length requested_hosts)" == "0" ]]; then
2236
2285
  die "至少需要提供一条 ingress 规则。可使用 --hostname/--service,或重复传 --ingress hostname=service"
2237
2286
  fi
2238
2287
 
2239
2288
  i=0
2240
- while [[ "$i" -lt "${#requested_hosts[@]}" ]]; do
2289
+ while [[ "$i" -lt "$(array_length requested_hosts)" ]]; do
2241
2290
  candidate_hostname="${requested_hosts[$i]}"
2242
2291
  candidate_service="${requested_services[$i]}"
2243
2292
 
@@ -2340,7 +2389,7 @@ EOF
2340
2389
  esac
2341
2390
  done
2342
2391
 
2343
- if [[ "${#hostnames[@]}" == "0" && "${#indices[@]}" == "0" ]]; then
2392
+ if [[ "$(array_length hostnames)" == "0" && "$(array_length indices)" == "0" ]]; then
2344
2393
  die "ingress-remove 至少需要一个 --hostname 或 --index"
2345
2394
  fi
2346
2395
 
@@ -2359,21 +2408,25 @@ EOF
2359
2408
  i=$((i + 1))
2360
2409
  done
2361
2410
 
2362
- for hostname in "${hostnames[@]}"; do
2363
- hostname="$(normalize_hostname "$hostname")"
2364
- current_index="$(find_ingress_rule_index_by_hostname "$hostname" 2>/dev/null || true)"
2365
- [[ -n "$current_index" ]] || die "未找到要删除的 ingress 主机名: $hostname"
2366
- remove_flags[$current_index]=1
2367
- done
2411
+ if array_has_items hostnames; then
2412
+ for hostname in "${hostnames[@]}"; do
2413
+ hostname="$(normalize_hostname "$hostname")"
2414
+ current_index="$(find_ingress_rule_index_by_hostname "$hostname" 2>/dev/null || true)"
2415
+ [[ -n "$current_index" ]] || die "未找到要删除的 ingress 主机名: $hostname"
2416
+ remove_flags[$current_index]=1
2417
+ done
2418
+ fi
2368
2419
 
2369
- for index in "${indices[@]}"; do
2370
- [[ "$index" =~ ^[1-9][0-9]*$ ]] || die "--index 必须是大于等于 1 的整数"
2371
- current_index=$((index - 1))
2372
- if [[ "$current_index" -lt 0 || "$current_index" -ge "$total_rules" ]]; then
2373
- die "要删除的 ingress 序号超出范围: $index"
2374
- fi
2375
- remove_flags[$current_index]=1
2376
- done
2420
+ if array_has_items indices; then
2421
+ for index in "${indices[@]}"; do
2422
+ [[ "$index" =~ ^[1-9][0-9]*$ ]] || die "--index 必须是大于等于 1 的整数"
2423
+ current_index=$((index - 1))
2424
+ if [[ "$current_index" -lt 0 || "$current_index" -ge "$total_rules" ]]; then
2425
+ die "要删除的 ingress 序号超出范围: $index"
2426
+ fi
2427
+ remove_flags[$current_index]=1
2428
+ done
2429
+ fi
2377
2430
 
2378
2431
  i=0
2379
2432
  while [[ "$i" -lt "$total_rules" ]]; do
@@ -2390,7 +2443,7 @@ EOF
2390
2443
  if [[ "$remove_count" == "0" ]]; then
2391
2444
  die "未找到要删除的 ingress 规则。"
2392
2445
  fi
2393
- if [[ "${#remaining_hosts[@]}" == "0" ]]; then
2446
+ if [[ "$(array_length remaining_hosts)" == "0" ]]; then
2394
2447
  die "至少需要保留一条 ingress 规则。当前删除范围会把全部规则删空。"
2395
2448
  fi
2396
2449
 
@@ -2402,7 +2455,7 @@ EOF
2402
2455
  save_ingress_rules
2403
2456
  write_config 0
2404
2457
  save_meta
2405
- warn "已从配置中移除 ${#removed_hosts[@]} 条 ingress。Cloudflare 上旧 DNS 记录不会自动删除,如已弃用请手工清理。"
2458
+ warn "已从配置中移除 $(array_length removed_hosts) 条 ingress。Cloudflare 上旧 DNS 记录不会自动删除,如已弃用请手工清理。"
2406
2459
 
2407
2460
  if [[ "$activate" == "1" ]]; then
2408
2461
  activate_default_config
@@ -2412,7 +2465,7 @@ EOF
2412
2465
  start_app 0 0
2413
2466
  info "已移除 ingress 并重启: $META_NAME"
2414
2467
  else
2415
- info "已移除 ingress 规则数量: ${#removed_hosts[@]}"
2468
+ info "已移除 ingress 规则数量: $(array_length removed_hosts)"
2416
2469
  fi
2417
2470
  }
2418
2471
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cloudflared-manager",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Interactive wrapper and shell-first manager for cloudflared tunnels",
5
5
  "type": "module",
6
6
  "bin": {