help-cfengine
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Question about LocateLineMatching


From: Brendan Strejcek
Subject: Re: Question about LocateLineMatching
Date: Wed, 17 Mar 2004 20:48:13 -0600
User-agent: Mutt/1.3.28i

Tim Nelson wrote:

> Hi. I have a question about LocateLineMatching. When it doesn't match,
> it immediately aborts editing of the file (as specified in the doco).
> I was wondering, if you have different independant edits on the same
> file, should you have two editfiles sections for the same file? Or is
> there some way of grouping which will get around this problem?

When I use LocateLineMatching, I always ensure there is something
there to find. For example, a common task for me when I edit files is
maintaining a left-hand-side/right-hand-side pair (that is, setting a
variable and making sure that the line is unique). I have some m4 macros
which make heavy use of LocateLineMatching and allows me to use the
self-invented SetVariableToValue(LHS,RHS) construct in my edits. This
allows me to deal with most config files (other than ini style).

Also note that you can use CatchAbort and ResetSearch to recover from
editfiles aborts.

The indentation is a hack (I know how far all my edits are indented).

$ cat macros.m4
ifelse(`ifelse defines a comment if used with one argument')dnl
ifelse(`dnl drops the newline')dnl
ifelse(`make sure A=B and that all other occurances of A= are deleted')dnl

define(`SetVariableToValue',`# begin SetVariableToValue $1$2
            BeginGroupIfNoLineMatching "^$1.*"
                Append "$1$2"
            EndGroup
            ResetSearch "1"
            LocateLineMatching "^$1.*"
            BeginGroupIfNoMatch "^$1$2$"
                ReplaceLineWith "$1$2"
            EndGroup
            DeleteLinesAfterThisMatching "^$1.*"
            # end SetVariableToValue $1$2')dnl

define(`MaintainMarkedLine',`# begin MaintainMarkedLine $1$2
            BeginGroupIfNoLineMatching "^.*$2$"
                Append "$1$2"
            EndGroup
            ResetSearch "1"
            LocateLineMatching "^.*$2$"
            BeginGroupIfNoMatch "^$1$2$"
                ReplaceLineWith "$1$2"
            EndGroup
            DeleteLinesAfterThisMatching "^.*$2$"
            # end MaintainMarkedLine $1$2')dnl

ifelse(`eg: SetVariableToValue(`portmap=',`YES')')dnl
ifelse(`every file which uses these macros should include this file')dnl
ifelse(`eg: include(`macros.m4')')dnl

Here is an example using SetVariableToValue:

editfiles:

    debian_3_0_nis_slave::

        { /etc/default/nis
            SetVariableToValue(`NISSERVER=',`slave')
            PrependIfNoSuchLine "# ${editmark}"
            DefineClasses "debian_3_0_nis_slave_new"
        }

-- Brendan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]