|
| 1 | +#!/bin/bash |
| 2 | +# Operacje na plikach konfiguracyjnych - noobs_lib modul |
| 3 | +[[ -n "${_NOOBS_CONFIG_LOADED:-}" ]] && return 0 |
| 4 | +readonly _NOOBS_CONFIG_LOADED=1 |
| 5 | + |
| 6 | +# Uzycie: config_set_value <file> <key> <value> [delimiter] |
| 7 | +config_set_value() { |
| 8 | + local file="$1" |
| 9 | + local key="$2" |
| 10 | + local value="$3" |
| 11 | + local delimiter="${4:-=}" |
| 12 | + |
| 13 | + [[ -z "$file" ]] && { msg_error "Nie podano pliku konfiguracyjnego."; return 1; } |
| 14 | + [[ -z "$key" ]] && { msg_error "Nie podano klucza."; return 1; } |
| 15 | + [[ ! -f "$file" ]] && { msg_error "Plik nie istnieje: $file"; return 1; } |
| 16 | + |
| 17 | + local key_esc value_esc delim_esc |
| 18 | + key_esc=$(printf '%s' "$key" | sed 's/[]\/$*.^[|]/\\&/g') |
| 19 | + value_esc=$(printf '%s' "$value" | sed 's/[\\&|]/\\&/g') |
| 20 | + delim_esc=$(printf '%s' "$delimiter" | sed 's/[]\/$*.^[|]/\\&/g') |
| 21 | + |
| 22 | + if grep -qE "^[#;]*[[:space:]]*${key_esc}[[:space:]]*${delim_esc}" "$file"; then |
| 23 | + sed -i "s|^[#;]*[[:space:]]*${key_esc}[[:space:]]*${delim_esc}.*|${key}${delimiter}${value_esc}|" "$file" |
| 24 | + msg_debug "Zmieniono: ${key}${delimiter}${value} w $file" |
| 25 | + else |
| 26 | + printf '%s%s%s\n' "$key" "$delimiter" "$value" >> "$file" |
| 27 | + msg_debug "Dodano: ${key}${delimiter}${value} do $file" |
| 28 | + fi |
| 29 | +} |
| 30 | + |
| 31 | +# Uzycie: config_append_if_missing <file> <line> |
| 32 | +config_append_if_missing() { |
| 33 | + local file="$1" |
| 34 | + local line="$2" |
| 35 | + |
| 36 | + [[ -z "$file" ]] && { msg_error "Nie podano pliku."; return 1; } |
| 37 | + [[ -z "$line" ]] && { msg_error "Nie podano linii."; return 1; } |
| 38 | + |
| 39 | + [[ ! -f "$file" ]] && touch "$file" |
| 40 | + |
| 41 | + if ! grep -qF "$line" "$file"; then |
| 42 | + echo "$line" >> "$file" |
| 43 | + msg_debug "Dodano do $file: $line" |
| 44 | + return 0 |
| 45 | + else |
| 46 | + msg_debug "Linia juz istnieje w $file" |
| 47 | + return 1 |
| 48 | + fi |
| 49 | +} |
| 50 | + |
| 51 | +# Uzycie: config_remove_line <file> <pattern> |
| 52 | +config_remove_line() { |
| 53 | + local file="$1" |
| 54 | + local pattern="$2" |
| 55 | + |
| 56 | + [[ -z "$file" ]] && { msg_error "Nie podano pliku."; return 1; } |
| 57 | + [[ -z "$pattern" ]] && { msg_error "Nie podano wzorca."; return 1; } |
| 58 | + [[ ! -f "$file" ]] && { msg_error "Plik nie istnieje: $file"; return 1; } |
| 59 | + |
| 60 | + sed -i "/${pattern}/d" "$file" |
| 61 | + msg_debug "Usunieto linie pasujace do '$pattern' z $file" |
| 62 | +} |
| 63 | + |
| 64 | +# Uzycie: config_comment_line <file> <pattern> [comment_char] |
| 65 | +config_comment_line() { |
| 66 | + local file="$1" |
| 67 | + local pattern="$2" |
| 68 | + local comment="${3:-#}" |
| 69 | + |
| 70 | + [[ ! -f "$file" ]] && { msg_error "Plik nie istnieje: $file"; return 1; } |
| 71 | + |
| 72 | + sed -i "s|^\(${pattern}.*\)|${comment}\1|" "$file" |
| 73 | +} |
| 74 | + |
| 75 | +# Uzycie: config_uncomment_line <file> <pattern> [comment_char] |
| 76 | +config_uncomment_line() { |
| 77 | + local file="$1" |
| 78 | + local pattern="$2" |
| 79 | + local comment="${3:-#}" |
| 80 | + |
| 81 | + [[ ! -f "$file" ]] && { msg_error "Plik nie istnieje: $file"; return 1; } |
| 82 | + |
| 83 | + sed -i "s|^${comment}\s*\(${pattern}.*\)|\1|" "$file" |
| 84 | +} |
| 85 | + |
| 86 | +# Uzycie: secure_file <file> [mode] |
| 87 | +secure_file() { |
| 88 | + local file="$1" |
| 89 | + local mode="${2:-600}" |
| 90 | + |
| 91 | + [[ -z "$file" ]] && { msg_error "Nie podano pliku."; return 1; } |
| 92 | + [[ ! -f "$file" ]] && { msg_error "Plik nie istnieje: $file"; return 1; } |
| 93 | + |
| 94 | + chown root:root "$file" |
| 95 | + chmod "$mode" "$file" |
| 96 | + msg_debug "Zabezpieczono plik: $file (mode: $mode)" |
| 97 | +} |
0 commit comments