properties-fileaugeas

Augeas: How to shorten tree paths?


My first Augeas script looks something like:

set /augeas/load/Properties/lens Properties.lns
set /augeas/load/Properties/incl /firstapp/WEB-INF/classes/some.properties
load
set /files/firstapp/WEB-INF/classes/some.properties/PROPERTY_1 "VALUE A"
set /files/firstapp/WEB-INF/classes/some.properties/PROPERTY_2 "VALUE B"
set /files/firstapp/WEB-INF/classes/some.properties/PROPERTY_3 "VALUE C"
save
set /augeas/load/Properties/lens Properties.lns
set /augeas/load/Properties/incl /secondapp/WEB-INF/classes/more.properties
load
set /files/secondapp/WEB-INF/classes/more.properties/PROPERTY_4 "VALUE D"
set /files/secondapp/WEB-INF/classes/more.properties/PROPERTY_5 "VALUE E"
set /files/secondapp/WEB-INF/classes/more.properties/PROPERTY_6 "VALUE F"
save

and I run it with e.g.:

augtool -LeAf adjust-properties.aug -r $WEB_SERVER_ROOT/

Now I wonder whether one could shorten the script by not repeating reoccuring path elements all the time.


Solution

According to this answer I can update my script to e.g.:

transform Properties.lns incl /firstapp/WEB-INF/classes/some.properties
transform Properties.lns incl /secondapp/WEB-INF/classes/more.properties
load
set /augeas/context /files/firstapp/WEB-INF/classes/some.properties
set PROPERTY_1 "VALUE A"
set PROPERTY_2 "VALUE B"
set PROPERTY_3 "VALUE C"
set /augeas/context /files/secondapp/WEB-INF/classes/more.properties
set PROPERTY_4 "VALUE D"
set PROPERTY_5 "VALUE E"
set PROPERTY_6 "VALUE F"
save

Solution

  • There's several things you can do.

    First, on recent augeas versions, you can use transform instead of the load commands.

    Then, you could set /augeas/context to use shorter, relative paths.

    Finally, you could declare variables using defvar and reuse them in your path expressions.