Sha256: ccdac2f05b1dd7a08132d09260ee193563f1c9c92214c297c2fe0aa85270a683

Contents?: true

Size: 1.84 KB

Versions: 4

Compression:

Stored size: 1.84 KB

Contents

# Copyright (c) 2013-2014 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

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

  def config(key = nil, value_string = nil)
    if !key
      # show all config entries
      @config.each do |key, value|
        Machinery::Ui.puts "#{key} = #{value[:value]} (#{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

  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}'.")
      end
    elsif current_value.kind_of?(Integer)
      return value_string.to_i
    else
      return value_string
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
machinery-tool-1.2.0 lib/config_task.rb
machinery-tool-1.1.1 lib/config_task.rb
machinery-tool-1.1.0 lib/config_task.rb
machinery-tool-1.0.2 lib/config_task.rb