Class: Bro::State

Inherits:
Object
  • Object
show all
Defined in:
lib/bro/state.rb

Direct Known Subclasses

BroState

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ State

Returns a new instance of State.



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

def initialize options
  @file = options[:file]
end

Instance Method Details

#read_stateObject

read the ~/.bro file and return a hash of the values



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bro/state.rb', line 25

def read_state
  obj = {}
  begin
    contents = File.read @file
    contents.strip.split(";").each {|kv|
      chunk = kv.split("=")
      key = chunk[0].intern
      obj[key] = chunk[1]
    }
  rescue => e
    # ignore file not found
  end
  obj
end

#write_state(obj, override = false) ⇒ Object

write state to this file, using the existing key/vals as defaults format is key=val;key2=val;key3=val



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bro/state.rb', line 10

def write_state obj, override=false
  # read the ~/.bro file and load the settings as defaults
  # we don't want to lose data if we're not writing over it all
  read_state.each {|k,v|
    obj[k] = v if obj[k].nil?
  } unless override # unless we pass in strict parsing

  # actually serialize the data and write the file
  File.open(@file, 'w') do |file|
    data_pairs = obj.collect{ |k,v| "#{k}=#{v}" }
    file.write data_pairs.join(";") + "\n"
  end
end