lib/dpl/cli.rb in dpl-1.10.17.travis.6637.6 vs lib/dpl/cli.rb in dpl-2.0.0.alpha.1
- old
+ new
@@ -1,66 +1,54 @@
-require 'dpl/error'
-require 'dpl/provider'
+require 'cl'
-module DPL
- class CLI
- def self.run(*args)
- new(args).run
+module Dpl
+ class Cli < Cl
+ def self.new(ctx = nil, name = 'dpl')
+ ctx ||= Dpl::Ctx::Bash.new
+ super
end
- OPTION_PATTERN = /\A--([a-z][a-z_\-]*)(?:=(.+))?\z/
- attr_accessor :options, :fold_count
-
- def initialize(*args)
- options = {}
- args.flatten.each do |arg|
- next options.update(arg) if arg.is_a? Hash
- die("invalid option %p" % arg) unless match = OPTION_PATTERN.match(arg)
- key = match[1].tr('-', '_').to_sym
- if options.include? key
- options[key] = Array(options[key]) << match[2]
- else
- options[key] = match[2] || true
- end
- end
-
- self.fold_count = 0
- self.options = default_options.merge(options)
+ def run(args)
+ args = untaint(args)
+ args = with_provider_opt(args)
+ super
+ rescue UnknownCmd
+ unknown_provider(args.first)
+ rescue Error => e
+ error(e)
end
- def run
- provider = Provider.new(self, options)
- provider.deploy
- rescue Error => error
- options[:debug] ? raise(error) : die(error.message)
+ # Tainting is being used for automatically obfuscating values for secure
+ # options, so we want to untaint all incoming args here.
+ def untaint(args)
+ args.map(&:dup).each(&:untaint)
end
- def fold(message)
- self.fold_count += 1
- print "travis_fold:start:dpl.#{fold_count}\r" if options[:fold]
- puts "\e[33m#{message}\e[0m"
- yield
- ensure
- print "\ntravis_fold:end:dpl.#{fold_count}\r" if options[:fold]
+ # 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 default_options
- {
- :app => File.basename(Dir.pwd),
- :key_name => %x[hostname].strip
- }
+ def error(e)
+ msg = "\e[31m#{e.message}\e[0m"
+ msg = [msg, *e.backtrace].join("\n") if e.backtrace?
+ abort msg
end
- def shell(command)
- system(command)
+ 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 die(message)
- $stderr.puts(message)
- exit 1
+ def suggestions(name)
+ return [] unless defined?(DidYouMean)
+ DidYouMean::SpellChecker.new(dictionary: providers).correct(name)
end
- def env
- ENV
+ def providers
+ Cl::Cmd.registry.keys.map(&:to_s)
end
end
end