Skip to content

add preservation of comment lines#20

Draft
wififreedom wants to merge 2 commits intoopenwrt:masterfrom
wififreedom:preserve_comments
Draft

add preservation of comment lines#20
wififreedom wants to merge 2 commits intoopenwrt:masterfrom
wififreedom:preserve_comments

Conversation

@wififreedom
Copy link
Copy Markdown

@wififreedom wififreedom commented Mar 31, 2026

I've implemented preservation of comments in the existing config file when doing one of uci commands 'add', 'delete', 'set', 'add_list', 'del_list', 'import' and 'export'.

Both comment lines before a non-comment (config/option/list) line and comments at the end of a non-comment line are preserved with the non-comment line. For example:

# comment before config line 1 is preserved
# comment before config line 2 is preserved
config interface 'myinterface' # comment after config is preserved
        # comment before option line 1 is preserved
        # comment before option line 2 is preserved
        option myoption 'myvalue' # comment after option is preserved
# this comment after all config/option/list lines is not preserved

uci command line commands 'set' (for adding config lines and option lines) and 'add_list' (for adding list lines) now also support setting and updating comment lines before and a comment after the config/option/list line. For example:

uci set "dhcp.lan=dhcp## comment after: config dhcp 'lan'"
uci set "dhcp.lan.interface=lan## comment after: option interface 'lan'"
uci add_list "dhcp.lan.ra_flags=managed_config## comment before line 1\\n# comment before line 2\\n#comment after: list ra_flags 'managed_config'"

results in:

config dhcp 'lan' # comment after: config dhcp 'lan'
        option interface 'lan' # comment after: option interface 'lan'
        # comment before line 1
        #comment before line 2
        list ra_flags 'managed_config' # comment after: list ra_flags 'managed_config'

Comment lines are auto-indented when preserved.

Limitations:

  • Comments at the end of the file are not preserved

@GeorgeSapkin
Copy link
Copy Markdown
Member

You'll need to look into the submission guidelines regarding commit formatting, etc.

@wififreedom
Copy link
Copy Markdown
Author

I will rebase with a proper commit message once I'm satisfied.

I'm interested in early reactions if this feature is even desirable.

@wififreedom
Copy link
Copy Markdown
Author

wififreedom commented Apr 2, 2026

Tests written.

@wififreedom
Copy link
Copy Markdown
Author

wififreedom commented Apr 2, 2026

The shunit2 test is broken, does not fail if one of the subtests fails, because exit code 1 is not propagated.

In tests/shunit2/tests.sh:

/bin/sh ${FULL_SUITE}

rm -rf ${TESTS_DIR}

should be:

/bin/sh ${FULL_SUITE}
EXITCODE=$?

rm -rf ${TESTS_DIR}

exit ${EXITCODE}

See #21

Features:
- uci now preserves comments in the existing config file for unaffected elements
when executing one of uci commands 'add', 'delete', 'set', 'add_list', and
'del_list'. For example:

  # comment before config line 1 is preserved
  # comment before config line 2 is preserved
  config interface 'myinterface' # comment after config is preserved
          # comment before option line 1 is preserved
          # comment before option line 2 is preserved
          option myoption 'myvalue' # comment after option is preserved
  # this comment after all config/option/list lines is not preserved

- Commands 'set' and 'add_list' now support setting comment lines
before and a comment after the config/option line. For example:

  uci set "dhcp.lan=dhcp## comment after: config dhcp 'lan'"
  uci set "dhcp.lan.interface=lan## comment after: option interface 'lan'"
  uci add_list "dhcp.lan.ra_flags=managed_config## line1\\n#line 2\\n#i after'"

  results in:

  config dhcp 'lan' # comment after: config dhcp 'lan'
          option interface 'lan' # comment after: option interface 'lan'
          # line 1
          #line 2
          list ra_flags 'managed_config' # after

- Command 'import' now also imports comments from the imported file into the
config file.

- Command 'output' now also exports comments from the config file to the
export result file.

- Comment lines are auto-indented when preserved.

Limitations:
- Comments at the end of the file are not preserved

Signed-off-by: Bastiaan Stougie <wififreedom2026@protonmail.com>
@wififreedom wififreedom marked this pull request as ready for review April 2, 2026 14:16
@wififreedom
Copy link
Copy Markdown
Author

wififreedom commented Apr 2, 2026

Additions i could implement:

  • DONE extend the 'get' command to optionally show the comments
  • extend the 'show' command to optionally also include the comments
  • allow working with comments (set, add_list, ...) in the lua wrapper (but I don't know anything about lua, let alone about programming a wrapper for lua)

@wififreedom wififreedom force-pushed the preserve_comments branch 3 times, most recently from 92f0747 to bf0a598 Compare April 3, 2026 11:21
@wififreedom wififreedom marked this pull request as draft April 4, 2026 09:06
Command 'get <config>.<section>##' prints escaped comment associated
with the section.

Command 'get <config>.<section>.<option>##' prints escaped comment associated
with the option.

Signed-off-by: Bastiaan Stougie <wififreedom2026@protonmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants