Hi,

I'm wondering if there's a way to set the maximum number of fields
that edit_field will use. For example, I've written the following
bundle for setting values in a configuration file of the form "var
value" (e.g. sshd config file). It is based on set_variable_values
from stdlib, using a different separator:

# Sets the RHS of configuration items in the file of the form
#   LHS RHS
# If the line is commented out with #, it gets uncommented first
# Adds a new line if non exists.
# The argument is an associative array containint v[LHS]="rhs"

# Based on set_variable_values from cfengine_stdlib.cf, modified
# by Diego Zamboni to use whitespace as separator, and to handle
# commented-out lines.
bundle edit_line set_config_values(v)
{
vars:
  "index" slist => getindices("$(v)");

  # Be careful if the index string contains funny chars
  "cindex[$(index)]" string => canonify("$(index)");

field_edits:

  # If the line is there, but commented out, first uncomment it
  "#+$(index)\s+.*"
     edit_field => col("\s+","1","$(index)","set");

  # match a line starting like the key something
  "$(index)\s+.*"
     edit_field => col("\s+","2","$($(v)[$(index)])","set"),
        classes => if_ok("not_$(cindex[$(index)])");

insert_lines:

  "$(index) $($(v)[$(index)])",
      ifvarclass => "!not_$(cindex[$(index)])";
}

The problem with this is that edit_field is splitting on whitespace,
so if the RHS contains spaces, only the first non-space sequence will
be replaced, leaving spurious values behind. It would be nice to be
able to tell edit_field that the line contains only two fields, and to
split only at the first instance of the separator.

This can be done with replace_patterns instead of field_edits, but I
think it would be a nice feature to have. Or maybe it is there already
and I am missing something?

Thanks! BTW, I find this list immensely useful and friendly. I've only
started with cfengine a few weeks ago, but I am already getting a good
feeling for its power, and the community has been a huge help.

--Diego
_______________________________________________
Help-cfengine mailing list
Help-cfengine@cfengine.org
https://cfengine.org/mailman/listinfo/help-cfengine

Reply via email to