Project

General

Profile

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

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

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, {
16
            'per_page' => 999999,
17
            'search' => option_search
18
        })['results'].each do |setting|
19
          csv << [setting['name'], setting['value']]
20
        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
        count(line[COUNT]).times do |number|
33
          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