Method: PDK::CLI::Set::Config.run

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

.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