gistfile1.txt
· 4.0 KiB · Text
原始文件
function cmd::config::run() {
local subcmd="${1:-show}"
# If first arg is a flag, treat as 'show' subcommand
if [[ "$subcmd" == --* ]]; then
subcmd="show"
else
shift || true
fi
case "$subcmd" in
show) cmd::config::_show "$@" ;;
migrate) cmd::config::migrate "$@" ;;
help) cmd::config::help ;;
*)
log::error "Unknown subcommand: '${subcmd}'"
cmd::config::help
return 1
;;
esac
}
# ============================================
# Show
# ============================================
function cmd::config::_show() {
local name="" type=""
while [[ $# -gt 0 ]]; do
case "$1" in
--name) name="$2"; shift 2 ;;
--type) type="$2"; shift 2 ;;
--help) cmd::config::help; return ;;
*) log::error "Unknown flag: $1"; return 1 ;;
esac
done
[[ -z "$name" ]] && log::error "Missing required flag: --name" && return 1
name=$(peers::resolve_and_require "$name" "$type") || return 1
local conf
conf="$(ctx::clients)/${name}.conf"
log::section "Client Config: ${name}"
cat "$conf"
}
# ============================================
# Migrate
# ============================================
function cmd::config::migrate() {
local force=false dry_run=false
while [[ $# -gt 0 ]]; do
case "$1" in
--force) force=true; shift ;;
--dry-run) dry_run=true; shift ;;
--help) cmd::config::help; return ;;
*) log::error "Unknown flag: $1"; return 1 ;;
esac
done
local wgctl_dir
wgctl_dir="$(ctx::wgctl)"
local config_dir="${wgctl_dir}/config"
local data_dir="${wgctl_dir}/data"
local legacy_conf="${wgctl_dir}/wgctl.conf"
local json_conf="${config_dir}/wgctl.json"
# Check if already migrated
if [[ -f "$json_conf" && ! -f "$legacy_conf" ]]; then
log::wg_warning "Already migrated to new config structure"
return 0
fi
log::section "wgctl Config Migration"
printf "\n"
printf " This will:\n"
printf " 1. Create %s/config/ and %s/data/\n" "$wgctl_dir" "$wgctl_dir"
printf " 2. Convert wgctl.conf → wgctl.json\n"
printf " 3. Move data files to data/\n\n"
if ! $force && ! $dry_run; then
read -r -p " Proceed? [y/N] " confirm
case "$confirm" in [yY]*) ;; *) log::info "Aborted"; return 0 ;; esac
fi
local do=""
$dry_run && do="echo [dry-run]"
# 1. Create directories
$dry_run || mkdir -p "$config_dir" "$data_dir"
$dry_run && printf " Would create: %s/config/\n" "$wgctl_dir"
$dry_run && printf " Would create: %s/data/\n" "$wgctl_dir"
# 2. Convert wgctl.conf → wgctl.json
if [[ -f "$legacy_conf" ]]; then
if ! $dry_run; then
config::_convert_to_json "$legacy_conf" "$json_conf"
fi
printf " %s wgctl.conf → config/wgctl.json\n" "$($dry_run && echo '[dry-run]' || echo '✓')"
else
log::wg_warning "No wgctl.conf found — creating default wgctl.json"
$dry_run || config::_write_default_json "$json_conf"
fi
# 3. Move data files
local -a data_files=(
"hosts.json"
"services.json"
"subnets.json"
"policies.json"
)
local -a data_dirs=(
"rules"
"identities"
"groups"
"blocks"
"meta"
"peer-history"
)
for f in "${data_files[@]}"; do
if [[ -f "${wgctl_dir}/${f}" ]]; then
$dry_run || mv "${wgctl_dir}/${f}" "${data_dir}/${f}"
printf " %s %s → data/%s\n" \
"$($dry_run && echo '[dry-run]' || echo '✓')" "$f" "$f"
fi
done
for d in "${data_dirs[@]}"; do
if [[ -d "${wgctl_dir}/${d}" ]]; then
$dry_run || mv "${wgctl_dir}/${d}" "${data_dir}/${d}"
printf " %s %s/ → data/%s/\n" \
"$($dry_run && echo '[dry-run]' || echo '✓')" "$d" "$d"
fi
done
# 4. Remove legacy conf after successful migration
if ! $dry_run && [[ -f "$legacy_conf" ]]; then
mv "$legacy_conf" "${legacy_conf}.bak"
printf " ✓ wgctl.conf → wgctl.conf.bak (backup)\n"
fi
printf "\n"
$dry_run && log::wg_warning "Dry run — no changes made" \
|| log::wg_success "Migration complete"
}
| 1 | function cmd::config::run() { |
| 2 | local subcmd="${1:-show}" |
| 3 | |
| 4 | # If first arg is a flag, treat as 'show' subcommand |
| 5 | if [[ "$subcmd" == --* ]]; then |
| 6 | subcmd="show" |
| 7 | else |
| 8 | shift || true |
| 9 | fi |
| 10 | |
| 11 | case "$subcmd" in |
| 12 | show) cmd::config::_show "$@" ;; |
| 13 | migrate) cmd::config::migrate "$@" ;; |
| 14 | help) cmd::config::help ;; |
| 15 | *) |
| 16 | log::error "Unknown subcommand: '${subcmd}'" |
| 17 | cmd::config::help |
| 18 | return 1 |
| 19 | ;; |
| 20 | esac |
| 21 | } |
| 22 | |
| 23 | # ============================================ |
| 24 | # Show |
| 25 | # ============================================ |
| 26 | |
| 27 | function cmd::config::_show() { |
| 28 | local name="" type="" |
| 29 | while [[ $# -gt 0 ]]; do |
| 30 | case "$1" in |
| 31 | --name) name="$2"; shift 2 ;; |
| 32 | --type) type="$2"; shift 2 ;; |
| 33 | --help) cmd::config::help; return ;; |
| 34 | *) log::error "Unknown flag: $1"; return 1 ;; |
| 35 | esac |
| 36 | done |
| 37 | |
| 38 | [[ -z "$name" ]] && log::error "Missing required flag: --name" && return 1 |
| 39 | name=$(peers::resolve_and_require "$name" "$type") || return 1 |
| 40 | |
| 41 | local conf |
| 42 | conf="$(ctx::clients)/${name}.conf" |
| 43 | |
| 44 | log::section "Client Config: ${name}" |
| 45 | cat "$conf" |
| 46 | } |
| 47 | |
| 48 | # ============================================ |
| 49 | # Migrate |
| 50 | # ============================================ |
| 51 | |
| 52 | function cmd::config::migrate() { |
| 53 | local force=false dry_run=false |
| 54 | |
| 55 | while [[ $# -gt 0 ]]; do |
| 56 | case "$1" in |
| 57 | --force) force=true; shift ;; |
| 58 | --dry-run) dry_run=true; shift ;; |
| 59 | --help) cmd::config::help; return ;; |
| 60 | *) log::error "Unknown flag: $1"; return 1 ;; |
| 61 | esac |
| 62 | done |
| 63 | |
| 64 | local wgctl_dir |
| 65 | wgctl_dir="$(ctx::wgctl)" |
| 66 | local config_dir="${wgctl_dir}/config" |
| 67 | local data_dir="${wgctl_dir}/data" |
| 68 | local legacy_conf="${wgctl_dir}/wgctl.conf" |
| 69 | local json_conf="${config_dir}/wgctl.json" |
| 70 | |
| 71 | # Check if already migrated |
| 72 | if [[ -f "$json_conf" && ! -f "$legacy_conf" ]]; then |
| 73 | log::wg_warning "Already migrated to new config structure" |
| 74 | return 0 |
| 75 | fi |
| 76 | |
| 77 | log::section "wgctl Config Migration" |
| 78 | printf "\n" |
| 79 | printf " This will:\n" |
| 80 | printf " 1. Create %s/config/ and %s/data/\n" "$wgctl_dir" "$wgctl_dir" |
| 81 | printf " 2. Convert wgctl.conf → wgctl.json\n" |
| 82 | printf " 3. Move data files to data/\n\n" |
| 83 | |
| 84 | if ! $force && ! $dry_run; then |
| 85 | read -r -p " Proceed? [y/N] " confirm |
| 86 | case "$confirm" in [yY]*) ;; *) log::info "Aborted"; return 0 ;; esac |
| 87 | fi |
| 88 | |
| 89 | local do="" |
| 90 | $dry_run && do="echo [dry-run]" |
| 91 | |
| 92 | # 1. Create directories |
| 93 | $dry_run || mkdir -p "$config_dir" "$data_dir" |
| 94 | $dry_run && printf " Would create: %s/config/\n" "$wgctl_dir" |
| 95 | $dry_run && printf " Would create: %s/data/\n" "$wgctl_dir" |
| 96 | |
| 97 | # 2. Convert wgctl.conf → wgctl.json |
| 98 | if [[ -f "$legacy_conf" ]]; then |
| 99 | if ! $dry_run; then |
| 100 | config::_convert_to_json "$legacy_conf" "$json_conf" |
| 101 | fi |
| 102 | printf " %s wgctl.conf → config/wgctl.json\n" "$($dry_run && echo '[dry-run]' || echo '✓')" |
| 103 | else |
| 104 | log::wg_warning "No wgctl.conf found — creating default wgctl.json" |
| 105 | $dry_run || config::_write_default_json "$json_conf" |
| 106 | fi |
| 107 | |
| 108 | # 3. Move data files |
| 109 | local -a data_files=( |
| 110 | "hosts.json" |
| 111 | "services.json" |
| 112 | "subnets.json" |
| 113 | "policies.json" |
| 114 | ) |
| 115 | local -a data_dirs=( |
| 116 | "rules" |
| 117 | "identities" |
| 118 | "groups" |
| 119 | "blocks" |
| 120 | "meta" |
| 121 | "peer-history" |
| 122 | ) |
| 123 | |
| 124 | for f in "${data_files[@]}"; do |
| 125 | if [[ -f "${wgctl_dir}/${f}" ]]; then |
| 126 | $dry_run || mv "${wgctl_dir}/${f}" "${data_dir}/${f}" |
| 127 | printf " %s %s → data/%s\n" \ |
| 128 | "$($dry_run && echo '[dry-run]' || echo '✓')" "$f" "$f" |
| 129 | fi |
| 130 | done |
| 131 | |
| 132 | for d in "${data_dirs[@]}"; do |
| 133 | if [[ -d "${wgctl_dir}/${d}" ]]; then |
| 134 | $dry_run || mv "${wgctl_dir}/${d}" "${data_dir}/${d}" |
| 135 | printf " %s %s/ → data/%s/\n" \ |
| 136 | "$($dry_run && echo '[dry-run]' || echo '✓')" "$d" "$d" |
| 137 | fi |
| 138 | done |
| 139 | |
| 140 | # 4. Remove legacy conf after successful migration |
| 141 | if ! $dry_run && [[ -f "$legacy_conf" ]]; then |
| 142 | mv "$legacy_conf" "${legacy_conf}.bak" |
| 143 | printf " ✓ wgctl.conf → wgctl.conf.bak (backup)\n" |
| 144 | fi |
| 145 | |
| 146 | printf "\n" |
| 147 | $dry_run && log::wg_warning "Dry run — no changes made" \ |
| 148 | || log::wg_success "Migration complete" |
| 149 | } |