[augeas-devel] How to select nodes in distinct position, possibly creating them?

Raphaël Pinson raphael.pinson at camptocamp.com
Fri Feb 12 17:45:01 UTC 2016


Hi Xavier,


There is not easy way using augtool. When using the Augeas API (in any
language), you can easily check for the presence of a node and decide if
you need to create it or not. In augtool, you can't do that (yet, until the
Lua code is merged). There is a little trick you can use though, using
defvar:


# define $nomatch as /t if it doesn't have an "A" node
augtool> defvar nomatch /t[count(A)=0]
# insert using $nomatch, which may not be defined
augtool> ins A before $nomatch/B
# You are sure it exists now
augtool> set /t/A "value"


Raphaël


On Fri, Feb 12, 2016 at 4:13 PM, Mol, Xavier (SCC) <xavier.mol at kit.edu>
wrote:

> Hello Augeas experts,
>
> I'm looking for a way to select a node in an Augeas tree in a distinct
> position, which isn't difficult, but I also want to create that node in
> case it doesn't exist yet, which seems impossible to me as of now. An
> example probably illustrates better what I mean...
>
> augtool> touch /t/#comment
> augtool> ins "" after /t/*[last()]
> augtool> touch /t/B
> augtool> touch /t/C
> augtool> print /t
> /t
> /t/#comment
> /t/
> /t/B
> /t/C
>
>
> What can I do now to make a node "A" appear right in front of "B"? The
> straight forward answer is, like this:
>
> insert "A" before "B"
>
> That works fine as long as there is no "A" node already, which I can't
> know beforehand. I could guarantee that there is no such node, by deleting
> it first (this very trick was taken from the Augeas wiki:
> https://github.com/hercules-team/augeas/wiki/Path-expressions#Tips_amp_Tricks
> ).
>
> augtool> rm /t/A[following-sibling::*[1][self::B]]
> rm : /t/A[following-sibling::*[1][self::B]] 0
>
>
> However, this way I might destroy any children of "A", which henceforth
> are lost. In other words, I'd replace "A" instead of updating it.
>
>
> Normally one selects specific nodes with 'defnode', which will create new
> nodes if no match was found.
>
> augtool> defnode newNode /t/A[following-sibling::*[1][self::B]]
> augtool> print /t
> /t
> /t/#comment
> /t/
> /t/B
> /t/C
> /t/A
>
> The problem this time is, that the new node will be appended to tree in
> last position, so it needs to be moved to the correct position manually.
> And 'mv' in turn has the inherent drawback, that it cannot target relative
> positions, only specific destinations. Which means, replacing existing
> nodes, which I don't want.
>
> Does someone maybe have a solution to this? Or is this another situation
> that needs to be dealt with by applications utilizting Augeas, since Augeas
> doesn't interpret the format of the files it edits?
>
> Thank you for your time,
> Xavier.
>
> PS: I'll be on holidays next week, so I won't be able to answer any
> replies until I've returned.
>
>
> ****
> Karlsruher Institute of Technology (KIT)
> Steinbuch Centre for Computing (SCC)
>
> B. Sc. Xavier Mol
> GridKa Storage Administrator and Support Manager
>
> Hermann-von-Helmholtz-Platz 1
> Geb. 449
> 76344 Eggenstein-Leopoldshafen
> Phone: +49 721 608 23041
> Email: xavier.mol at kit.edu
> www.kit.edu
> KIT - University of the State of Baden-Württemberg and National
> Large-scale Research Center of the Helmholtz Association
> "Since 2010, the KIT has been certified as a family-friendly university."
> ****
>
>
>
> _______________________________________________
> augeas-devel mailing list
> augeas-devel at redhat.com
> https://www.redhat.com/mailman/listinfo/augeas-devel
>



-- 
Raphaël Pinson
Infrastructure Developer & Training Leader
+33 458 482 013

Camptocamp France
Savoie Technolac
BP 352
48, avenue du Lac du Bourget
73372 Le Bourget du Lac, Cedex
www.camptocamp.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listman.redhat.com/archives/augeas-devel/attachments/20160212/8d3cf9e7/attachment.htm>


More information about the augeas-devel mailing list