lib/travis/cli/command.rb in travis-1.5.2 vs lib/travis/cli/command.rb in travis-1.5.3
- old
+ new
@@ -1,6 +1,7 @@
require 'travis/cli'
+require 'travis/tools/system'
require 'travis/tools/formatter'
require 'travis/version'
require 'highline'
require 'forwardable'
@@ -12,11 +13,11 @@
class Command
extend Parser
extend Forwardable
def_delegators :terminal, :agree, :ask, :choose
- HighLine.use_color = !CLI.windows? && $stdout.tty?
+ HighLine.use_color = !Tools::System.windows? && $stdout.tty?
HighLine.color_scheme = HighLine::ColorScheme.new do |cs|
cs[:command] = [ :bold ]
cs[:error] = [ :red ]
cs[:important] = [ :bold, :underline ]
cs[:success] = [ :green ]
@@ -28,11 +29,11 @@
c.say c.help
exit
end
on('-i', '--[no-]interactive', "be interactive and colorful") do |c, v|
- HighLine.use_color = v unless CLI.windows?
+ HighLine.use_color = v unless Tools::System.windows?
c.force_interactive = v
end
on('-E', '--[no-]explode', "don't rescue exceptions")
on('--skip-version-check', "don't check if travis client is up to date")
@@ -52,10 +53,15 @@
def self.skip(name)
define_method(name) {}
end
+ def self.description(description = nil)
+ @description = description if description
+ @description ||= ""
+ end
+
attr_accessor :arguments, :config, :force_interactive, :formatter
attr_reader :input, :output
def initialize(options = {})
@on_signal = []
@@ -97,27 +103,32 @@
end
def setup
end
+ def last_check
+ config['last_check'] ||= {
+ # migrate from old values
+ 'at' => config.delete('last_version_check'),
+ 'etag' => config.delete('etag')
+ }
+ end
+
def check_version
+ last_check.clear if last_check['version'] != Travis::VERSION
+
return if skip_version_check?
- return if Time.now.to_i - config['last_version_check'].to_i < 3600
- version = Travis::VERSION
+ return if Time.now.to_i - last_check['at'].to_i < 3600
Timeout.timeout 1.0 do
- response = Faraday.get('https://rubygems.org/api/v1/gems/travis.json', {}, 'If-None-Match' => config['etag'].to_s)
- config['etag'] = response.headers['etag']
- version = JSON.parse(response.body)['version'] if response.status == 200
+ 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
- if Travis::VERSION >= version
- config['last_version_check'] = Time.now.to_i
- else
- error "Outdated CLI version (#{Travis::VERSION}, current is #{version}), " \
- "run `gem install travis -v #{version}` or use --skip-version-check."
- 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
end
def check_ruby
return if RUBY_VERSION > '1.9.2' or skip_version_check?
@@ -143,11 +154,11 @@
def command_name
self.class.command_name
end
def usage
- usage = "#$0 #{command_name}"
+ 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
@@ -160,10 +171,10 @@
"Usage: " << color(usage, :command)
end
def help
parser.banner = usage
- parser.to_s
+ self.class.description.sub(/./) { |c| c.upcase } + ".\n" + parser.to_s
end
def say(data, format = nil, style = nil)
terminal.say format(data, format, style)
end