Module: PDK::CLI::Set::Config

Defined in:
lib/pdk/cli/set/config.rb

Constant Summary collapse

ALLOWED_TYPE_NAMES =
%w[array boolean number string].freeze

Class Method Summary collapse

Class Method Details

.pretty_allowed_namesObject

:nocov:



7
8
9
# File 'lib/pdk/cli/set/config.rb', line 7

def self.pretty_allowed_names
  ALLOWED_TYPE_NAMES.map { |name| "'#{name}'" }.join(', ')
end

.run(opts, args) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/pdk/cli/set/config.rb', line 63

def self.run(opts, args)
  item_name = (args.count > 0) ? args[0] : nil
  item_value = (args.count > 1) ? args[1] : nil

  opts[:type] = opts[:as] if opts[:type].nil? && !opts[:as].nil?
  force = opts[:force] || false

  # Transform the value if we need to
  item_value = PDK::CLI::Set::Config.transform_value(opts[:type], item_value) unless opts[:type].nil?

  raise PDK::CLI::ExitWithError, _('Configuration name is required') if item_name.nil?
  raise PDK::CLI::ExitWithError, _('Configuration value is required. If you wish to remove a value use \'pdk remove config\'') if item_value.nil?

  current_value = PDK.config.get(item_name)
  raise PDK::CLI::ExitWithError, _("The configuration item '%{name}' can not have a value set.") % { name: item_name } if current_value.is_a?(PDK::Config::Namespace)

  # If we're forcing the value, don't do any munging
  unless force
    # Check if the setting already exists
    if current_value.is_a?(Array) && current_value.include?(item_value)
      PDK.logger.info(_("No changes made to '%{name}' as it already contains value '%{to}'") % { name: item_name, to: item_value })
      return 0
    end
  end

  new_value = PDK.config.set(item_name, item_value, force: opts[:force])
  if current_value.nil? || force
    PDK.logger.info(_("Set initial value of '%{name}' to '%{to}'") % { name: item_name, to: new_value })
  elsif current_value.is_a?(Array)
    # Arrays have a special output format
    PDK.logger.info(_("Added new value '%{to}' to '%{name}'") % { name: item_name, to: item_value })
  else
    PDK.logger.info(_("Changed existing value of '%{name}' from '%{from}' to '%{to}'") % { name: item_name, from: current_value, to: new_value })
  end

  # Same output as `get config`
  $stdout.puts _('%{name}=%{value}') % { name: item_name, value: PDK.config.get(item_name) }
  0
end

.transform_value(type_name, value) ⇒ Object

:nocov:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pdk/cli/set/config.rb', line 12

def self.transform_value(type_name, value)
  normalized_name = type_name.downcase.strip
  unless ALLOWED_TYPE_NAMES.include?(normalized_name)
    raise PDK::CLI::ExitWithError, _('Unknown type %{type_name}. Expected one of %{allowed}') % { type_name: type_name, allowed: pretty_allowed_names }
  end

  # Short circuit string conversions as it's trivial
  if normalized_name == 'string'
    raise PDK::CLI::ExitWithError, _('An error occured converting \'%{value}\' into a %{type_name}') % { value: value.nil? ? 'nil' : value, type_name: type_name } unless value.is_a?(String)
    return value
  end

  begin
    case normalized_name
    when 'array'
      convert_to_array(value)
    when 'boolean'
      convert_to_boolean(value)
    when 'number'
      convert_to_number(value)
    else
      value
    end
  rescue ArgumentError, TypeError
    raise PDK::CLI::ExitWithError, _('An error occured converting \'%{value}\' into a %{type_name}') % { value: value.nil? ? 'nil' : value, type_name: type_name }
  end
end