Class: Puppetserver::Ca::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/puppetserver/ca/cli.rb

Constant Summary collapse

<<-BANNER
Usage: puppetserver ca <action> [options]

Manage the Private Key Infrastructure for
Puppet Server's built-in Certificate Authority
BANNER
VALID_ACTIONS =
{
  'clean'    => CleanAction,
  'create'   => CreateAction,
  'generate' => GenerateAction,
  'import'   => ImportAction,
  'list'     => ListAction,
  'revoke'   => RevokeAction,
  'sign'     => SignAction
}
ACTION_LIST =
"\nAvailable Actions:\n" +
VALID_ACTIONS.map do |action, cls|
  "    #{action}\t#{cls::SUMMARY}"
end.join("\n")
ACTION_OPTIONS =
"\nAction Options:\n" +
VALID_ACTIONS.map do |action, cls|
  action_summary = cls.parser.summarize.
                     select{|line| line =~ /^\s*--/ }.
                     reject{|line| line =~ /--help|--version/ }
  summary = action_summary.empty? ? '      N/A' : action_summary.join('')

  "  #{action}:\n" + summary
end.join("\n")

Class Method Summary collapse

Class Method Details

.parse_general_inputs(inputs) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/puppetserver/ca/cli.rb', line 87

def self.parse_general_inputs(inputs)
  parsed = {}
  general_parser = OptionParser.new do |opts|
    opts.banner = BANNER
    opts.separator ACTION_LIST
    opts.separator "\nGeneral Options:"

    opts.on('--help', 'Display this general help output') do |help|
      parsed['help'] = true
    end
    opts.on('--version', 'Display the version') do |v|
      parsed['version'] = true
    end

    opts.separator ACTION_OPTIONS
    opts.separator "\nSee `puppetserver ca <action> --help` for detailed info"

  end

  all,_,_,_ = Utils.parse_without_raising(general_parser, inputs)

  return general_parser, parsed, all
end

.run(cli_args = ARGV, out = STDOUT, err = STDERR) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/puppetserver/ca/cli.rb', line 49

def self.run(cli_args = ARGV, out = STDOUT, err = STDERR)
  logger = Puppetserver::Ca::Logger.new(:info, out, err)
  parser, general_options, unparsed = parse_general_inputs(cli_args)

  if general_options['version']
    logger.inform Puppetserver::Ca::VERSION
    return 0
  end

  action_argument = unparsed.shift
  action_class = VALID_ACTIONS[action_argument]

  if general_options['help']
    if action_class
      logger.inform action_class.parser.help
    else
      logger.inform parser.help
    end

    return 0
  end

  if action_class
    action = action_class.new(logger)
    input, exit_code = action.parse(unparsed)

    if exit_code
      return exit_code
    else
      return action.run(input)
    end
  else
    logger.warn "Unknown action: #{action_argument}"
    logger.warn parser.help
    return 1
  end
end