/ lib / _ak_settings
_ak_settings
  1  #!/usr/bin/env bash
  2  ###
  3  ### arching-kaos-tools
  4  ### Tools to interact and build an Arching Kaos Infochain
  5  ### Copyright (C) 2021 - 2026  kaotisk
  6  ###
  7  ### This program is free software: you can redistribute it and/or modify
  8  ### it under the terms of the GNU General Public License as published by
  9  ### the Free Software Foundation, either version 3 of the License, or
 10  ### (at your option) any later version.
 11  ###
 12  ### This program is distributed in the hope that it will be useful,
 13  ### but WITHOUT ANY WARRANTY; without even the implied warranty of
 14  ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15  ### GNU General Public License for more details.
 16  ###
 17  ### You should have received a copy of the GNU General Public License
 18  ### along with this program.  If not, see <http://www.gnu.org/licenses/>.
 19  ###
 20  
 21  source $AK_LIBDIR/_ak_lib_load
 22  _ak_lib_load _ak_script
 23  
 24  if [ ! -d "${AK_WORKDIR}" ]
 25  then
 26      _ak_log_error "No workdir"
 27      exit 4
 28  fi
 29  
 30  export AK_SETTINGS="$AK_WORKDIR/settings"
 31  _ak_check_and_create_dir $AK_SETTINGS
 32  
 33  function _ak_settings_get(){
 34      if [ ! -z "$1" ] && [ -n "$1" ]
 35      then
 36          cd $AK_SETTINGS
 37          echo $1 | grep '\.' > /dev/null 2>&1
 38          if [ $? -ne 0 ]
 39          then
 40              _ak_log_error "No ungrouped settings allowed"
 41              exit 1
 42          fi
 43          echo $1 | grep '\.\.' > /dev/null 2>&1
 44          if [ $? -eq 0 ]
 45          then
 46              _ak_log_error "No '..' allowed"
 47              exit 1
 48          fi
 49          subset="$(echo $1 | cut -d '.' -f 1)"
 50          echo "$subset" | grep '[.\-\*/~!@#$%^&*()_=\-\>\<,{}[]]' > /dev/null 2>&1
 51          if [ $? -eq 0 ]
 52          then
 53              _ak_log_error "Names containing symbols are not allowed"
 54              exit 1
 55          fi
 56          if [ ! -d $subset ]
 57          then
 58              _ak_log_error "Subsetting $subset not found"
 59              exit 1
 60          fi
 61          cd $subset
 62          label="$(echo $1 | cut -d '.' -f 2-)"
 63          echo "$label" | grep '[.\-\*/~!@#$%^&*()_=\-\>\<,{}[]]' > /dev/null 2>&1
 64          if [ $? -eq 0 ]
 65          then
 66              _ak_log_error "Names containing symbols are not allowed"
 67              exit 1
 68          fi
 69          if [ ! -f $AK_SETTINGS/$subset/$label ]
 70          then
 71              _ak_log_error "Key: $subset.$label was not found"
 72              exit 1
 73          fi
 74          cat $AK_SETTINGS/$subset/$label
 75      else
 76          cd $AK_SETTINGS
 77          find . -type f | while read setting
 78          do
 79              printf '%s:%s\n' "$(echo $setting| sed -e 's/^\.\///;s/\//./g')" "$(cat $setting)"
 80          done
 81      fi
 82  }
 83  
 84  function _ak_settings_set(){
 85      if [ ! -z "$1" ] && [ -n "$1" ]
 86      then
 87          echo $1 | grep '\.\.' > /dev/null 2>&1
 88          if [ $? -eq 0 ]
 89          then
 90              _ak_log_error "No '..' allowed"
 91              exit 1
 92          fi
 93          if [ ! -z "$2" ] && [ -n "$2" ]
 94          then
 95              cd $AK_SETTINGS
 96              echo $1 | grep '\.' > /dev/null 2>&1
 97              if [ $? -ne 0 ]
 98              then
 99                  _ak_log_error "No ungrouped settings allowed"
100                  exit 1
101              fi
102              subset="$(echo $1 | cut -d '.' -f 1)"
103              echo "$subset" | grep '[.\-\*/~!@#$%^&*()_=\-\>\<,{}[]]' > /dev/null 2>&1
104              if [ $? -eq 0 ]
105              then
106                  _ak_log_error "Names containing symbols are not allowed"
107                  exit 1
108              fi
109              if [ ! -d $subset ]
110              then
111                  mkdir $subset
112                  if [ $? -ne 0 ]
113                  then
114                      _ak_log_error "Could not create $AK_SETTINGS/$subset"
115                      exit 1
116                  fi
117              fi
118              label="$(echo $1 | cut -d '.' -f 2-)"
119              echo "$label" | grep '[.\-\*/~!@#$%^&*()_=\-\>\<,{}[]]' > /dev/null 2>&1
120              if [ $? -eq 0 ]
121              then
122                  _ak_log_error "Names containing symbols are not allowed"
123                  exit 1
124              fi
125              if [ -f $AK_SETTINGS/$subset/$label ]
126              then
127                  _ak_log_warning "Overwritting $subset.$label:$(cat $AK_SETTINGS/$subset/$label) with $2"
128              fi
129              printf '%s' "$2" > $AK_SETTINGS/$subset/$label
130          else
131              _ak_log_error "No value provided for $1"
132          fi
133      else
134          _ak_log_error "No value provided to set"
135      fi
136  }
137  
138  function _ak_settings_get_sub(){
139      _ak_not_implemented ${FUNCNAME}
140  }
141  
142  function _ak_settings_get_all(){
143      _ak_not_implemented ${FUNCNAME}
144  }
145  
146  _ak_log_debug "_ak_settings loaded $(caller)"