Class: Dpl::Cli

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

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

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



7
8
9
10
# File 'lib/dpl/cli.rb', line 7

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

Instance Method Details

#backtrace?(err) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#error(err) ⇒ Object



72
73
74
75
76
# 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) ⇒ Object



26
27
28
29
30
31
32
# 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) ⇒ Object



12
13
14
15
16
17
18
19
20
# 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) ⇒ Object



22
23
24
# File 'lib/dpl/cli.rb', line 22

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

#suggestions(name) ⇒ Object



94
95
96
97
98
# File 'lib/dpl/cli.rb', line 94

def suggestions(name)
  return [] unless defined?(DidYouMean)

  DidYouMean::SpellChecker.new(dictionary: providers).correct(name)
end

#unescape(args) ⇒ Object



34
35
36
# File 'lib/dpl/cli.rb', line 34

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

#unknown_option(err) ⇒ Object



88
89
90
91
92
# 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) ⇒ Object



82
83
84
85
86
# 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) ⇒ Object

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



40
41
42
# File 'lib/dpl/cli.rb', line 40

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

#with_cmd_opt(args, cmd, pos) ⇒ Object



50
51
52
53
54
55
56
57
58
# 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) ⇒ Object



44
45
46
47
48
# 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) ⇒ Object



65
66
67
68
69
70
# 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