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.



60
61
62
63
64
65
66
67
68
# File 'lib/travis/cli/command.rb', line 60

def initialize(options = {})
  @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.



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

def arguments
  @arguments
end

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#force_interactiveObject

Returns the value of attribute force_interactive.



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

def force_interactive
  @force_interactive
end

#formatterObject

Returns the value of attribute formatter.



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

def formatter
  @formatter
end

#inputObject

Returns the value of attribute input.



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

def input
  @input
end

#outputObject

Returns the value of attribute output.



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

def output
  @output
end

Class Method Details

.abstractObject



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

def self.abstract
  @@abstract << self
end

.abstract?Boolean

Returns:

  • (Boolean)


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

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

.command_nameObject



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

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

.skip(name) ⇒ Object



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

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

Instance Method Details

#check_rubyObject



121
122
123
124
# File 'lib/travis/cli/command.rb', line 121

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/travis/cli/command.rb', line 101

def check_version
  return if skip_version_check?
  return if Time.now.to_i - config['last_version_check'].to_i < 3600
  version = Travis::VERSION

  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
  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
rescue Timeout::Error, Faraday::Error::ClientError
end

#command_nameObject



141
142
143
# File 'lib/travis/cli/command.rb', line 141

def command_name
  self.class.command_name
end

#debug(line) ⇒ Object



170
171
172
173
174
# File 'lib/travis/cli/command.rb', line 170

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

#executeObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/travis/cli/command.rb', line 126

def execute
  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

#helpObject



161
162
163
164
# File 'lib/travis/cli/command.rb', line 161

def help
  parser.banner = usage
  parser.to_s
end

#parse(args) ⇒ Object



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

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



166
167
168
# File 'lib/travis/cli/command.rb', line 166

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

#setupObject



98
99
# File 'lib/travis/cli/command.rb', line 98

def setup
end

#terminalObject



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

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

#usageObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/travis/cli/command.rb', line 145

def usage
  usage  = "#$0 #{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



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

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