Class: Machinery::ConfigTask

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

Overview

Copyright © 2013-2016 SUSE LLC

This program is free software; you can redistribute it and/or modify it under the terms of version 3 of the GNU General Public License as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, contact SUSE LLC.

To contact SUSE about this file by physical or electronic mail, you may find current contact information at www.suse.com

Instance Method Summary collapse

Constructor Details

#initialize(config = Machinery::Config.new) ⇒ ConfigTask

Returns a new instance of ConfigTask.



19
20
21
# File 'lib/config_task.rb', line 19

def initialize(config = Machinery::Config.new)
  @config = config
end

Instance Method Details

#config(key = nil, value_string = nil) ⇒ Object



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

def config(key = nil, value_string = nil)
  if !key
    # show all config entries
    max_length = @config.entries.keys.map(&:length).max
    @config.each do |config_key, config_value|
      Machinery::Ui.puts (
        format(
          "%-#{max_length}s %s %s %s",
          config_key,
          "=",
          config_value[:value],
          "(#{config_value[:description]})"
        )
      )
    end
  elsif !value_string
    # show one specific config entry
    Machinery::Ui.puts "#{key}=#{@config.get(key)}"
  else
    # set one specific config entry
    @config.set(key, parse_value_string(key, value_string))
    Machinery::Ui.puts "#{key}=#{@config.get(key)}"
  end
end

#parse_value_string(key, value_string) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/config_task.rb', line 48

def parse_value_string(key, value_string)
  current_value = @config.get(key)

  if current_value == true || current_value == false
    if value_string == "true" || value_string == "on"
      return true
    elsif value_string == "false" || value_string == "off"
      return false
    else
      raise Machinery::Errors::MachineryError.new(
        "The value '#{value_string}' is not valid for key '#{key}'." \
          " Please enter a valid variable of type boolean."
      )
    end
  elsif current_value.kind_of?(Integer)
    return value_string.to_i
  else
    return value_string
  end
end