gistfile1.txt
· 2.2 KiB · Text
Originalformat
function cmd::export::_full() {
local no_config="${1:-false}" no_peers="${2:-false}"
local sections=()
# Config
if ! $no_config; then
local config_json="{}"
[[ -f "$(ctx::config_file)" ]] && config_json=$(cmd::export::_compact_json "$(ctx::config_file)")
sections+=("\"config\":${config_json}")
fi
# Peers
if ! $no_peers; then
local -a peer_jsons=()
for conf in "$(ctx::clients)"/*.conf; do
[[ -f "$conf" ]] || continue
local pname
pname=$(basename "$conf" .conf)
local peer_export
peer_export=$(cmd::export::_peer_data "$pname")
[[ -n "$peer_export" ]] && peer_jsons+=("$peer_export")
done
local peers_array
peers_array=$(printf '%s\n' "${peer_jsons[@]:-}" | paste -sd ',' -)
sections+=("\"peers\":[${peers_array:-}]")
fi
# Rules — raw JSON files
local -a rule_jsons=()
for rule_file in "$(ctx::rules)"/*.rule "$(ctx::rules)/base"/*.rule; do
[[ -f "$rule_file" ]] || continue
rule_jsons+=("$(cmd::export::_compact_json "$rule_file")")
done
local rules_array
rules_array=$(printf '%s\n' "${rule_jsons[@]:-}" | paste -sd ',' -)
sections+=("\"rules\":[${rules_array:-}]")
# Identities
local -a id_jsons=()
for id_file in "$(ctx::identities)"/*.identity; do
[[ -f "$id_file" ]] || continue
id_jsons+=("$(cmd::export::_compact_json "$id_file")")
done
local ids_array
ids_array=$(printf '%s\n' "${id_jsons[@]:-}" | paste -sd ',' -)
sections+=("\"identities\":[${ids_array:-}]")
# Groups
local -a grp_jsons=()
for grp_file in "$(ctx::groups)"/*.group; do
[[ -f "$grp_file" ]] || continue
grp_jsons+=("$(cmd::export::_compact_json "$grp_file")")
done
local grps_array
grps_array=$(printf '%s\n' "${grp_jsons[@]:-}" | paste -sd ',' -)
sections+=("\"groups\":[${grps_array:-}]")
# Flat JSON files
[[ -f "$(ctx::policies)" ]] && sections+=("\"policies\":$(cat "$(ctx::policies)")")
[[ -f "$(ctx::subnets)" ]] && sections+=("\"subnets\":$(cat "$(ctx::subnets)")")
[[ -f "$(ctx::net)" ]] && sections+=("\"services\":$(cat "$(ctx::net)")")
[[ -f "$(ctx::hosts)" ]] && sections+=("\"hosts\":$(cat "$(ctx::hosts)")")
local data
data=$(printf '%s\n' "${sections[@]}" | paste -sd ',' -)
cmd::export::_envelope "full" "{${data}}"
}
| 1 | function cmd::export::_full() { |
| 2 | local no_config="${1:-false}" no_peers="${2:-false}" |
| 3 | |
| 4 | local sections=() |
| 5 | |
| 6 | # Config |
| 7 | if ! $no_config; then |
| 8 | local config_json="{}" |
| 9 | [[ -f "$(ctx::config_file)" ]] && config_json=$(cmd::export::_compact_json "$(ctx::config_file)") |
| 10 | sections+=("\"config\":${config_json}") |
| 11 | fi |
| 12 | |
| 13 | # Peers |
| 14 | if ! $no_peers; then |
| 15 | local -a peer_jsons=() |
| 16 | for conf in "$(ctx::clients)"/*.conf; do |
| 17 | [[ -f "$conf" ]] || continue |
| 18 | local pname |
| 19 | pname=$(basename "$conf" .conf) |
| 20 | local peer_export |
| 21 | peer_export=$(cmd::export::_peer_data "$pname") |
| 22 | [[ -n "$peer_export" ]] && peer_jsons+=("$peer_export") |
| 23 | done |
| 24 | local peers_array |
| 25 | peers_array=$(printf '%s\n' "${peer_jsons[@]:-}" | paste -sd ',' -) |
| 26 | sections+=("\"peers\":[${peers_array:-}]") |
| 27 | fi |
| 28 | |
| 29 | # Rules — raw JSON files |
| 30 | local -a rule_jsons=() |
| 31 | for rule_file in "$(ctx::rules)"/*.rule "$(ctx::rules)/base"/*.rule; do |
| 32 | [[ -f "$rule_file" ]] || continue |
| 33 | rule_jsons+=("$(cmd::export::_compact_json "$rule_file")") |
| 34 | done |
| 35 | local rules_array |
| 36 | rules_array=$(printf '%s\n' "${rule_jsons[@]:-}" | paste -sd ',' -) |
| 37 | sections+=("\"rules\":[${rules_array:-}]") |
| 38 | |
| 39 | # Identities |
| 40 | local -a id_jsons=() |
| 41 | for id_file in "$(ctx::identities)"/*.identity; do |
| 42 | [[ -f "$id_file" ]] || continue |
| 43 | id_jsons+=("$(cmd::export::_compact_json "$id_file")") |
| 44 | done |
| 45 | local ids_array |
| 46 | ids_array=$(printf '%s\n' "${id_jsons[@]:-}" | paste -sd ',' -) |
| 47 | sections+=("\"identities\":[${ids_array:-}]") |
| 48 | |
| 49 | # Groups |
| 50 | local -a grp_jsons=() |
| 51 | for grp_file in "$(ctx::groups)"/*.group; do |
| 52 | [[ -f "$grp_file" ]] || continue |
| 53 | grp_jsons+=("$(cmd::export::_compact_json "$grp_file")") |
| 54 | done |
| 55 | local grps_array |
| 56 | grps_array=$(printf '%s\n' "${grp_jsons[@]:-}" | paste -sd ',' -) |
| 57 | sections+=("\"groups\":[${grps_array:-}]") |
| 58 | |
| 59 | # Flat JSON files |
| 60 | [[ -f "$(ctx::policies)" ]] && sections+=("\"policies\":$(cat "$(ctx::policies)")") |
| 61 | [[ -f "$(ctx::subnets)" ]] && sections+=("\"subnets\":$(cat "$(ctx::subnets)")") |
| 62 | [[ -f "$(ctx::net)" ]] && sections+=("\"services\":$(cat "$(ctx::net)")") |
| 63 | [[ -f "$(ctx::hosts)" ]] && sections+=("\"hosts\":$(cat "$(ctx::hosts)")") |
| 64 | |
| 65 | local data |
| 66 | data=$(printf '%s\n' "${sections[@]}" | paste -sd ',' -) |
| 67 | cmd::export::_envelope "full" "{${data}}" |
| 68 | } |