class Dpl::Cli

Constants

STRATEGIES

Public Class Methods

new(ctx = nil, name = 'dpl') click to toggle source
Calls superclass method
# File lib/dpl/cli.rb, line 7
def self.new(ctx = nil, name = 'dpl')
  ctx ||= Dpl::Ctx::Bash.new
  super
end

Public Instance Methods

backtrace?(err) click to toggle source
# File lib/dpl/cli.rb, line 78
def backtrace?(err)
  err.respond_to?(:backtrace?) && err.backtrace?
end
error(err) click to toggle source
# File lib/dpl/cli.rb, line 72
def error(err)
  msg = "\e[31m#{err.message}\e[0m"
  msg = [msg, *err.backtrace].join("\n") if backtrace?(err)
  abort msg
end
normalize(args) click to toggle source
# File lib/dpl/cli.rb, line 26
def normalize(args)
  args = unescape(args)
  args = untaint(args)
  args = with_cmd_opts(args, provider: 0, strategy: 1)
  args = with_strategy_default(args, :strategy) # should be a generic dispatch feature in Cl
  args
end
run(args) click to toggle source
Calls superclass method
# File lib/dpl/cli.rb, line 12
def run(args)
  super
rescue UnknownCmd => e
  unknown_provider(e)
rescue UnknownOption => e
  unknown_option(e)
rescue Cl::Error, Error => e
  error(e)
end
runner(args) click to toggle source
Calls superclass method
# File lib/dpl/cli.rb, line 22
def runner(args)
  super(normalize(args))
end
suggestions(name) click to toggle source
# File lib/dpl/cli.rb, line 94
def suggestions(name)
  return [] unless defined?(DidYouMean)

  DidYouMean::SpellChecker.new(dictionary: providers).correct(name)
end
unescape(args) click to toggle source
# File lib/dpl/cli.rb, line 34
def unescape(args)
  args.map { |arg| arg.gsub('\\n', "\n") }
end
unknown_option(err) click to toggle source
# File lib/dpl/cli.rb, line 88
def unknown_option(err)
  msg = "\e[31m#{err.message}\e[0m"
  msg << "\nDid you mean: #{err.suggestions.join(', ')}?" if err.suggestions.any?
  abort msg
end
unknown_provider(err) click to toggle source
# File lib/dpl/cli.rb, line 82
def unknown_provider(err)
  msg = "\e[31m#{err.message}\e[0m"
  msg << "\nDid you mean: #{err.suggestions.join(', ')}?" if err.suggestions.any?
  abort msg
end
untaint(args) click to toggle source

Tainting is being used for automatically obfuscating values for secure options, so we want to untaint all incoming args here.

# File lib/dpl/cli.rb, line 40
def untaint(args)
  args.map(&:dup).each(&:whitelist)
end
with_cmd_opt(args, cmd, pos) click to toggle source
# File lib/dpl/cli.rb, line 50
def with_cmd_opt(args, cmd, pos)
  return args unless opt = args.detect { |arg| arg.start_with?("--#{cmd}") }

  ix = args.index(opt)
  args.delete(opt)
  value = opt.include?('=') ? opt.split('=').last : args.delete_at(ix)
  args.insert(pos, value)
  args
end
with_cmd_opts(args, cmds) click to toggle source
# File lib/dpl/cli.rb, line 44
def with_cmd_opts(args, cmds)
  cmds.inject(args) do |args, (cmd, pos)|
    with_cmd_opt(args, cmd, pos)
  end
end
with_strategy_default(args, _cmd) click to toggle source
# File lib/dpl/cli.rb, line 65
def with_strategy_default(args, _cmd)
  return args unless default = STRATEGIES[args.first]

  args.insert(1, default) if args[1].nil? || args[1].to_s.start_with?('--')
  args
end