This context is used to access information about editing promises during their execution. It is context dependent and not universally meaningful or available.

edit.filename

This variable points to the filename of the file currently making an edit promise. If the file has been arrived at through a search, this could be different from the files promiser.

code
bundle agent __main__
{
  files:

      "/tmp/example-edit.filename.txt" content => "Hello World!";

      "/tmp/example-edit.filename.txt"
        edit_line => show_edit_filename;

}

bundle edit_line show_edit_filename
{
  reports:
      "$(with)"
        with => concat( "I found the string 'World' in the file being ",
                        "edited ('$(edit.filename)')"),
        if => strcmp( "false", "$(edit.empty_before_use)"); # It's probably
      # useless to probe
      # the content of the
      # file if you are
      # ignoring
      # pre-existing
      # content.

      "$(with)"
        with => concat( "It's probably not very useful to inspect content",
                        "that is being thrown away." ),
        if => strcmp( "true", "$(edit.empty_before_use)");

      "$(with)"
        with => concat ( "This version of CFEngine does not know if the",
                         "edit operation is expected to ignore pre-existing ",
                         "content the variable 'edit.empty_before_use' does ",
                         "not exist"),
        unless => isvariable ( "edit.empty_before_use" );

}

body edit_defaults my_empty_file_before_editing
{
        empty_file_before_editing => "true"; # The variable
                                             # edit.empty_before_use allows this
                                             # to be known from within an
                                             # edit_line bundle.
}
code
R: I found the string 'World' in the file being edited ('/tmp/example-edit.filename.txt')

This policy can be found in /var/cfengine/share/doc/examples/edit.filename.cf and downloaded directly from github.

edit.empty_before_use

This variable holds the value of empty_file_before_editing from edit_defaults bodies. It's useful for altering behavior within an edit_line bundle depending if the files prior content will or won't have any effect.

code
bundle agent __main__
{
  files:

      "/tmp/example-edit.empty_before_use.one" create => "true";
      "/tmp/example-edit.empty_before_use.two" create => "true";

      "/tmp/example-edit.empty_before_use.one"
        edit_line => show_edit_empty_before_use,
        edit_defaults => my_empty_file_before_editing;

      "/tmp/example-edit.empty_before_use.two"
        edit_line => show_edit_empty_before_use;

}

bundle edit_line show_edit_empty_before_use
{
  reports:
      "$(with)"
        with => concat( "The promise to edit '$(edit.filename)' was ",
                        "instructed to ignore any pre-existing content." ),
        if => strcmp( "true", "$(edit.empty_before_use)");

      "$(with)"
        with => concat( "The promise to edit '$(edit.filename)' was ",
                        "not instructed to ignore any pre-existing content."),
        if => strcmp( "false", "$(edit.empty_before_use)");

      "$(with)"
        with => concat ( "This version of CFEngine does not know if the",
                         "edit operation is expected to ignore pre-existing ",
                         "content the variable 'edit.empty_before_use' does ",
                         "not exist"),
        unless => isvariable ( "edit.empty_before_use" );

}

body edit_defaults my_empty_file_before_editing
{
        empty_file_before_editing => "true"; # The variable
                                             # edit.empty_before_use allows this
                                             # to be known from within an
                                             # edit_line bundle.
}
code
R: The promise to edit '/tmp/example-edit.empty_before_use.one' was instructed to ignore any pre-existing content.
R: The promise to edit '/tmp/example-edit.empty_before_use.two' was not instructed to ignore any pre-existing content.

This policy can be found in /var/cfengine/share/doc/examples/edit.empty_before_use.cf and downloaded directly from github.

See also:

History:

  • 3.21.0, 3.18.3 added