Class: Jets::Command::Base

Inherits:
Thor
  • Object
show all
Extended by:
Memoist
Includes:
Actions, ApiHelpers, AwsHelpers
Defined in:
lib/jets/command/base.rb

Defined Under Namespace

Classes: CorrectableError, Error

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApiHelpers

#check_for_error_message!, #no_token_exit!, #paging_params

Methods included from AwsHelpers

#find_stack, #first_run?

Methods included from AwsServices

#apigateway, #aws_lambda, #aws_options, #cfn, #dynamodb, #logs, #s3, #s3_resource, #sns, #sqs, #sts

Methods included from AwsServices::StackStatus

#lookup, #stack_exists?, #stack_in_progress?

Methods included from AwsServices::GlobalMemoist

included

Methods included from Actions

#load_generators, #load_tasks, #require_application!, #require_application_and_environment!, #set_application_directory!

Class Method Details

Use Jets’ default banner.



118
119
120
121
122
123
124
# File 'lib/jets/command/base.rb', line 118

def banner(*)
  command_name = full_namespace ? full_namespace.split(':').last : command_name
  command = commands[command_name]
  options_arg = '[options]' unless command && command.options.empty?
  output = "#{executable} #{arguments.map(&:usage).join(' ')} #{options_arg}".squish
  "  #{output}" # add 2 more spaces in front
end

.base_nameObject

Sets the base_name taking into account the current class namespace.

Jets::Command::TestCommand.base_name # => 'jets'


129
130
131
132
133
# File 'lib/jets/command/base.rb', line 129

def base_name
  @base_name ||= if base = name.to_s.split("::").first
    base.underscore
  end
end

.command_nameObject

Return command name without namespaces.

Jets::Command::TestCommand.command_name # => 'test'


138
139
140
141
142
143
# File 'lib/jets/command/base.rb', line 138

def command_name
  @command_name ||= if command = name.to_s.split("::").last
    command.chomp!("Command")
    command.underscore
  end
end

.default_command_rootObject

Default file root to place extra files a command might need, placed one folder above the command file.

For a Jets::Command::TestCommand placed in jets/command/test_command.rb would return jets/test.



158
159
160
161
# File 'lib/jets/command/base.rb', line 158

def default_command_root
  path = File.expand_path(relative_command_path, __dir__)
  path if File.exist?(path)
end

.desc(usage = nil, description = nil, options = {}) ⇒ Object

Tries to get the description from a USAGE file one folder above the command root.



62
63
64
65
66
67
68
# File 'lib/jets/command/base.rb', line 62

def desc(usage = nil, description = nil, options = {})
  if usage
    super
  else
    @desc ||= ERB.new(File.read(usage_path), trim_mode: "-").result(binding) if usage_path
  end
end

.engine?Boolean

Returns true when the app is a Jets engine.

Returns:

  • (Boolean)


56
57
58
# File 'lib/jets/command/base.rb', line 56

def engine?
  defined?(ENGINE_ROOT)
end

.executableObject



113
114
115
# File 'lib/jets/command/base.rb', line 113

def executable
  "jets #{full_namespace || command_name}"
end

.exit_on_failure?Boolean

:nodoc:

Returns:

  • (Boolean)


51
52
53
# File 'lib/jets/command/base.rb', line 51

def exit_on_failure? # :nodoc:
  false
end

.hide_command!Object

Convenience method to hide this command from the available ones when running jets command.



83
84
85
# File 'lib/jets/command/base.rb', line 83

def hide_command!
  Jets::Command.hidden_commands << self
end

.inherited(base) ⇒ Object

:nodoc:



87
88
89
90
91
92
93
# File 'lib/jets/command/base.rb', line 87

def inherited(base) # :nodoc:
  super

  if base.name && !base.name.end_with?("Base")
    Jets::Command.subclasses << base
  end
end

.long_desc(long_description, options = {}) ⇒ Object



46
47
48
49
# File 'lib/jets/command/base.rb', line 46

def long_desc(long_description, options = {})
  options[:wrap] = false
  super
end

.namespace(name = nil) ⇒ Object

Convenience method to get the namespace from the class name. It’s the same as Thor default except that the Command at the end of the class is removed.



73
74
75
76
77
78
79
# File 'lib/jets/command/base.rb', line 73

def namespace(name = nil)
  if name
    super
  else
    @namespace ||= super.chomp("_command").sub(/:command:/, ":")
  end
end

.perform(full_namespace, command, args, config) ⇒ Object

:nodoc:



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/jets/command/base.rb', line 95

def perform(full_namespace, command, args, config) # :nodoc:
  if Jets::Command::HELP_MAPPINGS.include?(args.first)
    command, args = "help", []
    self.full_namespace = full_namespace # store for help. clean:log => log
  end

  dispatch(command, args.dup, nil, config)
rescue Thor::InvocationError => e
  puts e.message.color(:red) # message already has ERROR prefix
  self.full_namespace = full_namespace # store for help. clean:log => log
  dispatch("help", [], nil, config)
  exit 1
end

.printing_commandsObject



109
110
111
# File 'lib/jets/command/base.rb', line 109

def printing_commands
  namespaced_commands
end

.usage_pathObject

Path to lookup a USAGE description in a file.



146
147
148
149
150
151
# File 'lib/jets/command/base.rb', line 146

def usage_path
  if default_command_root
    path = File.join(default_command_root, "USAGE")
    path if File.exist?(path)
  end
end

Instance Method Details

#helpObject



197
198
199
200
201
202
203
204
205
206
# File 'lib/jets/command/base.rb', line 197

def help
  if full_namespace = self.class.full_namespace
    command_name = full_namespace.split(':').last # clean:log => log
    self.class.command_help(shell, command_name)
  elsif command_name = self.class.command_name
    self.class.command_help(shell, command_name)
  else
    super
  end
end