Class: Travis::CLI::Command

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, Parser, Tools::Assets
Includes:
Tools::Assets
Defined in:
lib/travis/cli/command.rb

Direct Known Subclasses

ApiCommand, Help, Version

Constant Summary collapse

MINUTE =
60
HOUR =
3600
DAY =
86400
WEEK =
604800

Constants included from Tools::Assets

Tools::Assets::BASE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Tools::Assets

asset, asset_path

Methods included from Parser

new, on, on_initialize

Constructor Details

#initialize(options = {}) ⇒ Command

Returns a new instance of Command.



75
76
77
78
79
80
81
82
83
84
# File 'lib/travis/cli/command.rb', line 75

def initialize(options = {})
  @on_signal  = []
  @formatter  = Travis::Tools::Formatter.new
  self.output = $stdout
  self.input  = $stdin
  options.each do |key, value|
    public_send("#{key}=", value) if respond_to? "#{key}="
  end
  @arguments ||= []
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



71
72
73
# File 'lib/travis/cli/command.rb', line 71

def arguments
  @arguments
end

#configObject

Returns the value of attribute config.



71
72
73
# File 'lib/travis/cli/command.rb', line 71

def config
  @config
end

#debug(line) ⇒ Object

Returns the value of attribute debug.



71
72
73
# File 'lib/travis/cli/command.rb', line 71

def debug
  @debug
end

#force_interactiveObject

Returns the value of attribute force_interactive.



71
72
73
# File 'lib/travis/cli/command.rb', line 71

def force_interactive
  @force_interactive
end

#formatterObject

Returns the value of attribute formatter.



71
72
73
# File 'lib/travis/cli/command.rb', line 71

def formatter
  @formatter
end

#inputObject

Returns the value of attribute input.



72
73
74
# File 'lib/travis/cli/command.rb', line 72

def input
  @input
end

#outputObject

Returns the value of attribute output.



72
73
74
# File 'lib/travis/cli/command.rb', line 72

def output
  @output
end

Class Method Details

.abstractObject



58
59
60
# File 'lib/travis/cli/command.rb', line 58

def self.abstract
  @@abstract << self
end

.abstract?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/travis/cli/command.rb', line 54

def self.abstract?
  @@abstract.include? self
end

.command_nameObject



49
50
51
# File 'lib/travis/cli/command.rb', line 49

def self.command_name
  name[/[^:]*$/].downcase
end

.description(description = nil) ⇒ Object



66
67
68
69
# File 'lib/travis/cli/command.rb', line 66

def self.description(description = nil)
  @description = description if description
  @description ||= ""
end

.skip(*names) ⇒ Object



62
63
64
# File 'lib/travis/cli/command.rb', line 62

def self.skip(*names)
  names.each { |n| define_method(n) {} }
end

Instance Method Details

#check_completionObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/travis/cli/command.rb', line 151

def check_completion
  return if skip_completion_check? or !interactive?

  if config['checked_completion']
    Tools::Completion.update_completion if config['completion_version'] != Travis::VERSION
  else
    write_to($stderr) do
      next Tools::Completion.update_completion if Tools::Completion.completion_installed?
      next unless agree('Shell completion not installed. Would you like to like to install it now? ') { |q| q.default = "y" }
      Tools::Completion.install_completion
    end
  end

  config['checked_completion'] = true
  config['completion_version'] = Travis::VERSION
end

#check_rubyObject



168
169
170
171
# File 'lib/travis/cli/command.rb', line 168

def check_ruby
  return if RUBY_VERSION > '1.9.2' or skip_version_check?
  warn "Your Ruby version is outdated, please consider upgrading, as we will drop support for #{RUBY_VERSION} soon!"
end

#check_versionObject



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
# File 'lib/travis/cli/command.rb', line 125

def check_version
  last_check.clear if last_check['version'] != Travis::VERSION
  seconds_since = Time.now.to_i - last_check['at'].to_i

  return if skip_version_check?
  return if seconds_since < MINUTE

  case seconds_since
  when MINUTE .. HOUR then timeout = 0.5
  when HOUR   .. DAY  then timeout = 1.0
  when DAY    .. WEEK then timeout = 2.0
  else                     timeout = 10.0
  end

  Timeout.timeout(timeout) do
    response              = Faraday.get('https://rubygems.org/api/v1/gems/travis.json', {}, 'If-None-Match' => last_check['etag'].to_s)
    last_check['etag']    = response.headers['etag']
    last_check['version'] = JSON.parse(response.body)['version'] if response.status == 200
  end

  last_check['at'] = Time.now.to_i
  error "Outdated CLI version, run `gem install travis` or use --skip-version-check." if Travis::VERSION < last_check['version']
rescue Timeout::Error, Faraday::Error::ClientError => error
  debug "#{error.class}: #{error.message}"
end

#command_nameObject



195
196
197
# File 'lib/travis/cli/command.rb', line 195

def command_name
  self.class.command_name
end

#debug?Object

Returns the value of attribute debug.



73
74
75
# File 'lib/travis/cli/command.rb', line 73

def debug
  @debug
end

#executeObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/travis/cli/command.rb', line 173

def execute
  setup_trap
  check_ruby
  check_arity(method(:run), *arguments)
  load_config
  check_version
  check_completion
  setup
  run(*arguments)
  clear_error
  store_config
rescue Travis::Client::NotLoggedIn => e
  raise(e) if explode?
  error "#{e.message} - try running #{command("login#{endpoint_option}")}"
rescue StandardError => e
  raise(e) if explode?
  message = e.message
  message += color("\nfor a full error report, run #{command("report#{endpoint_option}")}", :error) if interactive?
  store_error(e)
  error(message)
end

#help(info = "") ⇒ Object



215
216
217
218
# File 'lib/travis/cli/command.rb', line 215

def help(info = "")
  parser.banner = usage
  self.class.description.sub(/./) { |c| c.upcase } + ".\n" + info + parser.to_s
end

#info(line) ⇒ Object



240
241
242
243
244
# File 'lib/travis/cli/command.rb', line 240

def info(line)
  write_to($stderr) do
    say color(line, :info)
  end
end

#last_checkObject



117
118
119
120
121
122
123
# File 'lib/travis/cli/command.rb', line 117

def last_check
  config['last_check'] ||= {
    # migrate from old values
    'at'   => config.delete('last_version_check'),
    'etag' => config.delete('etag')
  }
end

#on_signal(&block) ⇒ Object



246
247
248
# File 'lib/travis/cli/command.rb', line 246

def on_signal(&block)
  @on_signal << block
end

#parse(args) ⇒ Object



107
108
109
110
111
112
# File 'lib/travis/cli/command.rb', line 107

def parse(args)
  rest = parser.parse(args)
  arguments.concat(rest)
rescue OptionParser::ParseError => e
  error e.message
end

#say(data, format = nil, style = nil) ⇒ Object



220
221
222
# File 'lib/travis/cli/command.rb', line 220

def say(data, format = nil, style = nil)
  terminal.say format(data, format, style)
end

#setupObject



114
115
# File 'lib/travis/cli/command.rb', line 114

def setup
end

#terminalObject



86
87
88
# File 'lib/travis/cli/command.rb', line 86

def terminal
  @terminal ||= HighLine.new(input, output)
end

#time(info, callback = Proc.new) ⇒ Object



231
232
233
234
235
236
237
238
# File 'lib/travis/cli/command.rb', line 231

def time(info, callback = Proc.new)
  return callback.call unless debug?
  start = Time.now
  debug(info)
  callback.call
  duration = Time.now - start
  debug("  took %.2g seconds" % duration)
end

#usageObject



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/travis/cli/command.rb', line 199

def usage
  usage  = "travis #{command_name}"
  method = method(:run)
  if method.respond_to? :parameters
    method.parameters.each do |type, name|
      name = "[#{name}]"      if type == :opt
      name = "[#{name}..]" if type == :rest
      usage << " #{name}"
    end
  elsif method.arity != 0
    usage << " ..."
  end
  usage << " [options]"
  "Usage: " << color(usage, :command)
end

#write_to(io) ⇒ Object



100
101
102
103
104
105
# File 'lib/travis/cli/command.rb', line 100

def write_to(io)
  io_was, self.output = output, io
  yield
ensure
  self.output = io_was if io_was
end