Class: ConfCtl::Cli::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/confctl/cli/command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(global_opts, opts, args) ⇒ Command



30
31
32
33
34
35
36
# File 'lib/confctl/cli/command.rb', line 30

def initialize(global_opts, opts, args)
  @gopts = global_opts
  @opts = opts
  @args = args
  @use_color = determine_color
  @use_pager = determine_pager
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



28
29
30
# File 'lib/confctl/cli/command.rb', line 28

def args
  @args
end

#goptsObject (readonly)

Returns the value of attribute gopts.



28
29
30
# File 'lib/confctl/cli/command.rb', line 28

def gopts
  @gopts
end

#optsObject (readonly)

Returns the value of attribute opts.



28
29
30
# File 'lib/confctl/cli/command.rb', line 28

def opts
  @opts
end

Class Method Details

.run(gli_cmd, klass, method) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/confctl/cli/command.rb', line 6

def self.run(gli_cmd, klass, method)
  proc do |global_opts, opts, args|
    log = ConfCtl::Logger.instance
    log.open(gli_cmd.name_for_help.join('-'))
    log.cli(
      gli_cmd.name_for_help,
      global_opts,
      opts,
      args
    )

    cmd = klass.new(global_opts, opts, args)
    cmd.run_method(method)

    if log.keep?
      log.close
    else
      log.close_and_unlink
    end
  end
end

Instance Method Details

#ask_action(options:, default:) ⇒ String



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/confctl/cli/command.rb', line 100

def ask_action(options:, default:)
  yield if block_given?

  loop do
    $stdout.puts("\nOptions:\n")

    options.each do |key, desc|
      $stdout.puts("  [#{key}] #{desc}")
    end

    keys = options.keys.map do |k|
      if k == default
        k.upcase
      else
        k
      end
    end.join('/')

    $stdout.write("\nAction: [#{keys}]: ")
    $stdout.flush

    answer = $stdin.readline.strip.downcase

    if options.has_key?(answer)
      puts
      return answer
    end
  end
end

#ask_confirmation(always: false) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/confctl/cli/command.rb', line 73

def ask_confirmation(always: false)
  return true if !always && opts[:yes]

  yield if block_given?

  loop do
    $stdout.write("\nContinue? [y/N]: ")
    $stdout.flush

    case $stdin.readline.strip.downcase
    when 'y'
      puts
      return true
    when 'n'
      puts
      return false
    end
  end
end

#ask_confirmation!Object



93
94
95
# File 'lib/confctl/cli/command.rb', line 93

def ask_confirmation!(...)
  raise 'Aborted' unless ask_confirmation(...)
end

#require_args!(*required, optional: [], strict: true) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/confctl/cli/command.rb', line 41

def require_args!(*required, optional: [], strict: true)
  if args.count < required.count
    arg = required[args.count]
    raise GLI::BadCommandLine, "missing argument <#{arg}>"

  elsif strict && args.count > (required.count + optional.count)
    unknown = args[(required.count + optional.count)..]

    msg = ''

    msg << if unknown.count > 1
             'unknown arguments: '
           else
             'unknown argument: '
           end

    msg << unknown.join(' ')

    msg << ' (note that options must come before arguments)' if unknown.detect { |v| v.start_with?('-') }

    raise GLI::BadCommandLine, msg
  end
end

#run_method(method) ⇒ Object



130
131
132
# File 'lib/confctl/cli/command.rb', line 130

def run_method(method)
  self.method(method).call
end

#use_color?Boolean



65
66
67
# File 'lib/confctl/cli/command.rb', line 65

def use_color?
  @use_color
end

#use_pager?Boolean



69
70
71
# File 'lib/confctl/cli/command.rb', line 69

def use_pager?
  @use_pager
end