Project

General

Profile

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

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

1
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
      def self.supported?
10
        true
11
      end
12

    
13
      def export(csv)
14
        csv << [NAME, VALUE]
15
        @api.resource(:settings).call(:index, {'per_page' => 999999})['results'].each do |setting|
16
          csv << [setting['name'], setting['value']]
17
        end
18
      end
19

    
20
      def import
21
        @existing = {}
22

    
23
        thread_import do |line|
24
          create_settings_from_csv(line)
25
        end
26
      end
27

    
28
      def create_settings_from_csv(line)
29
        count(line[COUNT]).times do |number|
30
          name = namify(line[NAME], number)
31
          params =  { 'id' => get_setting_id(name),
32
                      'setting' => {
33
                        'value' => line[VALUE]
34
                      }
35
                    }
36
          print "Updating setting '#{name}'..." if option_verbose?
37
          @api.resource(:settings).call(:update, params)
38
        end
39
        print "done\n" if option_verbose?
40
      end
41

    
42
      private
43

    
44
      def get_setting_id(name)
45
        results = @api.resource(:settings).call(:index, { :search => "name=\"#{name}\"" })['results']
46
        raise "Setting '#{name}' not found" if !results || results.empty?
47
        results[0]['id']
48
      end
49
    end
50
  end
51
end