Skip to content

Commit

Permalink
Merge pull request #1039 from travis-ci/sf-port-856
Browse files Browse the repository at this point in the history
move rescueing errors to Dpl::Cli, and suggest provider names on unknown providers (ports #856)
  • Loading branch information
svenfuchs committed Aug 21, 2019
2 parents 291f0ac + 256284c commit aee4ba4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
8 changes: 1 addition & 7 deletions bin/dpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,4 @@ $stderr.sync = true

require 'dpl'

begin
Dpl::Cli.new.run(ARGV)
rescue Dpl::Error => e
msg = "\e[31m#{e.message}\e[0m"
msg = [msg, *e.backtrace].join("\n") if e.backtrace?
abort msg
end
Dpl::Cli.new.run(ARGV)
6 changes: 3 additions & 3 deletions dpl.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ Gem::Specification.new do |s|
s.require_path = 'lib'
s.required_ruby_version = '>= 2.2'

s.executables = ['dpl']
s.files = Dir['{lib/**/*,[A-Z]*}']
s.executables = ['dpl']
s.files = Dir['{lib/**/*,[A-Z]*}']

s.add_runtime_dependency 'cl', '~> 1.0.0'
s.add_runtime_dependency 'cl', '~> 1.0'
s.add_development_dependency 'rake', '~> 12.3'
end
32 changes: 29 additions & 3 deletions lib/dpl/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

module Dpl
class Cli < Cl
def self.new(ctx = nil, cmd = nil)
super(ctx || Dpl::Ctx::Bash.new, cmd || 'dpl')
def self.new(ctx = nil, name = 'dpl')
ctx ||= Dpl::Ctx::Bash.new
super
end

def run(args)
args = untaint(args)
args = with_provider_opt(args)
super
rescue UnknownCmd
unknown_provider(args.first)
rescue Error => e
error(e)
end

# Tainting is being used for automatically obfuscating values for secure
Expand All @@ -18,11 +23,32 @@ def untaint(args)
args.map(&:dup).each(&:untaint)
end

# bc with travis-build dpl v1 integration
# backwards compatibility for travis-build dpl v1 integration
def with_provider_opt(args)
return args unless arg = args.detect { |arg| arg.include?('--provider') }
args.delete(arg)
[arg.split('=').last, *args]
end

def error(e)
msg = "\e[31m#{e.message}\e[0m"
msg = [msg, *e.backtrace].join("\n") if e.backtrace?
abort msg
end

def unknown_provider(name)
msg = "\e[31mUnknown provider: #{name}\e[0m"
msg << "\nDid you mean: #{suggestions(name).join(', ')}?" if suggestions(name).any?
abort msg
end

def suggestions(name)
return [] unless defined?(DidYouMean)
DidYouMean::SpellChecker.new(dictionary: providers).correct(name)
end

def providers
Cl::Cmd.registry.keys.map(&:to_s)
end
end
end
2 changes: 1 addition & 1 deletion lib/dpl/ctx/bash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Bash < Cl::Ctx
def initialize(stdout = $stdout, stderr = $stderr)
@stdout, @stderr = stdout, stderr
@folds = 0
super('dpl')
super('dpl', abort: false)
end

# Folds any log output from the given block
Expand Down

0 comments on commit aee4ba4

Please sign in to comment.