Class: Razor::CLI::Parse
- Inherits:
-
Object
- Object
- Razor::CLI::Parse
- Extended by:
- Forwardable
- Defined in:
- lib/razor/cli/parse.rb
Constant Summary collapse
- DEFAULT_RAZOR_API =
"http://localhost:8150/api"- LINUX_PEM_FILE =
'/etc/puppetlabs/puppet/ssl/certs/ca.pem'- WIN_PEM_FILE =
'C:\ProgramData\PuppetLabs\puppet\etc\ssl\ccerts\cca.pem'
Instance Attribute Summary collapse
-
#api_url ⇒ Object
readonly
Returns the value of attribute api_url.
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#format ⇒ Object
The format can be determined from later segments.
-
#ssl_ca_file ⇒ Object
The format can be determined from later segments.
-
#stripped_args ⇒ Object
The format can be determined from later segments.
Instance Method Summary collapse
- #dump_response? ⇒ Boolean
- #get_optparse ⇒ Object
- #help ⇒ Object
-
#initialize(args) ⇒ Parse
constructor
A new instance of Parse.
- #list_things(name, items) ⇒ Object
- #navigate ⇒ Object
-
#set_help_vars(rest) ⇒ Object
This method sets the appropriate help flags ‘@command_help` and `@option_help`, then returns a new set of arguments.
- #show_api_help? ⇒ Boolean
- #show_command_help? ⇒ Boolean
- #show_help? ⇒ Boolean
- #show_version? ⇒ Boolean
- #verify_ssl? ⇒ Boolean
- #version ⇒ Object
Constructor Details
#initialize(args) ⇒ Parse
Returns a new instance of Parse.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/razor/cli/parse.rb', line 160 def initialize(args) parse_and_set_api_url(ENV["RAZOR_API"] || DEFAULT_RAZOR_API, :env) @args = args.dup # To be populated externally. @stripped_args = [] @format = 'short' @verify_ssl = true env_pem_file = ENV['RAZOR_CA_FILE'] # If this is set, it should actually exist. if env_pem_file && !File.exists?(env_pem_file) raise Razor::CLI::InvalidCAFileError.new(env_pem_file) end pem_file_locations = [env_pem_file, LINUX_PEM_FILE, WIN_PEM_FILE] pem_file_locations.each do |file| if file && File.exists?(file) @ssl_ca_file = file break end end @args = get_optparse.order(@args) # Localhost won't match the server's certificate; no verification required. # This needs to happen after get_optparse so `-k` and `-u` can take effect. if @api_url.hostname == 'localhost' @verify_ssl = false end @args = set_help_vars(@args) if @args == ['version'] or @show_version @show_version = true elsif @args.any? = @args.dup else # Called with no remaining arguments to parse. @option_help = true end end |
Instance Attribute Details
#api_url ⇒ Object (readonly)
Returns the value of attribute api_url.
154 155 156 |
# File 'lib/razor/cli/parse.rb', line 154 def api_url @api_url end |
#args ⇒ Object (readonly)
Returns the value of attribute args.
154 155 156 |
# File 'lib/razor/cli/parse.rb', line 154 def args @args end |
#format ⇒ Object
The format can be determined from later segments.
156 157 158 |
# File 'lib/razor/cli/parse.rb', line 156 def format @format end |
#ssl_ca_file ⇒ Object
The format can be determined from later segments.
156 157 158 |
# File 'lib/razor/cli/parse.rb', line 156 def ssl_ca_file @ssl_ca_file end |
#stripped_args ⇒ Object
The format can be determined from later segments.
156 157 158 |
# File 'lib/razor/cli/parse.rb', line 156 def stripped_args @stripped_args end |
Instance Method Details
#dump_response? ⇒ Boolean
146 147 148 |
# File 'lib/razor/cli/parse.rb', line 146 def dump_response? !!@dump end |
#get_optparse ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/razor/cli/parse.rb', line 23 def get_optparse @optparse ||= OptionParser.new do |opts| opts. = _("Usage: razor [FLAGS] NAVIGATION\n") opts.on "-d", "--dump", _("Dumps API output to the screen") do @dump = true end opts.on "-a", "--api", _("Show API help for a command") do @api_help = true end opts.on "-k", "--insecure", _("Allow SSL connections without verified certificates") do @verify_ssl = false end opts.on "-u", "--url URL", _("The full Razor API URL, can also be set\n" + " "*37 + "with the RAZOR_API environment variable\n" + " "*37 + "(default %{default_api})") % {default_api: DEFAULT_RAZOR_API} do |url| parse_and_set_api_url(url, :opts) end opts.on "-v", "--version", _("Show the version of Razor") do @show_version = true end opts.on "-h", "--help", _("Show this screen") do # If searching for a command's help, leave the argument for navigation. @option_help = true end end end |
#help ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 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/razor/cli/parse.rb', line 82 def help output = get_optparse.to_s exit = 0 begin replacements = {collections: list_things(_("Collections"), navigate.collections), commands: list_things(_("Commands"), navigate.commands)} output << _("%{collections}\n\n Navigate to entries of a collection using COLLECTION NAME, for example,\n 'nodes node15' for the details of a node or 'nodes node15 log' to see\n the log for node15\n%{commands}\n\n Pass arguments to commands either directly by name ('--name=NAME')\n or save the JSON body for the command in a file and pass it with\n '--json FILE'. Using --json is the only way to pass arguments in\n nested structures such as the configuration for a broker.\n\n") % replacements rescue RestClient:: output << _("Error: Credentials are required to connect to the server at %{url}\"\n") % {url: @api_url} exit = 1 rescue SocketError, Errno::ECONNREFUSED => e puts _("Error: Could not connect to the server at %{url}") % {url: @api_url} puts " #{e}\n" die rescue RestClient::SSLCertificateNotVerified puts _("Error: SSL certificate could not be verified against known CA certificates.\n" + " To turn off verification, use the -k or --insecure option.") die rescue OpenSSL::SSL::SSLError => e # Occurs in case of e.g. certificate mismatch (FQDN vs. hostname) puts _("Error: SSL certificate error from server at %{url}") % {url: @api_url} puts " #{e}" die rescue => e output << _("Error: Unknown error occurred while connecting to server at %{url}:\n \#{e}\n") % {url: @api_url} exit = 1 end [output, exit] end |
#list_things(name, items) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/razor/cli/parse.rb', line 58 def list_things(name, items) "\n #{name}:\n" + items.map {|x| x["name"]}.compact.sort.map do |name| " #{name}" end.join("\n") end |
#navigate ⇒ Object
218 219 220 |
# File 'lib/razor/cli/parse.rb', line 218 def navigate @navigate ||=Navigate.new(self, ) end |
#set_help_vars(rest) ⇒ Object
This method sets the appropriate help flags ‘@command_help` and `@option_help`, then returns a new set of arguments.
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/razor/cli/parse.rb', line 200 def set_help_vars(rest) # Find and remove 'help' variations anywhere in the command. if rest.any? { |arg| ['-h', '--help'].include? arg } or rest.first == 'help' or rest.drop(1).first == 'help' rest = rest.reject { |arg| ['-h', '--help', 'help'].include? arg } # If anything is left, assume it is a command. if rest.any? @command_help = true else @option_help = true end end if @option_help && rest.any? @command_help = true end rest end |
#show_api_help? ⇒ Boolean
134 135 136 |
# File 'lib/razor/cli/parse.rb', line 134 def show_api_help? !!@api_help end |
#show_command_help? ⇒ Boolean
142 143 144 |
# File 'lib/razor/cli/parse.rb', line 142 def show_command_help? !!@command_help end |
#show_help? ⇒ Boolean
138 139 140 |
# File 'lib/razor/cli/parse.rb', line 138 def show_help? !!@option_help end |
#show_version? ⇒ Boolean
130 131 132 |
# File 'lib/razor/cli/parse.rb', line 130 def show_version? !!@show_version end |
#verify_ssl? ⇒ Boolean
150 151 152 |
# File 'lib/razor/cli/parse.rb', line 150 def verify_ssl? !!@verify_ssl end |
#version ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/razor/cli/parse.rb', line 65 def version server_version = '(unknown)' error = '' begin server_version = navigate.server_version rescue RestClient:: error = _("Error: Credentials are required to connect to the server at %{url}.") % {url: @api_url} rescue error = _("Error: Could not connect to the server at %{url}.") % {url: @api_url} ensure return [(_(" Razor Server version: \#{server_version}\n Razor Client version: \#{Razor::CLI::VERSION}\n OUTPUT\n end\nend\n") % {server_version: server_version, client_version: Razor::CLI::VERSION} + "\n" + error).rstrip, error != '' ? 1 : 0] |