Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

easily add/remove values to existing tedge config properties of an array type #2864

Closed
reubenmiller opened this issue May 8, 2024 · 1 comment
Assignees
Labels
idea ideas/opportunities/feature requests which need to be further investigated before implementation theme:cli Theme: cli related topics theme:configuration Theme: Configuration management
Milestone

Comments

@reubenmiller
Copy link
Contributor

reubenmiller commented May 8, 2024

Is your feature request related to a problem? Please describe.

Editing tedge config values which are arrays is difficult to managed for users as it involves checking the initial value, and adding the new value only if it is not already included in the list. Most of the time the "thing" adding the new value does not care about the existing values, and just wants to make sure the value it cares about is added to the list without breaking any other components.

There are currently the following tedge config settings which expect an array:

$ tedge --config-dir /opt/homebrew/etc/tedge config list --all | grep '\['
c8y.smartrest.templates=["collection1", "collection2"]
c8y.topics=["te/+/+/+/+", "te/+/+/+/+/twin/+", "te/+/+/+/+/m/+", "te/+/+/+/+/e/+", "te/+/+/+/+/a/+", "te/+/+/+/+/status/health"]
az.topics=["te/+/+/+/+/m/+", "te/+/+/+/+/e/+", "te/+/+/+/+/a/+", "te/+/+/+/+/status/health"]
aws.topics=["te/+/+/+/+/m/+", "te/+/+/+/+/e/+", "te/+/+/+/+/a/+", "te/+/+/+/+/status/health"]

The problem can be demonstrated via the tedge config c8y.smartrest.templates setting.

  1. Package 1 includes an operation handler which requires subscribing to a SmartREST template called collection1

    tedge config set c8y.smartrest.templates "collection1"

    After adding the collection1 template, the configuration get be retrieved using:

    $ tedge config get c8y.smartrest.templates
    ["collection1"]
  2. Package 2 includes another operation handlers which requires subscribing to a different SmartREST template called collection2

    The package can't just run the following command, as it would overwrite the existing value from the previous step, so it requires more complex logic using jq to splice in a new value (below adds the new values, and then removes any duplicated template names)

    SMARTREST_TEMPLATE_NAME=collection2
    
    NEW_VALUE=$(tedge config get c8y.smartrest.templates | jq -r ". += [\"$SMARTREST_TEMPLATE_NAME\"]| unique | @tsv" | tr '\t' ',')
    tedge config set c8y.smartrest.templates "$NEW_VALUE"

    Note If the user would not have jq then the logic was be more complex (and naive):

    SMARTREST_TEMPLATE_NAME=collection2
    
    EXISTING_VALUES=$(tedge --config-dir /opt/homebrew/etc/tedge config get c8y.smartrest.templates | tr -d '[]"' | sed 's/, /,/g')
    if [ -n "$EXISTING_VALUES" ]; then
        NEW_VALUE=$(echo "$EXISTING_VALUES,$SMARTREST_TEMPLATE_NAME" | sort | uniq)
    else
        NEW_VALUE="$SMARTREST_TEMPLATE_NAME"
    fi
    tedge config set c8y.smartrest.templates "$NEW_VALUE"

Describe the solution you'd like

Add new subcommands for tedge config to "add" and "remove" values from an array.

Below shows an example of some of the commands (and the result of the setting afterwards)

tedge config set c8y.smartrest.templates collection1
# Afterwards: ["collection1"]

# Add new value to the existing array
tedge config add c8y.smartrest.templates collection2
# Afterwards: ["collection1","collection2"]

# Only add the value if it does not already exist
tedge config add c8y.smartrest.templates collection2
# Afterwards: ["collection1","collection2"]

# Add multiple new values to the existing array
tedge config add c8y.smartrest.templates collection3,collection4
# Afterwards: ["collection1","collection2","collection3","collection4"]

# Remove value from existing template array
tedge config remove c8y.smartrest.templates collection3
# Afterwards: ["collection1","collection2","collection4"]

tedge config add {prop} {value}

  • Append the given value to the existing items in the array (and if the value does not already existing in the array)
  • If the value is already in the array, then do nothing and exit with a status code 0 (as the command goal is already achieved)
  • If used on a setting which is not an array, replace the value (e.g. on non-array properties, it should behave like tedge config set)

tedge config remove {prop} {value}

  • Remove the given value to the existing items in the array (if the value occurs more than once, then all values should be removed)
  • If the value is NOT in the array, then do nothing and exit with a status code 0 (as the command goal is already achieved)
  • If used on a setting which is not an array, unset the property (e.g. on non-array properties, it should behave like tedge config unset)

Describe alternatives you've considered

Additional context

@reubenmiller reubenmiller added idea ideas/opportunities/feature requests which need to be further investigated before implementation theme:configuration Theme: Configuration management theme:cli Theme: cli related topics labels May 8, 2024
@gligorisaev
Copy link
Contributor

QA troughly check was done during the development/implementation of this feature. Suggested steps to be added in the test case are done properly the review was done in:
#2943

@reubenmiller reubenmiller added this to the 1.2.0 milestone Jul 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
idea ideas/opportunities/feature requests which need to be further investigated before implementation theme:cli Theme: cli related topics theme:configuration Theme: Configuration management
Projects
None yet
Development

No branches or pull requests

3 participants