Hi Josef,

that is indeed an icky problem. There is currently no direct way to get the
newly inserted node. Besides carefully recomputing indices like you
described, the only other way I can think of is to use the
following-sibling and preceding-sibling axes in path expressions. For
example, to get the comment right before the first real entry in /etc/hosts
you'd do

match /files/etc/hosts/#comment[following-sibling::1][last()]

This is based on the fact that the first real entry in /etc/hosts is
/files/etc/hosts/1 (i.e., the '1' in the above path expression is the label
of a node). /files/etc/hosts/#comment matches all comments in /etc/hosts,
[following-sibling::1] restricts that to all the comments that come before
/files/etc/hosts/1, and the [last()] picks out the last of those comments.

Similarly, you'd pick the comment right after /files/etc/hosts/1 with

match /files/etc/hosts/#comment[preceding-sibling::1][1]

I'd agree though that writing these path expressions isn't anybody's idea
of fun. Another approach would be to change some commands to set a variable
(say $_last) to point to the newly inserted node, so that you could just do
'match $_last' to get the inserted node. There's probably more commands
that could benefit from that, particularly set. What do you think ?

David


On Tue, Mar 14, 2017 at 3:12 AM, Josef Reidinger <jreidin...@suse.com>
wrote:

> Good morning to list,
> Let me at first get context. I am using augeas ruby bindings to do some
> automatic changes in configuration files. I build some tools around
> allowing easier access to augeas, but I have now one challenge.
>
> In general I have set of modifications, additional entries and removed
> entries. My challenge is that path is not stable, so when new entry is
> added or removed from array like #comment, then it changes path of
> other items in array.
>
> So consider this scenario. I need to modify two comments, remove two
> comments and add comment before another comment. The first part is
> easy and I use set. The second part I already solved as I know original
> path and do it in reverse order so e.g. remove #comment[50] and
> #comment[36]. Then I want to add new #comment before original
> #comment[10] but it is no longer #comment[10] and I also do not know
> what will be number of new comment ( ok, in this case I know it is path
> of comment I inserted before, but sometimes I need to add it before
> another key like "inst #comment before $prefix/alias[5]" then is there
> easy way how to get path of newly inserted label? like
> $prefix/#comment[16] ?
>
> Also is there way how to get what is old path? Value is not unique,
> especially in comments and I find manually counting and changing all
> indexes quite complex.
>
> Thanks for help and tips.
>
> Josef
>
> _______________________________________________
> augeas-devel mailing list
> augeas-devel@redhat.com
> https://www.redhat.com/mailman/listinfo/augeas-devel
>
_______________________________________________
augeas-devel mailing list
augeas-devel@redhat.com
https://www.redhat.com/mailman/listinfo/augeas-devel

Reply via email to