Class: ZoneUpdate

Inherits:
Command show all
Defined in:
lib/zone_update.rb

Instance Attribute Summary

Attributes inherited from Command

#desc, #name

Instance Method Summary collapse

Methods inherited from Command

#add_arg, #add_cmd, #add_flow, #add_flow_from_usage, #add_input, #add_option, #add_options, #command_name?, #flow_passes_parse, #flow_passes_preconditions, #get_args_used, #init, #initialize, #option_help_string, #run, #show_help, #show_help_option

Constructor Details

This class inherits a constructor from Command

Instance Method Details

#boolean_value_of(s) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/zone_update.rb', line 12

def boolean_value_of(s)
  if s == 'true'
    true
  elsif s == 'false'
    false
  else
    raise "illegal value of boolean:#{s}"
  end
end

#create_args ⇒ Object



4
5
6
# File 'lib/zone_update.rb', line 4

def create_args
  add_arg('name-value','of the form name:new-value, where name is the (abbreviated) name of field', /[[:alpha:]]+:.*/)
end

#create_flows ⇒ Object



8
9
10
# File 'lib/zone_update.rb', line 8

def create_flows
  add_flow_from_usage("<name-value>...")
end

#execute(arghash, rest) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/zone_update.rb', line 45

def execute(arghash, rest)
  nvarray = arghash["name-value"]
  struct = @input["struct"]
  if struct.class == Hash
    nvarray.each { | nv |
      name, value = Util.parse_line(nv,':')
      update_struct(struct, name, value)
    }
  elsif struct.class == Array
    raise "can only update single item" if struct.length > 1
    update_struct(struct[0], name, value)
  else
    raise "can't update at this level"
  end
end

#update_struct(struct, key, value) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/zone_update.rb', line 22

def update_struct(struct, key, value)
  name = Util.match_key(key, struct)
  if name.nil?
    raise "no (unique) key matches" + key 
  end
  target_type = struct[name].class
  case [target_type]
  when [Fixnum]
    value = value.to_i
  when [TrueClass]
    value = boolean_value_of(value)
  when [FalseClass]
    value = boolean_value_of(value)
  when [String]
  else
    raise "cannot change item of type #{target_type}"
  end
  struct[name] = value
  zone = @input["zone"]
  zone.write_zone_file(@input["toplevel"], false)
  StructurePrint.new("-").structure_print(struct, "")
end