Class: SensuCli::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/sensu-cli/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cliObject

Returns the value of attribute cli.



3
4
5
# File 'lib/sensu-cli/base.rb', line 3

def cli
  @cli
end

Instance Method Details

#api_path(cli) ⇒ Object



40
41
42
43
44
# File 'lib/sensu-cli/base.rb', line 40

def api_path(cli)
  p = PathCreator.new
  p.respond_to?(cli[:command]) ? path = p.send(cli[:command], cli) : SensuCli::die(1, 'Something Bad Happened')
  @api = { :path => path[:path], :method => cli[:method], :command => cli[:command], :payload => (path[:payload] || false) }
end

#make_callObject



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
# File 'lib/sensu-cli/base.rb', line 46

def make_call
  opts = {
    :path => @api[:path],
    :method => @api[:method],
    :payload => @api[:payload],
    :host => Config.host,
    :port => Config.port,
    :ssl => Config.ssl || false,
    :user => Config.user || nil,
    :read_timeout => Config.read_timeout || 15,
    :open_timeout => Config.open_timeout || 5,
    :password => Config.password || nil,
    :auth_token => Config.auth_token || nil,
    :proxy_address => Config.proxy_address || nil,
    :proxy_port => Config.proxy_port || nil
  }
  api = Api.new
  res = api.request(opts)
  msg = api.response(res.code, res.body, @api[:command])
  msg = Filter.new(cli[:fields][:filter]).process(msg) if cli[:fields][:filter]
  endpoint = @api[:command]
  if res.code != '200'
    SensuCli::die(0)
  elsif cli[:fields][:format] == 'single'
    Pretty.single(msg, endpoint)
  elsif cli[:fields][:format] == 'table'
    fields = nil || cli[:fields][:fields]
    Pretty.table(msg, endpoint, fields)
  elsif cli[:fields][:format] == 'json'
    Pretty.json(msg)
  else
    Pretty.print(msg, endpoint)
  end
  Pretty.count(msg) unless cli[:fields][:format] == 'table' or cli[:fields][:format] == 'json'
end

#settings(directory = "#{Dir.home}/.sensu", filename = 'settings.rb') ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sensu-cli/base.rb', line 22

def settings(directory = "#{Dir.home}/.sensu", filename = 'settings.rb')
  file = "#{directory}/#{filename}"
  alt = '/etc/sensu/sensu-cli/settings.rb'
  settings = Settings.new
  if settings.file?(file)
    SensuCli::Config.from_file(file)
  elsif settings.file?(alt)
    SensuCli::Config.from_file(alt)
  else
    settings.create(directory, file)
  end
  if $stdout.isatty and not Config.pretty_colors == false
    Rainbow.enabled = true
  else
    Rainbow.enabled = false
  end
end

#setupObject



4
5
6
7
8
9
10
11
12
13
# File 'lib/sensu-cli/base.rb', line 4

def setup
  self.cli = Cli.new.global
  settings
  if cli[:command] == 'socket'
    socket
  else
    api_path(cli)
    make_call
  end
end

#socketObject



15
16
17
18
19
20
# File 'lib/sensu-cli/base.rb', line 15

def socket
  socket = SensuCli::Client::Socket.new
  socket.format_message(cli[:fields]) if cli[:method] == 'create'
  socket.message = cli[:raw] if cli[:method] == 'raw'
  socket.send_udp_message
end