Right now I'm playing with something like:
And when I run a little "check" script you see:
network:
hostname: franzfw
interfaces:
eth0:
address: dhcp
eth2:
address: 192.168.100.1
netmask: 255.255.255.0
routes:
-'192.168.169.0/24': 192.168.100.2
forwarding: true
With the script that created this...
franz-g4:~/dev/playin/ruby mdfranz$ ruby ycheck.rb config.yaml
yf is a:Hash
{"network"=>
{"routes"=>{"-'192.168.169.0/24'"=>"192.168.100.2"},
"forwarding"=>true,
"interfaces"=>
{"eth0"=>{"address"=>"dhcp"},
"eth2"=>{"netmask"=>"255.255.255.0", "address"=>"192.168.100.1"}},
"hostname"=>"franzfw"}}
With the following keys:
-network
Hostname:franzfw
interfaces is a:Hash
Configuring:eth0
Configuring:eth2
require 'yaml'
require 'pp'
yf = YAML.load_file(ARGV[0])
print "yf is a:"
print yf.class
print "\n"
pp yf
print "With the following keys:\n"
yf.keys.each { |k| puts "-" + k }
puts "Hostname:" + yf['network']['hostname']
interfaces = yf['network']['interfaces']
print "interfaces is a:"
print interfaces.class
print "\n"
print interfaces.keys.each { |i| puts "Configuring:" + i }
Don't have time to get into the details (see the YAML Cookbook for more info) but there were a couple of areas I got hung up on:
- Using tabs instead of whitespaces. Ruby YAML doesn't like tabs, but if your vimrc is setup for Python you should be good to go.
- Getting hashes instead of arrays (and vice versa). Play around with ident's and -'s until you get what you need
Since ruby is so small (less than 2MB compressed) it will be included as part of the core and probably everything outside /init may use it.
4 comments:
> Using tabs instead of whitespaces. Ruby YAML doesn't like tabs
Tabs for indentation? - I thought YAML specifically disallowed that...
KarlMW.
You are correct. If you set your editor to replace tabs with spaces it is fine.
puts "yf is a #{ yf.class }"
Yeah but that is so ugly...
Post a Comment