Module: DLDInternet::Thor::MixIns::NoCommands

Includes:
Mixlib::Logging
Defined in:
lib/dldinternet/thor/mixins/no_commands.rb

Instance Method Summary collapse

Instance Method Details

#abort!(msg) ⇒ Object



156
157
158
159
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 156

def abort!(msg)
  @logger.error msg
  exit -1
end

#command_out(res, fmtr = nil) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 240

def command_out(res, fmtr=nil)
  unless fmtr
    fmtr = formatter.call(res, options)
    fmtr.table_widths
  end
  case options[:format]
  when /text|none|plain/
    output(header_line(res, fmtr), fmtr, true) unless options[:header] === false
    case res.class.name
    when /Array/
      res.each do |obj|
        output format_line(obj, fmtr)
      end
    # when /Hash|String/
    else
      output format_line(res, fmtr)
    end
  else
    output res
  end
end

#command_pre(*args) ⇒ Object



235
236
237
238
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 235

def command_pre(*args)
  parse_options
  @logger.info @_invocations.map{ |_,v| v[0]}.join(' ') if options[:verbose]
end

#default_format(obj, opts = nil, format_helper = nil) ⇒ Object



190
191
192
193
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 190

def default_format(obj, opts=nil, format_helper=nil)
  format_helper ||= default_formatter(obj, opts || options)
  format_helper.format_it
end

#default_formatter(obj, opts = nil) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 165

def default_formatter(obj, opts=nil)
  opts ||= Hashie::Mash.new(options.to_h)
  format_helper = DLDInternet::Formatters::Basic.new(obj, notation, title: opts[:title], columns: opts[:columns])
  case notation.to_sym
  when :json
  when :yaml
  when :none
  when :basic
  when :text
    # noop
  when :awesome
    format_helper = DLDInternet::Formatters::Awesome.new(obj, notation, title: opts[:title], columns: opts[:columns])
  when :table
    format_helper = DLDInternet::Formatters::Table.new(obj, notation, title: opts[:title], columns: opts[:columns])
  else
    raise DLDInternet::Formatters::Error, "Unknown format requested: #{notation}"
  end
  format_helper
end

#default_header(obj, format_helper = nil) ⇒ Object



185
186
187
188
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 185

def default_header(obj, format_helper=nil)
  format_helper ||= default_formatter(obj)
  format_helper.header_it
end

#format_line(obj, fmtr = nil) ⇒ Object



278
279
280
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 278

def format_line(obj, fmtr=nil)
  @format.call(obj, fmtr)
end

#hash_it(robj) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 195

def hash_it(robj)
  klass = robj.class
  repre = begin
    klass.const_get('Representation')
  rescue
    false
  end
  if robj.nil?
    robj
  elsif robj.respond_to?(:to_hash)
    robj.to_hash
  elsif robj.respond_to?(:to_a)
    robj.to_a.map { |obj| hash_it(obj) }
  elsif repre
    representation = repre.new(robj)
    representation.to_hash
  elsif robj.respond_to?(:to_h)
    robj.to_h
  else
    robj
  end
end

#header_line(obj, fmtr = nil) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 262

def header_line(obj, fmtr=nil)
  if obj.is_a?(String)
    obj
  elsif obj.is_a?(Hash)
    @header.call(obj, fmtr)
  elsif obj.is_a?(Array)
    if obj.size > 0
      header_line(obj[0], fmtr)
    else
      header_line({}, fmtr)
    end
  else
    raise "Cannot produce header from this object: #{obj.class.name}"
  end
end

#invoke_command(command, *args) ⇒ Object

:nodoc:



282
283
284
285
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 282

def invoke_command(command, *args) #:nodoc:
  ::DLDInternet::Thor::Command.invocations = @_invocations.dup.map{ |_,v| v[0]}
  super
end

#load_configObject



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
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 77

def load_config
  if File.exist?(@options[:config])
    begin
      envs = ::Config::Factory::Environments.load_file(@options[:config])
      if envs and envs.is_a?(Hash) and @options[:environment]
        @options[:environments] = ::Hashie::Mash.new(envs)
      else
        yaml = ::YAML.load(File.read(@options[:config]))
        if yaml
          yaml.each {|key, value|
            @options[key.to_s.gsub(%r{[-]}, '_').to_sym]=value
          }
        else
          msg = "#{options.config} is not a valid configuration!"
          @logger.error msg
          raise StandardError.new(msg)
        end
      end
    rescue ::Exception => e
      @logger.error "#{e.class.name} #{e.message}"
      raise e
    end
  else
    @logger.warn "#{options.config} not found"
  end
end

#load_inifileObject



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
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 34

def load_inifile
  unless File.exist?(@options[:inifile])
    raise "#{@options[:inifile]} not found!"
  end
  begin
    ini = ::IniFile.load(@options[:inifile])
    ini['global'].each{ |key,value|
      @options[key.to_s]=value
      ENV[key.to_s]=value
    }
    def _expand(k,v,regex,rerun)
      matches = v.match(regex)
      if matches
        var = matches[1]
        if options[var]
          options[k]=v.gsub(/\%\(#{var}\)/,options[var]).gsub(/\%#{var}/,options[var])
        else
          rerun[var] = 1
        end
      end
    end

    pending = nil
    rerun = {}
    begin
      pending = rerun
      rerun = {}
      options.to_hash.each{|k,v|
        if v.to_s.match(/\%/)
          _expand(k,v,%r'[^\\]\%\((\w+)\)', rerun)
          _expand(k,v,%r'[^\\]\%(\w+)',     rerun)
        end
      }
      # Should break out the first time that we make no progress!
    end while pending != rerun
  rescue ::IniFile::Error => e
    # noop
  rescue ::Exception => e
    @logger.error "#{e.class.name} #{e.message}"
    raise e
  end
end

#notationObject



161
162
163
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 161

def notation
  @config[:output] || :none
end

#output(obj, fmtr = nil, header = false) ⇒ Object



227
228
229
230
231
232
233
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 227

def output(obj, fmtr=nil, header=false)
  unless obj.nil?
    hash = obj.is_a?(Array) ? obj.map { |o| hash_it(o) } : (obj.is_a?(String) ? obj : hash_it(obj))
    str = string_it(hash, fmtr, header)
    write str
  end
end

#parse_optionsObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 104

def parse_options
  validate_options

  lcs = ::Logging::ColorScheme.new( 'compiler', :levels => {
      :trace => :blue,
      :debug => :cyan,
      :info  => :green,
      :note  => :green,
      :warn  => :yellow,
      :error => :red,
      :fatal => :red,
      :todo  => :purple,
  })
  scheme = lcs.scheme
  scheme['trace'] = "\e[38;5;33m"
  scheme['fatal'] = "\e[38;5;89m"
  scheme['todo']  = "\e[38;5;55m"
  lcs.scheme scheme
  @config         = @options.dup
  @config[:log_opts] = lambda{|mlll| {
      :pattern      => "%#{mlll}l: %m %g\n",
      :date_pattern => '%Y-%m-%d %H:%M:%S',
      :color_scheme => 'compiler',
      :trace        => (@config[:trace].nil? ? false : @config[:trace]),
      # [2014-06-30 Christo] DO NOT do this ... it needs to be a FixNum!!!!
      # If you want to do ::Logging.init first then fine ... go ahead :)
      # :level        => @config[:log_level],
  }
  }
  @config[:log_levels] ||= LOG_LEVELS
  @options[:log_config] = @config
  # initLogging(@config)
  @logger = getLogger(@config)

  if @options[:inifile]
    @options[:inifile] = File.expand_path(@options[:inifile])
    load_inifile
  elsif @options[:config]
    @options[:config] = File.expand_path(@options[:config])
    if @options[:config] =~ /\.ini/i
      @options[:inifile] = @options[:config]
      load_inifile
    else
      load_config
    end
  end
  if options[:debug]
    @logger.info "Options:\n#{options.ai}"
  end

end

#string_it(obj, fmtr = nil, header = false) ⇒ Object



218
219
220
221
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 218

def string_it(obj, fmtr=nil, header=false)
  fmtr ||= @formatter.call(obj, options)
  fmtr.send(header ? :header_it : :format_it, obj)
end

#validate_optionsObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 21

def validate_options
  OptionsMash.disable_warnings
  @options = OptionsMash.new(@options.to_h)
  if options[:log_level]
    log_level = options[:log_level].to_sym
    raise "Invalid log-level: #{log_level}" unless LOG_LEVELS.include?(log_level)
    options[:log_level] = log_level
  end
  @options[:log_level] ||= :warn
  @options[:format] ||= @options[:output]
  @options[:output] ||= @options[:format]
end

#write(obj) ⇒ Object



223
224
225
# File 'lib/dldinternet/thor/mixins/no_commands.rb', line 223

def write(obj)
  writer.call(obj)
end