Class: MultiIndex::Config

Inherits:
Thor
  • Object
show all
Includes:
CommandsCommon, ConfigValidation, Preconditions, Prettify::Prettifier
Defined in:
lib/multi_index/commands.rb

Constant Summary

Constants included from ConfigValidation

MultiIndex::ConfigValidation::ALLOWED_VALUES, MultiIndex::ConfigValidation::MAPPING_FORMAT

Constants included from CommandsCommon

MultiIndex::CommandsCommon::LOG

Instance Method Summary collapse

Methods included from ConfigValidation

#get_password, #get_plain, #validate_config_parameters, #validate_write_parameters

Methods included from Encryption

#decrypt, #encrypt, #secret_key

Methods included from CommandsCommon

#print_parameters, #with_exception_handling

Methods included from Preconditions

included

Instance Method Details

#profileObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/multi_index/commands.rb', line 83

def profile
  with_exception_handling do
    print_parameters(options)

    filename = options['file']
    format = (/.+\.json$/.match(filename)) ? :to_json : ((/.+\.yml/.match(filename)) ? :to_yaml : :unknown)
    check_argument(format != :unknown, 'File must end with a .json or .yml extension')

    # Reuse validation code so there is no duplication
    # Set output to CONFIG_SERVICE to force LDAP prompts
    profile = validate_config_parameters({'output' => 'CONFIG_SERVICE'})[:profile]
    file = File.open(filename, 'w')
    file.write(profile.send(format))
    file.close

    LOG.info "Profile written to #{filename}. You can now use this file with the -p option in the `write` command."
  end
end

#writeObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/multi_index/commands.rb', line 27

def write
  with_exception_handling do
    print_parameters(options)

    write_options = validate_write_parameters(options)
    config_options = validate_config_parameters(options)
    profile = config_options[:profile]
    tier = config_options[:tier]

    mapping = MultiIndex::GoogleDoc.new(profile[:google_drive]).mapping
    output = write_options[:output]
    format = write_options[:format]
    method = options['invert'] || (output != 'stdout') ? :invert : :original

    case output

      when 'stdout'
        puts "******** MULTI-INDEX CONFIGURATION: #{tier} ********".green
        case format
          when 'yaml'
            puts mapping.send(method, tier).to_yaml.green
          when 'json'
            puts prettify(mapping.send(method, tier).to_json).green
          when 'escaped'
            puts prettify(mapping.send(method, tier).to_json).gsub(',', '\,').green
        end
        break

      when 'EAS'
        LOG.info 'Performing update assuming present working directory is EAS'
        resources_path = 'entity-attribute-server/src/main/resources'

        mapping.send(method, tier).each do |tier, indices|
          indices = Hash[indices.map { |k, v| [k.upcase, v] }]
          config_filename = "#{resources_path}/#{tier}.yml"

          config = YAML.load_file(config_filename)
          LOG.info "Updating file: #{config_filename} for tier: #{tier}"
          config['elasticsearch'] ||= {}
          config['elasticsearch'].merge!({'indices' => indices})

          config_file = File.open(config_filename, 'w')
          config_file.write(config.to_yaml.gsub(/^---\n/, ''))
          config_file.close
        end
        break

      else
        # TODO: Code for config service writing
    end
  end
end