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.

API:

  • private

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • Error to report.

API:

  • private



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.

Parameters:

  • Parser to configure.

  • Mutable parsed option accumulator.

API:

  • private



136
137
138
139
140
# File 'lib/leopard/nats_service_discovery/cli.rb', line 136

def add_common_options(parser, opts)
  add_connection_options(parser, opts)
  add_filter_options(parser, opts)
  add_output_options(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.

Parameters:

  • Parser to configure.

  • Mutable parsed option accumulator.

API:

  • private



148
149
150
151
152
153
# File 'lib/leopard/nats_service_discovery/cli.rb', line 148

def add_connection_options(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.

Parameters:

  • Parser to configure.

  • Mutable parsed option accumulator.

API:

  • private



161
162
163
164
165
166
# File 'lib/leopard/nats_service_discovery/cli.rb', line 161

def add_filter_options(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.

Parameters:

  • Parser to configure.

  • Mutable parsed option accumulator.

API:

  • private



174
175
176
177
178
179
180
# File 'lib/leopard/nats_service_discovery/cli.rb', line 174

def add_output_options(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.

Parameters:

  • Column headers.

  • Table rows.

Returns:

  • Width for each column.

API:

  • private



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.

Parameters:

  • Whether the command supports --json.

Returns:

  • Default options.

API:

  • private



121
122
123
124
125
126
127
128
# File 'lib/leopard/nats_service_discovery/cli.rb', line 121

def default_options(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.

Parameters:

  • Parsed CLI options.

  • Connected NATS client.

Returns:

  • Arguments for discovery operations.

API:

  • private



106
107
108
109
110
111
112
113
114
# File 'lib/leopard/nats_service_discovery/cli.rb', line 106

def discovery_options(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.

Parameters:

  • Operation result.

Returns:

API:

  • private



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.

Parameters:

  • Command-line arguments.

  • OptionParser banner.

  • Command description.

  • (defaults to: false)

    Whether the command supports --json.

Returns:

  • Parsed options.

API:

  • private



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 = default_options(json:)
  OptionParser.new do |parser|
    parser.banner = banner
    parser.separator ''
    parser.separator description
    parser.separator ''
    add_common_options(parser, opts)
  end.parse!(argv)
  validate!(opts)
  opts
end

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.

Parameters:

  • JSON-serializable value.

API:

  • private



61
62
63
# File 'lib/leopard/nats_service_discovery/cli.rb', line 61

def print_json(value)
  puts JSON.pretty_generate(value)
end

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.

Parameters:

  • Column headers.

  • Table rows.

API:

  • private



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.

Parameters:

  • Parsed CLI options.

API:

  • private



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.

Parameters:

  • Parsed CLI options.

Yield Parameters:

  • client (NATS::Client)

    Connected NATS client.

Returns:

  • The block return value.

API:

  • private



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.

Parameters:

  • Error to report.

API:

  • private



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.

Parameters:

  • Parser to configure.

  • Mutable parsed option accumulator.

API:

  • private



136
137
138
139
140
# File 'lib/leopard/nats_service_discovery/cli.rb', line 136

def add_common_options(parser, opts)
  add_connection_options(parser, opts)
  add_filter_options(parser, opts)
  add_output_options(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.

Parameters:

  • Parser to configure.

  • Mutable parsed option accumulator.

API:

  • private



148
149
150
151
152
153
# File 'lib/leopard/nats_service_discovery/cli.rb', line 148

def add_connection_options(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.

Parameters:

  • Parser to configure.

  • Mutable parsed option accumulator.

API:

  • private



161
162
163
164
165
166
# File 'lib/leopard/nats_service_discovery/cli.rb', line 161

def add_filter_options(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.

Parameters:

  • Parser to configure.

  • Mutable parsed option accumulator.

API:

  • private



174
175
176
177
178
179
180
# File 'lib/leopard/nats_service_discovery/cli.rb', line 174

def add_output_options(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.

Parameters:

  • Column headers.

  • Table rows.

Returns:

  • Width for each column.

API:

  • private



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.

Parameters:

  • Whether the command supports --json.

Returns:

  • Default options.

API:

  • private



121
122
123
124
125
126
127
128
# File 'lib/leopard/nats_service_discovery/cli.rb', line 121

def default_options(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.

Parameters:

  • Parsed CLI options.

  • Connected NATS client.

Returns:

  • Arguments for discovery operations.

API:

  • private



106
107
108
109
110
111
112
113
114
# File 'lib/leopard/nats_service_discovery/cli.rb', line 106

def discovery_options(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.

Parameters:

  • Operation result.

Returns:

API:

  • private



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.

Parameters:

  • Command-line arguments.

  • OptionParser banner.

  • Command description.

  • (defaults to: false)

    Whether the command supports --json.

Returns:

  • Parsed options.

API:

  • private



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 = default_options(json:)
  OptionParser.new do |parser|
    parser.banner = banner
    parser.separator ''
    parser.separator description
    parser.separator ''
    add_common_options(parser, opts)
  end.parse!(argv)
  validate!(opts)
  opts
end

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.

Parameters:

  • JSON-serializable value.

API:

  • private



61
62
63
# File 'lib/leopard/nats_service_discovery/cli.rb', line 61

def print_json(value)
  puts JSON.pretty_generate(value)
end

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.

Parameters:

  • Column headers.

  • Table rows.

API:

  • private



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.

Parameters:

  • Parsed CLI options.

API:

  • private



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.

Parameters:

  • Parsed CLI options.

Yield Parameters:

  • client (NATS::Client)

    Connected NATS client.

Returns:

  • The block return value.

API:

  • private



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