Upgrade script : how to modify parameter inside configuration file

Hello,

While upgrading Cheky (https://github.com/YunoHost-Apps/cheky_ynh/tree/testing), I need to modify an existing configuration file and I do not know how to do this correctly
I need to do this so the user has nothing to do and it will work out of the box.

What I need to change

before : version = "3.8.1"
after : version = 4.1

Questions

  1. Have you ever need to do something like this ?
  2. As it is an upgrade to version 4.1 and for the next releases, I guess that it’s not nice to hardcode 4.1 in the upgrade script. Not elegant but good enough for a community app. If you have a clever idea, I take it :slight_smile: (Version can be found in this file https://github.com/Blount/Cheky/blob/master/version.php and is located in $final_path/version.php)

Proposals / solutions

  1. use plain sed ?
    1. https://stackoverflow.com/questions/20568515/how-to-use-sed-to-replace-a-config-files-variable
    2. https://stackoverflow.com/questions/5955548/how-do-i-use-sed-to-change-my-configuration-files-with-flexible-keys-and-values
  2. use ynh_replace_special_string ?

Hi Gofannon

you’re also supposed to have the version number in your manifest, which would be easier to get by using this helper.
Then the better way is to use ynh_replace_string which is using sed.
ynh_replace_special_string should be used only if you expect some special characters in the variable you’ll use. Like a password.

1 Like

Hello,

Thanks for the inputs. I thought that ynh_replace_string did not allow regex. Unfortunately, I am not fluent with regex and sed …

Do you have any example available somewhere? I wrote this test script and I think that will spend a lot of time on it to make it run … So, if you know something already, I will appreciate it :innocent:

#!/bin/bash

# Read the value of a key in a ynh manifest file
#
# usage: ynh_read_manifest manifest key
# | arg: manifest - Path of the manifest to read
# | arg: key - Name of the key to find
ynh_read_manifest () {
manifest="$1"
key="$2"
python3 -c "import sys, json;print(json.load(open('$manifest', encoding='utf-8'))['$key'])"
}

# Read the upstream version from the manifest 
# The version number in the manifest is defined by <upstreamversion>~ynh<packageversion>
# For example : 4.3-2~ynh3
# This include the number before ~ynh
# In the last example it return 4.3-2
#
# usage: ynh_app_upstream_version
ynh_app_upstream_version () {

    # HACK - Use a file in the same folder as the script
    manifest_path="./manifest.json"
    if [ ! -e "$manifest_path" ]; then
        manifest_path="../settings/manifest.json"       # Into the restore script, the manifest is not at the same place
    fi
    version_key=$(ynh_read_manifest "$manifest_path" "version")
    echo "${version_key/~ynh*/}"
}


echo "######"
echo "begin of serious stuff"

source /usr/share/yunohost/helpers
#ynh_replace_string "^(version[[:blank:]]*=[[:blank:]]*).*/\1toto/" "$upstream_version" "./test_config"
#ynh_replace_string "/^version[[:blank:]]*=[[:blank:]]*.*" "$upstream_version" "./test_config"
ynh_replace_string "/^version[[:blank:]]*=[[:blank:]]*.*" "toto" "./test_config"

echo "######"
echo $test_config
echo "END"

You don’t have to use regex here.
You have

version = "3.8.1"

And you want to change the version number.
You can simply do that:

ynh_replace_string "^version =.*" "version = \"$upstream_version\"" "./test_config"
1 Like