[augeas-devel] [PATCH] add match construct to lens language

David Lutterkort lutter at redhat.com
Sat Feb 21 07:52:21 UTC 2009


This adds a very simple switch/case facility to the lens language. The main
reason for doing this is to make it possible to tailor certain aspects of
lenses to the system Augeas is running on, for example which file the Grub.lns should actually look at.

To make this really useful, we also need some information about the current
system - I plan on adding a separate 'Sys' module that provides things like
the type of the OS, the distro etc.

Syntactically, the case statement follows ML's pattern matching, but allows
only for very simple minded matches, namely literal matches of strings,
i.e. none of the fancy record matching. The general form is

  match string with
  | lit1 -> exp1
  | lit2 -> exp2
  ...
  | _ -> expN

where string is some expression evaluating to a string, exp1,...,expN are
arbitrary expressions of compatible types, and lit1, lit2, ... are literal
strings. The default branch '| _ -> ...' is mandatory and must be the last
one in the match statement (I don't want to deal with none of the branches
matching at runtime)

To make this more concrete, assuming that Sys.os_distro returns the name of
the distro and Sys.os_vendor some sort of vendor namein some canonical
form, we could write something like

  let grub_file_name = match Sys.os_vendor with
    | "Red Hat" -> "/etc/grub.conf"
    | _ -> (match Sys.os_distro with
              | "Debian" -> "/boot/grub/menu.lst"
              | "Ubuntu" -> "/boot/grub/menu.lst"
              | "FreeBSD" -> "/some/where/else"
              | _ -> "/nowhere/at/all")
  let xfm = transform Grub.lns (incl grub_file_name)

The one incompatibility is that 'match' and 'with' are now keywords - but
the only place where they were used as identifiers was the Sshd lens, and
easily fixed.

David




More information about the augeas-devel mailing list