aboutsummaryrefslogtreecommitdiff
path: root/debian-container/lib/profiles.sh
blob: 77deee7e86e6ed006dd00594946f8844261350c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env bash
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (C) 2026 by Richard Qin

DC_PROFILE_FILE=
DC_PROFILE_NETWORK=

dc_profile_parse() {
  local file=$1
  local line
  local key
  local value

  while IFS= read -r line || [[ -n "$line" ]]; do
    [[ -z "$line" || "$line" == \#* ]] && continue
    [[ "$line" == *=* ]] || dc_die "profile 配置格式无效:$line"
    key=${line%%=*}
    value=${line#*=}
    case "$key" in
      network)
        DC_PROFILE_NETWORK=$value
        ;;
      mount)
        dc_bind_add "$value"
        ;;
      *)
        dc_die "profile 配置项无效:$key"
        ;;
    esac
  done < "$file"
}

dc_profile_load() {
  DC_BIND_SPECS=()
  DC_BIND_SOURCES=()
  DC_BIND_TARGETS=()
  DC_BIND_MODES=()
  DC_PROFILE_FILE="$DC_INSTANCE_DIR/config"
  DC_PROFILE_NETWORK=$DC_NETWORK_MODE

  if [[ -f "$DC_PROFILE_FILE" ]]; then
    dc_profile_parse "$DC_PROFILE_FILE"
  fi
}

dc_profile_write_default() {
  local network_mode=$1

  printf 'network=%s\n' "$network_mode" > "$DC_PROFILE_FILE"
}