3

I need to read and modify a single file (pcmanfm.conf) in the current directory.

I tried

$ augtool -At "Toml.lns incl $(pwd)/pcmanfm.conf" -I lenses/ print /files
/files

but it does not work. toml.aug (on /usr/share/augeas/lenses/dist/toml.aug) starts with

(*
Module: Toml
  Parses TOML files

Author: Raphael Pinson <[email protected]>
...

so I believe I put the name of the lens correctly (Toml.lns).

The same setup works well if I parse file of a different type, e.g.

$ augtool -At "Shellvars.lns incl /tmp/vars.sh" -I lenses/ print /files
/files
/files/tmp
/files/tmp/vars.sh
/files/tmp/vars.sh/TESTINT = "2"
/files/tmp/vars.sh/TESTSTR = "\"FF\""

I've posted the same question on https://github.com/hercules-team/augeas/issues/699 in case it is a bug in Augeas.

The file I try to parse has the following content:

[config]
bm_open_method=0

[volume]
mount_on_startup=1
mount_removable=1
autorun=1

[ui]
always_show_tabs=0
max_tab_chars=32
win_width=1916
win_height=1149
splitter_pos=150
media_in_new_tab=0
desktop_folder_new_win=0
change_tab_on_drop=1
close_on_unmount=1
focus_previous=0
side_pane_mode=places
view_mode=compact
show_hidden=0
sort=name;ascending;
toolbar=newtab;navigation;home;
show_statusbar=1
pathbar_mode_buttons=0

I want to add/replace one value in the [ui] section.

2
  • The Toml lens for Augeas is not complete (not even sure it could possibly be). Can you paste the file you're trying to parse?
    – raphink
    Commented Oct 17, 2020 at 11:21
  • 1
    @ℝaphink I've pasted it. Commented Oct 17, 2020 at 12:45

2 Answers 2

1

The TOML specification specifies that string must be quoted (see https://toml.io/en/v1.0.0#string). A corrected variant of your file would look like

[config]
bm_open_method=0

[volume]
mount_on_startup=1
mount_removable=1
autorun=1

[ui]
always_show_tabs=0
max_tab_chars=32
win_width=1916
win_height=1149
splitter_pos=150
media_in_new_tab=0
desktop_folder_new_win=0
change_tab_on_drop=1
close_on_unmount=1
focus_previous=0
side_pane_mode="places"
view_mode="compact"
show_hidden=0
sort="name;ascending;"
toolbar="newtab;navigation;home;"
show_statusbar=1
pathbar_mode_buttons=0

You then want to replace a value in the ui section. Let's say you want to change view_mode from compact to forward leaning and that you have this value in a shell variable called vmode.

Then, using the tomlq tool from https://kislyuk.github.io/yq/,

$ vmode='forward leaning'
$ tomlq -t --arg vmode "$vmode" '.ui.view_mode |= $vmode' pcmanfm.conf
[config]
bm_open_method = 0

[volume]
mount_on_startup = 1
mount_removable = 1
autorun = 1

[ui]
always_show_tabs = 0
max_tab_chars = 32
win_width = 1916
win_height = 1149
splitter_pos = 150
media_in_new_tab = 0
desktop_folder_new_win = 0
change_tab_on_drop = 1
close_on_unmount = 1
focus_previous = 0
side_pane_mode = "places"
view_mode = "forward leaning"
show_hidden = 0
sort = "name;ascending;"
toolbar = "newtab;navigation;home;"
show_statusbar = 1
pathbar_mode_buttons = 0

The tomlq tool uses jq syntax to access and modify the document. The .ui.view_mode path is what we decided we wanted to change, and $vmode is the value that we are updating it to. This is an internal variable that we set to the same value as the shell variable of the same name on the command line using --arg.


For reference, the tomlq utility internally works with a JSON document created from your TOML document that looks like

{
  "config": {
    "bm_open_method": 0
  },
  "volume": {
    "mount_on_startup": 1,
    "mount_removable": 1,
    "autorun": 1
  },
  "ui": {
    "always_show_tabs": 0,
    "max_tab_chars": 32,
    "win_width": 1916,
    "win_height": 1149,
    "splitter_pos": 150,
    "media_in_new_tab": 0,
    "desktop_folder_new_win": 0,
    "change_tab_on_drop": 1,
    "close_on_unmount": 1,
    "focus_previous": 0,
    "side_pane_mode": "places",
    "view_mode": "compact",
    "show_hidden": 0,
    "sort": "name;ascending;",
    "toolbar": "newtab;navigation;home;",
    "show_statusbar": 1,
    "pathbar_mode_buttons": 0
  }
}
1

When Augeas fails to parse a file, you can use the augcheck script to figure out why:

./augcheck /tmp/test.toml Toml
/tmp/augcheck.h99O5K/parse_file.aug:3.0-.54:exception thrown in test
/tmp/augcheck.h99O5K/parse_file.aug:3.5-.50:exception: Syntax error
    Lens: /usr/share/augeas/lenses/dist/toml.aug:145.10-.45:
    Error encountered at 20:15 (290 characters into string)
    <s_previous=0\nside_pane_mode=|=|places\nview_mode=compact\nsho>

    Tree generated so far:
    

Syntax error in lens definition
Failed to load /tmp/augcheck.h99O5K/parse_file.aug

This indicates that Augeas failed to parse the line: side_pane_mode=planes. From looking at the lens, it seems the TOML lens currently only supports quoted strings.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .