Class: Dpl::Cli

Inherits:
Cl
  • Object
show all
Defined in:
lib/dpl/cli.rb

Constant Summary collapse

STRATEGIES =
{
  'heroku' => 'api',
  'pages'  => 'git'
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(ctx = nil, name = 'dpl') ⇒ Object



5
6
7
8
# File 'lib/dpl/cli.rb', line 5

def self.new(ctx = nil, name = 'dpl')
  ctx ||= Dpl::Ctx::Bash.new
  super
end

Instance Method Details

#backtrace?(e) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/dpl/cli.rb', line 74

def backtrace?(e)
  e.respond_to?(:backtrace?) && e.backtrace?
end

#error(e) ⇒ Object



68
69
70
71
72
# File 'lib/dpl/cli.rb', line 68

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

#normalize(args) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/dpl/cli.rb', line 24

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) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/dpl/cli.rb', line 10

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) ⇒ Object



20
21
22
# File 'lib/dpl/cli.rb', line 20

def runner(args)
  super(normalize(args))
end

#suggestions(name) ⇒ Object



90
91
92
93
# File 'lib/dpl/cli.rb', line 90

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

#unescape(args) ⇒ Object



32
33
34
# File 'lib/dpl/cli.rb', line 32

def unescape(args)
  args.map { |arg| arg.gsub('\\n', "\n") }
end

#unknown_option(e) ⇒ Object



84
85
86
87
88
# File 'lib/dpl/cli.rb', line 84

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

#unknown_provider(e) ⇒ Object



78
79
80
81
82
# File 'lib/dpl/cli.rb', line 78

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

#untaint(args) ⇒ Object

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



38
39
40
# File 'lib/dpl/cli.rb', line 38

def untaint(args)
  args.map(&:dup).each(&:whitelist)
end

#with_cmd_opt(args, cmd, pos) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/dpl/cli.rb', line 48

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) ⇒ Object



42
43
44
45
46
# File 'lib/dpl/cli.rb', line 42

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) ⇒ Object



62
63
64
65
66
# File 'lib/dpl/cli.rb', line 62

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