Class: Travis::CLI::Monitor

Inherits:
ApiCommand show all
Defined in:
lib/travis/cli/monitor.rb

Instance Attribute Summary collapse

Attributes inherited from ApiCommand

#session

Attributes inherited from Command

#arguments, #config, #force_interactive, #formatter, #input, #output

Instance Method Summary collapse

Methods inherited from ApiCommand

#authenticate, #detected_endpoint?, #endpoint_config, #org?, #pro?, #sync

Methods included from Travis::Client::Methods

#access_token, #access_token=, #accounts, #api_endpoint, #api_endpoint=, #artifact, #broadcasts, #build, #cancel, #explicit_api_endpoint?, #github_auth, #job, #listen, #repo, #restart, #user, #worker, #workers

Methods inherited from Command

abstract, abstract?, #check_ruby, #check_version, #command_name, command_name, #debug, description, #execute, #help, #info, #last_check, #on_signal, #parse, #say, skip, #terminal, #usage, #write_to

Methods included from Parser

#new, #on, #on_initialize

Constructor Details

#initializeMonitor

Returns a new instance of Monitor.



21
22
23
24
# File 'lib/travis/cli/monitor.rb', line 21

def initialize(*)
  @repos = []
  super
end

Instance Attribute Details

#notificationObject (readonly)

Returns the value of attribute notification.



19
20
21
# File 'lib/travis/cli/monitor.rb', line 19

def notification
  @notification
end

#reposObject (readonly)

Returns the value of attribute repos.



19
20
21
# File 'lib/travis/cli/monitor.rb', line 19

def repos
  @repos
end

Instance Method Details

#descriptionObject



48
49
50
51
52
53
54
# File 'lib/travis/cli/monitor.rb', line 48

def description
  case repos.size
  when 0 then session.config['host']
  when 1 then repos.first.slug
  else "#{repos.size} repositories"
  end
end

#runObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/travis/cli/monitor.rb', line 56

def run
  listen(*repos) do |listener|
    listener.on_connect { say description, 'Monitoring %s:' }
    listener.on 'build:started', 'job:started', 'build:finished', 'job:finished' do |event|
      entity = event.job          || event.build
      time   = entity.finished_at || entity.started_at
      say [
        color(formatter.time(time), entity.color),
        color(entity.inspect_info, [entity.color, :bold]),
        color(entity.state, entity.color)
      ].join(" ")
      notification.notify("Travis CI", "#{entity.inspect_info} #{entity.state}")
    end
  end
end

#setupObject



26
27
28
29
30
31
# File 'lib/travis/cli/monitor.rb', line 26

def setup
  super
  repos.map! { |r| repo(r) }
  repos.concat(user.repositories) if my_repos?
  setup_notification(repos.any? || :dummy) unless notification
end

#setup_notification(type = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/travis/cli/monitor.rb', line 33

def setup_notification(type = nil)
  refuse = false
  case type
  when false     then @notification = Tools::Notification.new(:dummy)
  when nil, true then @notification = Tools::Notification.new
  else
    refuse        = true
    @notification = Tools::Notification.new(type)
  end
rescue ArgumentError => e
  @notification = Tools::Notification.new(:dummy)
  error(e.message) if refuse
  warn(e.message)
end