Class: CFA::Grub2::Default::KernelParams

Inherits:
Object
  • Object
show all
Defined in:
lib/cfa/grub2/default.rb

Overview

Represents kernel append line with helpers to easier modification. TODO: handle quoting, maybe have own lense to parse/serialize kernel

params?

Defined Under Namespace

Classes: ParamTree

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line, key) ⇒ KernelParams

Returns a new instance of KernelParams.



147
148
149
150
# File 'lib/cfa/grub2/default.rb', line 147

def initialize(line, key)
  @tree = ParamTree.new(line)
  @key = key
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



145
146
147
# File 'lib/cfa/grub2/default.rb', line 145

def key
  @key
end

Instance Method Details

#add_parameter(key, value, placer = AppendPlacer.new) ⇒ Object

Adds new parameter to kernel command line. Uses augeas placers. To replace value use ReplacePlacer



194
195
196
197
198
199
200
# File 'lib/cfa/grub2/default.rb', line 194

def add_parameter(key, value, placer = AppendPlacer.new)
  element = placer.new_element(@tree)

  element[:operation] = :add
  element[:key]   = key
  element[:value] = value
end

#empty?Boolean

checks if there is any parameter

Returns:

  • (Boolean)


162
163
164
# File 'lib/cfa/grub2/default.rb', line 162

def empty?
  serialize.empty?
end

#parameter(key) ⇒ Object

gets value for parameters.

Examples:

different values

line = "quite console=S0 console=S1 vga=0x400"
params = KernelParams.new(line)
params.parameter("quite") # => true
params.parameter("verbose") # => false
params.parameter("vga") # => "0x400"
params.parameter("console") # => ["S0", "S1"]

Returns:

  • possible values are ‘false` when parameter missing, `true` when parameter without value placed, string if single instance with value is there and array if multiple instance with values are there.



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/cfa/grub2/default.rb', line 180

def parameter(key)
  values = @tree.data
                .select { |e| e[:key] == key }
                .map { |e| e[:value] }

  return false if values.empty?
  return values if values.size > 1
  return true if values.first == true

  values.first
end

#remove_parameter(matcher) ⇒ Object

Removes parameter from kernel command line.

Parameters:

  • matcher (Matcher)

    to find entry to remove



204
205
206
# File 'lib/cfa/grub2/default.rb', line 204

def remove_parameter(matcher)
  @tree.data.select(&matcher).each { |e| e[:operation] = :remove }
end

#replace(line) ⇒ Object

replaces kernel params with passed line



157
158
159
# File 'lib/cfa/grub2/default.rb', line 157

def replace(line)
  @tree = ParamTree.new(line)
end

#serializeObject



152
153
154
# File 'lib/cfa/grub2/default.rb', line 152

def serialize
  @tree.to_string
end