Module: Jerakia::CLI::Lookup

Included in:
Jerakia::CLI
Defined in:
lib/jerakia/cli/lookup.rb

Class Method Summary collapse

Class Method Details

.included(thor) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/jerakia/cli/lookup.rb', line 4

def self.included(thor)
  thor.class_eval do
    desc 'lookup [KEY]', 'Lookup [KEY] with Jerakia'
    option :config,
           aliases: :c,
           type: :string,
           desc: 'Configuration file'
    option :policy,
           aliases: :p,
           type: :string,
           default: 'default',
           desc: 'Lookup policy'
    option :namespace,
           aliases: :n,
           type: :string,
           default: '',
           desc: 'Lookup namespace'
    option :type,
           aliases: :t,
           type: :string,
           default: 'first',
           desc: 'Lookup type'
    option :scope,
           aliases: :s,
           type: :string,
           desc: 'Scope handler',
           default: 'metadata'
    option :scope_options,
           type: :hash,
           desc: 'Key/value pairs to be passed to the scope handler'
    option :merge_type,
           aliases: :m,
           type: :string,
           default: 'array',
           desc: 'Merge type'
    option :log_level,
           aliases: :l,
           type: :string,
           desc: 'Log level'
    option :verbose,
           aliases: :v,
           type: :boolean,
           desc: 'Print verbose information'
    option :debug,
           aliases: :D,
           type: :boolean,
           desc: 'Debug information to console, implies --log-level debug'
    option :trace,
           type: :boolean,
           desc: 'Output stacktrace to stdout'
    option :metadata,
           aliases: :d,
           type: :hash,
           desc: 'Key/value pairs to be used as metadata for the lookup'
    option :schema,
           aliases: :S,
           type: :boolean,
           desc: 'Enable/disable schema lookup, default true',
           default: true
    option :output,
           aliases: :o,
           type: :string,
           efault: 'json',
           desc: 'Output format, yaml or json'

    def lookup(key=nil)
      # Thor by default now returns a frozen options hash so we
      # need to dup this here to prevent problems later with
      # modifying the request object
      #
      options_copy = options.dup
      key_copy = key.nil? ? key : key.dup
      case true
      when options[:verbose]
        loglevel = 'verbose'
        logfile  = STDOUT
      when options[:debug]
        loglevel = 'debug'
        logfile  = STDOUT
      else
        logfile = nil
        loglevel = options[:log_level]
      end

      begin
        jac = Jerakia.new(:config => options[:config],
                          :logfile  => logfile,
                          :loglevel => loglevel,
                          :trace    => options[:trace])
        req = Jerakia::Request.new(
          :key           => key_copy,
          :namespace     => options_copy[:namespace].split(/::/),
          :policy        => options_copy[:policy].to_sym,
          :lookup_type   => options_copy[:type].to_sym,
          :merge         => options_copy[:merge_type].to_sym,
          :metadata      => options_copy[:metadata] || {},
          :scope         => options_copy[:scope].to_sym,
          :scope_options => options_copy[:scope_options],
          :use_schema    => options_copy[:schema]
        )

        answer = jac.lookup(req)
        case options[:output]
        when 'json'
          puts answer.payload.to_json
        when 'yaml'
          puts answer.payload.to_yaml
        else
          puts answer.payload
        end
      rescue Jerakia::Error => e
        STDERR.puts "Error(#{e.class}): #{e.message}"
        STDERR.puts e.backtrace.join("\n") if options[:trace]
        exit 1
      end
    end
  end
end