lib/travis/cli.rb in travis-1.11.1 vs lib/travis/cli.rb in travis-1.12.0
- old
+ new
@@ -1,14 +1,14 @@
+# frozen_string_literal: true
+
begin
require 'travis/client'
rescue LoadError => e
- if e.message == 'no such file to load -- json'
- $stderr.puts "You should either run `gem install json` or upgrade your Ruby version!"
- exit 1
- else
- raise e
- end
+ raise e unless e.message == 'no such file to load -- json'
+
+ warn 'You should either run `gem install json` or upgrade your Ruby version!'
+ exit 1
end
require 'stringio'
module Travis
@@ -64,63 +64,66 @@
command.execute
end
def command(name)
const_name = command_name(name)
- constant = CLI.const_get(const_name) if const_name =~ /^[A-Z][A-Za-z]+$/ and const_defined? const_name
+ constant = CLI.const_get(const_name) if const_name =~ (/^[A-Z][A-Za-z]+$/) && const_defined?(const_name)
if command? constant
constant
else
- $stderr.puts "unknown command #{name}"
+ warn "unknown command #{name}"
exit 1
end
end
def commands
CLI.constants.map { |n| try_const_get(n) }.select { |c| command? c }
end
def silent
- stderr, $stderr = $stderr, dummy_io
- stdout, $stdout = $stdout, dummy_io
+ stderr = $stderr
+ $stderr = dummy_io
+ stdout = $stdout
+ $stdout = dummy_io
yield
ensure
$stderr = stderr if stderr
$stdout = stdout if stdout
end
private
- def try_const_get(name)
- CLI.const_get(name)
- rescue Exception
- end
+ def try_const_get(name)
+ CLI.const_get(name)
+ rescue Exception
+ end
- def dummy_io
- return StringIO.new unless defined? IO::NULL and IO::NULL
- File.open(IO::NULL, 'w')
- end
+ def dummy_io
+ return StringIO.new unless defined? IO::NULL && IO::NULL
- def command?(constant)
- constant.is_a? Class and constant < Command and not constant.abstract?
- end
+ File.open(IO::NULL, 'w')
+ end
- def command_name(name)
- case name
- when nil, '-h', '-?' then 'Help'
- when '-v' then 'Version'
- when /^--/ then command_name(name[2..-1])
- else name.split('-').map(&:capitalize).join
- end
+ def command?(constant)
+ constant.is_a? Class and constant < Command and !constant.abstract?
+ end
+
+ def command_name(name)
+ case name
+ when nil, '-h', '-?' then 'Help'
+ when '-v' then 'Version'
+ when /^--/ then command_name(name[2..])
+ else name.split('-').map(&:capitalize).join
end
+ end
- # can't use flatten as it will flatten hashes
- def preparse(unparsed, args = [], opts = {})
- case unparsed
- when Hash then opts.merge! unparsed
- when Array then unparsed.each { |e| preparse(e, args, opts) }
- else args << unparsed.to_s
- end
- [args, opts]
+ # can't use flatten as it will flatten hashes
+ def preparse(unparsed, args = [], opts = {})
+ case unparsed
+ when Hash then opts.merge! unparsed
+ when Array then unparsed.each { |e| preparse(e, args, opts) }
+ else args << unparsed.to_s
end
+ [args, opts]
+ end
end
end