Module: HammerCLI

Defined in:
lib/hammer_cli/i18n.rb,
lib/hammer_cli/main.rb,
lib/hammer_cli/shell.rb,
lib/hammer_cli/utils.rb,
lib/hammer_cli/logger.rb,
lib/hammer_cli/context.rb,
lib/hammer_cli/modules.rb,
lib/hammer_cli/version.rb,
lib/hammer_cli/abstract.rb,
lib/hammer_cli/defaults.rb,
lib/hammer_cli/messages.rb,
lib/hammer_cli/settings.rb,
lib/hammer_cli/completer.rb,
lib/hammer_cli/full_help.rb,
lib/hammer_cli/verbosity.rb,
lib/hammer_cli/connection.rb,
lib/hammer_cli/csv_parser.rb,
lib/hammer_cli/exceptions.rb,
lib/hammer_cli/exit_codes.rb,
lib/hammer_cli/ssloptions.rb,
lib/hammer_cli/subcommand.rb,
lib/hammer_cli/task_helper.rb,
lib/hammer_cli/help/builder.rb,
lib/hammer_cli/logger_watch.rb,
lib/hammer_cli/output/utils.rb,
lib/hammer_cli/i18n/find_task.rb,
lib/hammer_cli/option_builder.rb,
lib/hammer_cli/bash/completion.rb,
lib/hammer_cli/ca_cert_fetcher.rb,
lib/hammer_cli/ca_cert_manager.rb,
lib/hammer_cli/help/definition.rb,
lib/hammer_cli/options/matcher.rb,
lib/hammer_cli/testing/messages.rb,
lib/hammer_cli/defaults_commands.rb,
lib/hammer_cli/exception_handler.rb,
lib/hammer_cli/help/text_builder.rb,
lib/hammer_cli/command_extensions.rb,
lib/hammer_cli/options/predefined.rb,
lib/hammer_cli/options/normalizers.rb,
lib/hammer_cli/help/definition/list.rb,
lib/hammer_cli/help/definition/note.rb,
lib/hammer_cli/help/definition/text.rb,
lib/hammer_cli/options/sources/base.rb,
lib/hammer_cli/testing/data_helpers.rb,
lib/hammer_cli/bash/prebuild_command.rb,
lib/hammer_cli/options/option_family.rb,
lib/hammer_cli/options/processor_list.rb,
lib/hammer_cli/options/validators/dsl.rb,
lib/hammer_cli/help/definition/section.rb,
lib/hammer_cli/options/validators/base.rb,
lib/hammer_cli/output/generators/table.rb,
lib/hammer_cli/testing/output_matchers.rb,
lib/hammer_cli/options/option_collector.rb,
lib/hammer_cli/options/option_processor.rb,
lib/hammer_cli/options/option_definition.rb,
lib/hammer_cli/testing/command_assertions.rb,
lib/hammer_cli/options/sources/command_line.rb,
lib/hammer_cli/help/definition/abstract_item.rb,
lib/hammer_cli/options/sources/saved_defaults.rb,
lib/hammer_cli/options/validators/dsl_block_validator.rb

Defined Under Namespace

Modules: Apipie, Bash, Help, I18n, Logger, Messages, Options, Output, Subcommand, TaskHelper, Testing Classes: AbstractCommand, AbstractConnector, AbstractOptionBuilder, BaseDefaultsProvider, CACertFetcher, CACertManager, CSVParser, CertDownloader, CommandConflict, CommandExtensions, Completer, CompleterLine, CompleterWord, Connection, Defaults, DefaultsCommand, ExceptionHandler, FullHelpCommand, MainCommand, ModuleDisabledButRequired, ModuleLoadingError, Modules, NilValue, NoCACertificate, OperationNotSupportedError, OptionBuilderContainer, SSLOptions, Settings, ShellCommand, ShellHistory, ShellMainCommand

Constant Summary collapse

DEFAULT_FILE =
"#{Dir.home}/.hammer/defaults.yml"
V_QUIET =
0
V_UNIX =
1
V_VERBOSE =
2
EX_OK =

taken from sysexits.h

0
EX_USAGE =

successful termination

64
EX_DATAERR =

command line usage error

65
EX_NOINPUT =

data format error

66
EX_NOUSER =

cannot open input

67
EX_NOHOST =

addressee unknown

68
EX_UNAVAILABLE =

host name unknown

69
EX_SOFTWARE =

service unavailable

70
EX_OSERR =

internal software error

71
EX_OSFILE =

system error (e.g., can’t fork)

72
EX_CANTCREAT =

critical OS file missing

73
EX_IOERR =

can’t create (user) output file

74
EX_TEMPFAIL =

input/output error

75
EX_PROTOCOL =

temp failure; user is invited to retry

76
EX_NOPERM =

remote error in protocol

77
EX_CONFIG =

permission denied

78
EX_NOT_FOUND =

non POSIX codes

128
EX_UNAUTHORIZED =

resource was not found

129
EX_RETRY =

authorization failed

666

Class Method Summary collapse

Class Method Details

._option_accessor_name(name) ⇒ Object



15
16
17
# File 'lib/hammer_cli/options/option_definition.rb', line 15

def self._option_accessor_name(name)
  "option_#{name.to_s}".gsub('-', '_')
end

.capitalizationObject



76
77
78
79
80
81
82
83
# File 'lib/hammer_cli/utils.rb', line 76

def self.capitalization
  supported = %w[downcase capitalize upcase]
  capitalization = HammerCLI::Settings.get(:ui, :capitalization).to_s
  return nil if capitalization.empty?
  return capitalization if supported.include?(capitalization)
  warn _("Cannot use such capitalization. Try one of %s.") % supported.join(', ')
  nil
end

.clear_cacheObject



57
58
59
60
61
# File 'lib/hammer_cli/utils.rb', line 57

def self.clear_cache
  if (completion_file = HammerCLI::Settings.get(:completion_cache_file))
    FileUtils.rm_f(File.expand_path(completion_file))
  end
end

.constant_path(name) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/hammer_cli/utils.rb', line 68

def self.constant_path(name)
  path = name.to_s.split('::').inject([Object]) do |mod, class_name|
    mod << mod[-1].const_get(class_name)
  end
  path.shift
  path
end

.contextObject



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/hammer_cli/context.rb', line 4

def self.context
  @context ||= {
    :defaults => HammerCLI.defaults,
    :is_tty? => HammerCLI.tty?,
    :api_connection => HammerCLI::Connection.new(Logging.logger['Connection']),
    :no_headers => HammerCLI::Settings.get(:ui, :no_headers),
    :use_defaults => HammerCLI::Settings.get(:use_defaults),
    :capitalization => HammerCLI.capitalization,
    :verbosity => (HammerCLI::Settings.get(:verbosity) || HammerCLI::V_VERBOSE).to_i
  }
end

.defaultsObject



101
102
103
# File 'lib/hammer_cli/defaults.rb', line 101

def self.defaults
  @defaults ||= Defaults.new(HammerCLI::Settings.get(:defaults))
end

.expand_invocation_path(path) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/hammer_cli/utils.rb', line 115

def self.expand_invocation_path(path)
  bits = path.split(' ')
  parent_command = HammerCLI::MainCommand
  new_path = (bits[1..-1] || []).each_with_object([]) do |bit, names|
    subcommand = parent_command.find_subcommand(bit)
    next if subcommand.nil?

    names << if subcommand.names.size > 1
               "<#{subcommand.names.join('|')}>"
             else
               subcommand.names.first
             end
    parent_command = subcommand.subcommand_class
  end
  new_path.unshift(bits.first).join(' ')
end

.insert_relative(array, mode, idx, *new_items) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/hammer_cli/utils.rb', line 100

def self.insert_relative(array, mode, idx, *new_items)
  case mode
  when :prepend
    idx = 0
  when :append
    idx = -1
  when :after
    idx += 1
  when :replace
    array.delete_at(idx)
  end

  array.insert(idx, *new_items)
end

.interactive?Boolean

Returns:

  • (Boolean)


63
64
65
66
# File 'lib/hammer_cli/utils.rb', line 63

def self.interactive?
  return HammerCLI::Settings.get(:_params, :interactive) unless HammerCLI::Settings.get(:_params, :interactive).nil?
  HammerCLI::Settings.get(:ui, :interactive) != false
end

.interactive_outputObject



85
86
87
# File 'lib/hammer_cli/utils.rb', line 85

def self.interactive_output
  @interactive_output ||= HighLine.new($stdin, IO.new(IO.sysopen('/dev/tty', 'w'), 'w'))
end

.open_in_editor(content, content_type: '', tempdir: '/tmp', suffix: '.tmp') ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/hammer_cli/utils.rb', line 89

def self.open_in_editor(content, content_type: '', tempdir: '/tmp', suffix: '.tmp')
  result = content
  Tempfile.open([content_type, suffix], tempdir) do |f|
    f.write(content)
    f.rewind
    system("#{ENV['EDITOR'] || 'vi'} #{f.path}")
    result = f.read
  end
  result
end

.option_accessor_name(*name) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/hammer_cli/options/option_definition.rb', line 7

def self.option_accessor_name(*name)
  if name.length > 1
    name.map { |n| _option_accessor_name(n) }
  else
    _option_accessor_name(name.first)
  end
end

.tty?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/hammer_cli/utils.rb', line 53

def self.tty?
  STDOUT.tty?
end

.versionObject



2
3
4
# File 'lib/hammer_cli/version.rb', line 2

def self.version
  @version ||= Gem::Version.new "3.13.0"
end