Module: Rubyists::Leopard::NatsServiceDiscovery::CLI Private
- Defined in:
- lib/leopard/nats_service_discovery/cli.rb
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Shared command-line helpers for NATS service discovery executables.
Class Method Summary collapse
-
.abort_with(error) ⇒ void
private
Prints an error and exits with a non-zero status.
-
.add_common_options(parser, opts) ⇒ void
private
Adds all common options to an OptionParser.
-
.add_connection_options(parser, opts) ⇒ void
private
Adds NATS connection options to an OptionParser.
-
.add_filter_options(parser, opts) ⇒ void
private
Adds service filter options to an OptionParser.
-
.add_output_options(parser, opts) ⇒ void
private
Adds output and help options to an OptionParser.
-
.column_widths(headers, rows) ⇒ Array<Integer>
private
Computes table column widths.
-
.default_options(json:) ⇒ Hash
private
Builds default CLI options.
-
.discovery_options(opts, client) ⇒ Hash
private
Builds discovery operation options from CLI options and a client.
-
.operation_result!(result) ⇒ Trailblazer::Operation::Railway::Result
private
Returns a successful operation result or aborts the process.
-
.parse(argv, banner:, description:, json: false) ⇒ Hash
private
Parses common NATS discovery CLI options.
-
.print_json(value) ⇒ void
private
Prints a value as pretty JSON.
-
.print_table(headers, rows) ⇒ void
private
Prints rows as a simple aligned table.
-
.validate!(opts) ⇒ void
private
Validates parsed CLI options.
-
.with_client(opts) {|client| ... } ⇒ Object
private
Opens a NATS client for the duration of the provided block.
Instance Method Summary collapse
-
#abort_with(error) ⇒ void
private
private
Prints an error and exits with a non-zero status.
-
#add_common_options(parser, opts) ⇒ void
private
private
Adds all common options to an OptionParser.
-
#add_connection_options(parser, opts) ⇒ void
private
private
Adds NATS connection options to an OptionParser.
-
#add_filter_options(parser, opts) ⇒ void
private
private
Adds service filter options to an OptionParser.
-
#add_output_options(parser, opts) ⇒ void
private
private
Adds output and help options to an OptionParser.
-
#column_widths(headers, rows) ⇒ Array<Integer>
private
private
Computes table column widths.
-
#default_options(json:) ⇒ Hash
private
private
Builds default CLI options.
-
#discovery_options(opts, client) ⇒ Hash
private
private
Builds discovery operation options from CLI options and a client.
-
#operation_result!(result) ⇒ Trailblazer::Operation::Railway::Result
private
private
Returns a successful operation result or aborts the process.
-
#parse(argv, banner:, description:, json: false) ⇒ Hash
private
private
Parses common NATS discovery CLI options.
-
#print_json(value) ⇒ void
private
private
Prints a value as pretty JSON.
-
#print_table(headers, rows) ⇒ void
private
private
Prints rows as a simple aligned table.
-
#validate!(opts) ⇒ void
private
private
Validates parsed CLI options.
-
#with_client(opts) {|client| ... } ⇒ Object
private
private
Opens a NATS client for the duration of the provided block.
Class Method Details
.abort_with(error) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Prints an error and exits with a non-zero status.
84 85 86 87 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 84 def abort_with(error) warn "error: #{error.message}" exit 1 end |
.add_common_options(parser, opts) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Adds all common options to an OptionParser.
136 137 138 139 140 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 136 def (parser, opts) (parser, opts) (parser, opts) (parser, opts) end |
.add_connection_options(parser, opts) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Adds NATS connection options to an OptionParser.
148 149 150 151 152 153 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 148 def (parser, opts) parser.on('-s', '--server URL', 'NATS server URL. Defaults to ENV[NATS_URL] or local NATS') do |url| opts[:server] = url end parser.on('--creds FILE', 'NATS user credentials file') { |file| opts[:creds] = file } end |
.add_filter_options(parser, opts) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Adds service filter options to an OptionParser.
161 162 163 164 165 166 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 161 def (parser, opts) parser.on('--name NAME', 'Only query services with this name') { |name| opts[:name] = name } parser.on('--id ID', 'Only query this service id. Requires --name') { |id| opts[:id] = id } parser.on('--prefix PREFIX', 'Service API prefix. Defaults to $SRV') { |prefix| opts[:prefix] = prefix } parser.on('-t', '--timeout SECONDS', Float, 'Reply collection idle timeout') { |timeout| opts[:timeout] = timeout } end |
.add_output_options(parser, opts) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Adds output and help options to an OptionParser.
174 175 176 177 178 179 180 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 174 def (parser, opts) parser.on('--json', 'Print JSON output') { opts[:json] = true } unless opts[:json].nil? parser.on('-h', '--help', 'Show this help') do puts parser exit end end |
.column_widths(headers, rows) ⇒ Array<Integer>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Computes table column widths.
200 201 202 203 204 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 200 def column_widths(headers, rows) headers.each_index.map do |index| ([headers[index]] + rows.map { |row| row[index].to_s }).map(&:length).max end end |
.default_options(json:) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Builds default CLI options.
121 122 123 124 125 126 127 128 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 121 def (json:) { server: ENV.fetch('NATS_URL', 'nats://127.0.0.1:4222'), timeout: DEFAULT_TIMEOUT, prefix: DEFAULT_PREFIX, json: json ? false : nil, } end |
.discovery_options(opts, client) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Builds discovery operation options from CLI options and a client.
106 107 108 109 110 111 112 113 114 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 106 def (opts, client) { client:, name: opts[:name], id: opts[:id], prefix: opts[:prefix], timeout: opts[:timeout], } end |
.operation_result!(result) ⇒ Trailblazer::Operation::Railway::Result
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a successful operation result or aborts the process.
94 95 96 97 98 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 94 def operation_result!(result) abort_with(result[:error]) unless result.success? result end |
.parse(argv, banner:, description:, json: false) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Parses common NATS discovery CLI options.
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 24 def parse(argv, banner:, description:, json: false) opts = (json:) OptionParser.new do |parser| parser. = parser.separator '' parser.separator description parser.separator '' (parser, opts) end.parse!(argv) validate!(opts) opts end |
.print_json(value) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Prints a value as pretty JSON.
61 62 63 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 61 def print_json(value) puts JSON.pretty_generate(value) end |
.print_table(headers, rows) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Prints rows as a simple aligned table.
71 72 73 74 75 76 77 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 71 def print_table(headers, rows) widths = column_widths(headers, rows) format = widths.map { |width| "%-#{width}s" }.join(' ') puts format % headers puts widths.map { |width| '-' * width }.join(' ') rows.each { |row| puts format % row } end |
.validate!(opts) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Validates parsed CLI options.
187 188 189 190 191 192 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 187 def validate!(opts) return unless opts[:id] && opts[:name].to_s.empty? warn 'error: --id requires --name' exit 1 end |
.with_client(opts) {|client| ... } ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Opens a NATS client for the duration of the provided block.
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 43 def with_client(opts) result = Operation::Connect.call( server: opts[:server], creds: opts[:creds], connect_options: { reconnect: false, connect_timeout: opts[:timeout] }, ) abort_with(result[:error]) unless result.success? yield result[:client] ensure result&.[](:client)&.close end |
Instance Method Details
#abort_with(error) ⇒ void (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Prints an error and exits with a non-zero status.
84 85 86 87 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 84 def abort_with(error) warn "error: #{error.message}" exit 1 end |
#add_common_options(parser, opts) ⇒ void (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Adds all common options to an OptionParser.
136 137 138 139 140 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 136 def (parser, opts) (parser, opts) (parser, opts) (parser, opts) end |
#add_connection_options(parser, opts) ⇒ void (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Adds NATS connection options to an OptionParser.
148 149 150 151 152 153 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 148 def (parser, opts) parser.on('-s', '--server URL', 'NATS server URL. Defaults to ENV[NATS_URL] or local NATS') do |url| opts[:server] = url end parser.on('--creds FILE', 'NATS user credentials file') { |file| opts[:creds] = file } end |
#add_filter_options(parser, opts) ⇒ void (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Adds service filter options to an OptionParser.
161 162 163 164 165 166 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 161 def (parser, opts) parser.on('--name NAME', 'Only query services with this name') { |name| opts[:name] = name } parser.on('--id ID', 'Only query this service id. Requires --name') { |id| opts[:id] = id } parser.on('--prefix PREFIX', 'Service API prefix. Defaults to $SRV') { |prefix| opts[:prefix] = prefix } parser.on('-t', '--timeout SECONDS', Float, 'Reply collection idle timeout') { |timeout| opts[:timeout] = timeout } end |
#add_output_options(parser, opts) ⇒ void (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Adds output and help options to an OptionParser.
174 175 176 177 178 179 180 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 174 def (parser, opts) parser.on('--json', 'Print JSON output') { opts[:json] = true } unless opts[:json].nil? parser.on('-h', '--help', 'Show this help') do puts parser exit end end |
#column_widths(headers, rows) ⇒ Array<Integer> (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Computes table column widths.
200 201 202 203 204 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 200 def column_widths(headers, rows) headers.each_index.map do |index| ([headers[index]] + rows.map { |row| row[index].to_s }).map(&:length).max end end |
#default_options(json:) ⇒ Hash (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Builds default CLI options.
121 122 123 124 125 126 127 128 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 121 def (json:) { server: ENV.fetch('NATS_URL', 'nats://127.0.0.1:4222'), timeout: DEFAULT_TIMEOUT, prefix: DEFAULT_PREFIX, json: json ? false : nil, } end |
#discovery_options(opts, client) ⇒ Hash (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Builds discovery operation options from CLI options and a client.
106 107 108 109 110 111 112 113 114 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 106 def (opts, client) { client:, name: opts[:name], id: opts[:id], prefix: opts[:prefix], timeout: opts[:timeout], } end |
#operation_result!(result) ⇒ Trailblazer::Operation::Railway::Result (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a successful operation result or aborts the process.
94 95 96 97 98 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 94 def operation_result!(result) abort_with(result[:error]) unless result.success? result end |
#parse(argv, banner:, description:, json: false) ⇒ Hash (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Parses common NATS discovery CLI options.
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 24 def parse(argv, banner:, description:, json: false) opts = (json:) OptionParser.new do |parser| parser. = parser.separator '' parser.separator description parser.separator '' (parser, opts) end.parse!(argv) validate!(opts) opts end |
#print_json(value) ⇒ void (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Prints a value as pretty JSON.
61 62 63 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 61 def print_json(value) puts JSON.pretty_generate(value) end |
#print_table(headers, rows) ⇒ void (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Prints rows as a simple aligned table.
71 72 73 74 75 76 77 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 71 def print_table(headers, rows) widths = column_widths(headers, rows) format = widths.map { |width| "%-#{width}s" }.join(' ') puts format % headers puts widths.map { |width| '-' * width }.join(' ') rows.each { |row| puts format % row } end |
#validate!(opts) ⇒ void (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Validates parsed CLI options.
187 188 189 190 191 192 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 187 def validate!(opts) return unless opts[:id] && opts[:name].to_s.empty? warn 'error: --id requires --name' exit 1 end |
#with_client(opts) {|client| ... } ⇒ Object (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Opens a NATS client for the duration of the provided block.
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/leopard/nats_service_discovery/cli.rb', line 43 def with_client(opts) result = Operation::Connect.call( server: opts[:server], creds: opts[:creds], connect_options: { reconnect: false, connect_timeout: opts[:timeout] }, ) abort_with(result[:error]) unless result.success? yield result[:client] ensure result&.[](:client)&.close end |