I also have a relatively small library called 'facter' that is a bit more
generic, in that it can used to retrieve any set of facts that varies by
platform, release, or whatever. The initial and probably most important
facts are the platform and release (I develop software for sysadmins, so the
OS release matters quite a bit), but I've also got resolution mechanisms for
things like IP addresses, MAC addresses, and the domain name.
I wrote it because I was tired of having nasty switch statements based on
the output of 'uname -s', and then often repeating the same switch
statements in different programs.
You can find the library here:
http://reductivelabs.com/projects/facter/
It's pretty easy to use:
require 'facter'
os = Facter["operatingsystem"].value
It actually returns the fact object, and calling 'value' on it calls each of
its resolution mechanisms in turn until one returns a value. I should
probably short circuit that and just return the value itself, but, well, I
haven't, at this point.
You can also iterate across all of the known facts, in which case you get
the actual fact values:
Facter.each { |fact, value| puts "%s => %s" % [fact, value] }
It's very easy to add new resolution mechanisms, with arbitrary
restrictions. Here are the latest ones I've added:
# ps for most people
Facter["ps"].add { |obj|
obj.code = "echo 'ps -ef'"
}
# ps for darwin; note the tag
Facter["ps"].add { |obj|
obj.tag("operatingsystem","=","Darwin")
obj.code = "echo 'ps -auxwww'"
}
# how to get your name on linux
Facter["id"].add { |obj|
obj.tag("operatingsystem","=","Linux")
obj.code = "whoami"
}
You can add tag restrictions based on any other facts, and you can add as
many tags as you want. The tags are just a triad of an existing fact, an
operator, and the value. I basically just eval the three, so it's nothing
complicated I do here.
So, this is probably a bit more functionality than you need in this case,
but, well, it's out there, and if you're doing any sysadmin-style
development (I haven't seen many other people using Ruby for sysadmin work
yet), this could be a pretty useful library for you.
At this point it's only used in my Puppet project, I believe.
···
On Wed, 23 Nov 2005, Kaspar Schiess wrote:
Matt Mower proposed this:
blogs.it
This actually supports my case in that libraries should really be written
using this (or another) small lib. This should be standard, even. Matching
with Regexps just does not cut it.
And no, mingw is not like cygwin at all. Just for the record.
--
It is a mistake to think you can solve any major problems just with
potatoes. --Douglas Adams
---------------------------------------------------------------------
Luke Kanies | http://reductivelabs.com | http://madstop.com