Project

General

Profile

Download (1.4 KB) Statistics
| Branch: | Tag: | Revision:

hammer-cli-csv / lib / hammer_cli_csv / settings.rb @ f92e695a

1 088e0854 Tom McKay
module HammerCLICsv
2
  class CsvCommand
3
    class SettingsCommand < BaseCommand
4
      command_name 'settings'
5
      desc         'import or export settings'
6
7
      VALUE = 'Value'
8
9 b2e88b7c Tom McKay
      def self.supported?
10
        true
11
      end
12
13 c06f1783 Tom McKay
      def export(csv)
14
        csv << [NAME, VALUE]
15 6498291e Tom McKay
        @api.resource(:settings).call(:index, {
16
            'per_page' => 999999,
17
            'search' => option_search
18
        })['results'].each do |setting|
19 c06f1783 Tom McKay
          csv << [setting['name'], setting['value']]
20 088e0854 Tom McKay
        end
21
      end
22
23
      def import
24
        @existing = {}
25
26
        thread_import do |line|
27
          create_settings_from_csv(line)
28
        end
29
      end
30
31
      def create_settings_from_csv(line)
32 02387fb5 Tom McKay
        count(line[COUNT]).times do |number|
33 088e0854 Tom McKay
          name = namify(line[NAME], number)
34
          params =  { 'id' => get_setting_id(name),
35
                      'setting' => {
36
                        'value' => line[VALUE]
37
                      }
38
                    }
39
          print "Updating setting '#{name}'..." if option_verbose?
40
          @api.resource(:settings).call(:update, params)
41
        end
42
        print "done\n" if option_verbose?
43
      end
44
45
      private
46
47
      def get_setting_id(name)
48
        results = @api.resource(:settings).call(:index, { :search => "name=\"#{name}\"" })['results']
49
        raise "Setting '#{name}' not found" if !results || results.empty?
50
        results[0]['id']
51
      end
52
    end
53
  end
54
end