Class: Travis::CLI::Command

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

Direct Known Subclasses

ApiCommand, Help, Version

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Parser

new, on, on_initialize

Constructor Details

#initialize(options = {}) ⇒ Command

Returns a new instance of Command.



66
67
68
69
70
71
72
73
74
75
# File 'lib/travis/cli/command.rb', line 66

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.



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

def arguments
  @arguments
end

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#force_interactiveObject

Returns the value of attribute force_interactive.



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

def force_interactive
  @force_interactive
end

#formatterObject

Returns the value of attribute formatter.



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

def formatter
  @formatter
end

#inputObject

Returns the value of attribute input.



64
65
66
# File 'lib/travis/cli/command.rb', line 64

def input
  @input
end

#outputObject

Returns the value of attribute output.



64
65
66
# File 'lib/travis/cli/command.rb', line 64

def output
  @output
end

Class Method Details

.abstractObject



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

def self.abstract
  @@abstract << self
end

.abstract?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/travis/cli/command.rb', line 46

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

.command_nameObject



41
42
43
# File 'lib/travis/cli/command.rb', line 41

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

.description(description = nil) ⇒ Object



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

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

.skip(name) ⇒ Object



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

def self.skip(name)
  define_method(name) {}
end

Instance Method Details

#check_rubyObject



133
134
135
136
# File 'lib/travis/cli/command.rb', line 133

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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/travis/cli/command.rb', line 116

def check_version
  last_check.clear if last_check['version'] != Travis::VERSION

  return if skip_version_check?
  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' => 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
end

#command_nameObject



154
155
156
# File 'lib/travis/cli/command.rb', line 154

def command_name
  self.class.command_name
end

#debug(line) ⇒ Object



183
184
185
186
187
# File 'lib/travis/cli/command.rb', line 183

def debug(line)
  write_to($stderr) do
    say color("** #{line}", :debug)
  end
end

#executeObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/travis/cli/command.rb', line 138

def execute
  setup_trap
  check_ruby
  check_arity(method(:run), *arguments)
  load_config
  check_version
  setup
  run(*arguments)
  store_config
rescue StandardError => e
  raise(e) if explode?
  message = e.message
  message += " - need to run `travis login` again?" if Travis::Client::Error === e and message == 'access denied'
  error message
end

#help(info = "") ⇒ Object



174
175
176
177
# File 'lib/travis/cli/command.rb', line 174

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

#info(line) ⇒ Object



189
190
191
192
193
# File 'lib/travis/cli/command.rb', line 189

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

#last_checkObject



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

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



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

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

#parse(args) ⇒ Object



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

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



179
180
181
# File 'lib/travis/cli/command.rb', line 179

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

#setupObject



105
106
# File 'lib/travis/cli/command.rb', line 105

def setup
end

#terminalObject



77
78
79
# File 'lib/travis/cli/command.rb', line 77

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

#usageObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/travis/cli/command.rb', line 158

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



91
92
93
94
95
96
# File 'lib/travis/cli/command.rb', line 91

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