Browse Source

Replace 'yunohost app setting' by a good old grep for more speed (close #5)

Julien VAUBOURG 9 years ago
parent
commit
3f75e96538
2 changed files with 26 additions and 57 deletions
  1. 14 45
      conf/ynh-torclient
  2. 12 12
      sources/controller.php

+ 14 - 45
conf/ynh-torclient

@@ -71,50 +71,19 @@ start_tor() {
 
 ## Tools
 
-moulinette_get() {
-  var=${1}
-  gotcha=0
+ynh_setting_get() {
+  app=${1}
+  setting=${2}
 
-  while [ "${gotcha}" -eq 0 ]; do
-    value=$(yunohost app setting torclient "${var}")
-
-    if [[ "${value}" =~ "An instance is already running" ]]; then
-      sleep $(($((RANDOM%5)) + 1))
-    else
-      gotcha=1
-    fi
-  done
-
-  echo "${value}"
-}
-
-moulinette_set() {
-  var=${1}
-  value=${2}
-
-  msg=$(yunohost app setting torclient "${var}" -v "${value}")
-
-  if [ ! $? -eq 0 ]; then
-    echo "${msg}" >&2
-    exit 1
-  fi
+  grep "^${setting}:" "/etc/yunohost/apps/${app}/settings.yml" | sed s/^[^:]\\+:\\s*[\"\']\\?// | sed s/\\s*[\"\']\$//
 }
 
-moulinette_hotspot_get() {
-  var=${1}
-  gotcha=0
-
-  while [ "${gotcha}" -eq 0 ]; do
-    value=$(yunohost app setting hotspot "${var}")
-
-    if [[ "${value}" =~ "An instance is already running" ]]; then
-      sleep $(($((RANDOM%5)) + 1))
-    else
-      gotcha=1
-    fi
-  done
+ynh_setting_set() {
+  app=${1}
+  setting=${2}
+  value=${3}
 
-  echo "${value}"
+  yunohost app setting "${app}" "${setting}" -v "${value}"
 }
 
 do_start() {
@@ -195,7 +164,7 @@ if [ "$1" != restart ]; then
     systemctl restart php5-fpm
   fi
 
-  ynh_wifi_device_id=$(moulinette_get wifi_device_id)
+  ynh_wifi_device_id=$(ynh_setting_get torclient wifi_device_id)
 
   if [[ ! "${1}" =~ stop ]]; then
     exitcode=0
@@ -212,18 +181,18 @@ if [ "$1" != restart ]; then
   
   echo -n "Retrieving Yunohost settings... "
   
-  ynh_service_enabled=$(moulinette_get service_enabled)
+  ynh_service_enabled=$(ynh_setting_get torclient service_enabled)
   
   if [ "${ynh_wifi_device_id}" -eq 0 ]; then
-    ynh_wifi_device=$(moulinette_hotspot_get wifi_device)
+    ynh_wifi_device=$(ynh_setting_get hotspot wifi_device)
   else
     ynh_wifi_device="hotspot${ynh_wifi_device_id}"
   fi
 
   echo OK
   
-  IFS='|' read -a ynh_wifi_ssid <<< "$(moulinette_hotspot_get wifi_ssid)"
-  IFS='|' read -a ynh_wifi_prefix <<< "$(moulinette_hotspot_get ip4_nat_prefix)"
+  IFS='|' read -a ynh_wifi_ssid <<< "$(ynh_setting_get hotspot wifi_ssid)"
+  IFS='|' read -a ynh_wifi_prefix <<< "$(ynh_setting_get hotspot ip4_nat_prefix)"
   ynh_wifi_prefix=${ynh_wifi_prefix[$ynh_wifi_device_id]}
   ynh_wifi_ssid=${ynh_wifi_ssid[$ynh_wifi_device_id]}
 fi

+ 12 - 12
sources/controller.php

@@ -19,16 +19,16 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-function moulinette_hotspot_get($var) {
-  return htmlspecialchars(exec('sudo yunohost app setting hotspot '.escapeshellarg($var)));
-}
+function ynh_setting_get($setting, $app = 'torclient') {
+  $value = exec("sudo grep \"^$setting:\" /etc/yunohost/apps/$app/settings.yml");
+  $value = preg_replace('/^[^:]+:\s*["\']?/', '', $value);
+  $value = preg_replace('/\s*["\']$/', '', $value);
 
-function moulinette_get($var) {
-  return htmlspecialchars(exec('sudo yunohost app setting torclient '.escapeshellarg($var)));
+  return htmlspecialchars($value);
 }
 
-function moulinette_set($var, $value) {
-  return exec('sudo yunohost app setting torclient '.escapeshellarg($var).' -v '.escapeshellarg($value));
+function ynh_setting_set($setting, $value) {
+  return exec('sudo yunohost app setting torclient '.escapeshellarg($setting).' -v '.escapeshellarg($value));
 }
 
 function stop_service() {
@@ -54,8 +54,8 @@ function service_faststatus() {
 }
 
 dispatch('/', function() {
-  $ssids = explode('|', moulinette_hotspot_get('wifi_ssid'));
-  $wifi_device_id = moulinette_get('wifi_device_id');
+  $ssids = explode('|', ynh_setting_get('wifi_ssid', 'hotspot'));
+  $wifi_device_id = ynh_setting_get('wifi_device_id');
   $wifi_ssid_list = '';
   $wifi_ssid = '';
 
@@ -71,7 +71,7 @@ dispatch('/', function() {
   }
 
   set('faststatus', service_faststatus() == 0);
-  set('service_enabled', moulinette_get('service_enabled'));
+  set('service_enabled', ynh_setting_get('service_enabled'));
   set('wifi_device_id', $wifi_device_id);
   set('wifi_ssid', $wifi_ssid);
   set('wifi_ssid_list', $wifi_ssid_list);
@@ -96,10 +96,10 @@ dispatch_put('/settings', function() {
 
   stop_service();
 
-  moulinette_set('service_enabled', $service_enabled);
+  ynh_setting_set('service_enabled', $service_enabled);
 
   if($service_enabled == 1) {
-    moulinette_set('wifi_device_id', $_POST['wifi_device_id']);
+    ynh_setting_set('wifi_device_id', $_POST['wifi_device_id']);
 
      $retcode = start_service();