Class: Brut::CLI::Commands::BaseCommand
- Inherits:
-
Object
- Object
- Brut::CLI::Commands::BaseCommand
- Defined in:
- lib/brut/cli/commands/base_command.rb
Direct Known Subclasses
Apps::BuildAssets, Apps::BuildAssets::Css, Apps::BuildAssets::Images, Apps::BuildAssets::Js, Apps::DB, Apps::DB::Create, Apps::DB::Drop, Apps::DB::Migrate, Apps::DB::NewMigration, Apps::DB::Seed, Apps::DB::Status, Apps::Deploy, Apps::Deploy::Build, Apps::Deploy::Build::Docker, Apps::Deploy::Check, Apps::Deploy::Check::Git, Apps::Deploy::Heroku, Apps::New::App, Apps::New::App::Segment, Apps::Scaffold, Apps::Scaffold::Action, Apps::Scaffold::Component, Apps::Scaffold::CustomElementTest, Apps::Scaffold::DbModel, Apps::Scaffold::E2ETest, Apps::Scaffold::Page, Apps::Scaffold::Test, Apps::Test, Apps::Test::Audit, Apps::Test::Js, Apps::Test::Run, CompoundCommand, Help, OutputError, RaiseError
Instance Attribute Summary collapse
-
#parent_command ⇒ Object
Returns the value of attribute parent_command.
Instance Method Summary collapse
-
#accepts ⇒ Array<Array>|Array<Class>
Specify type conversions for options.
-
#args_description ⇒ String
Description of the arguments this command accepts.
-
#argv ⇒ Array<String>
Access the command line arguments leftover after all options have been parsed, when
.executewas called. -
#bootstrap? ⇒ true|false
True if the command requires Brut to fully bootstrap and start itself up.
-
#commands ⇒ Array<Brut::CLI::Commands::BaseCommand>
Returns a list of commands that represent the subcommands available to this command.
-
#default_command ⇒ Brut::CLI::Commands::BaseCommand|nil
Command to run if none provided on the command line.
-
#default_command_class ⇒ Object
-
#default_rack_env ⇒ String|nil
The default
RACK_ENVto use for this command. -
#description ⇒ String
Description of this command for use in help output.
-
#env ⇒ Object
Convienience methods to defer to
Brut::CLI::Commands::ExecutionContext#env. -
#env_vars ⇒ Array<Array<String>>
Array of arrays documenting which environment variables affect this command's behavior.
-
#execute(execution_context) ⇒ Object
Execute the command in the given context.
-
#name ⇒ String
The name of the command that the developer should use on the command line.
-
#options ⇒ Object
Convienience methods to defer to
Brut::CLI::Commands::ExecutionContext#options. -
#opts ⇒ Array<Array<Object>>
Used to specify the command line options for the command.
-
#puts(*args) ⇒ Object
Convienience methods to defer to
Brut::CLI::Commands::ExecutionContext#stdout'sputs. -
#run ⇒ Integer|Object|StandardError
Runs whatever logic this command exists to execute.
-
#stderr ⇒ Object
Convienience methods to defer to
Brut::CLI::Commands::ExecutionContext#stderr. -
#stdin ⇒ Object
Convienience methods to defer to
Brut::CLI::Commands::ExecutionContext#stdin. -
#stdout ⇒ Object
Convienience methods to defer to
Brut::CLI::Commands::ExecutionContext#stdout. -
#system!(*args, &block) ⇒ Object
Convienience methods to defer to
Brut::CLI::Commands::ExecutionContext'sBrut::CLI::Executor#system!.
Instance Attribute Details
#parent_command ⇒ Object
Returns the value of attribute parent_command.
4 5 6 |
# File 'lib/brut/cli/commands/base_command.rb', line 4 def parent_command @parent_command end |
Instance Method Details
#accepts ⇒ Array<Array>|Array<Class>
Specify type conversions for options. This is a loose wrapper around OptionParser#accept with a convienience feature
to allow simpler conversions without a proc.
86 |
# File 'lib/brut/cli/commands/base_command.rb', line 86 def accepts = [] |
#args_description ⇒ String
Returns description of the arguments this command accepts. Used for documentation only.
44 |
# File 'lib/brut/cli/commands/base_command.rb', line 44 def args_description = nil |
#argv ⇒ Array<String>
Access the command line arguments leftover after all options have been parsed, when .execute was called
119 |
# File 'lib/brut/cli/commands/base_command.rb', line 119 def argv = self.execution_context.argv |
#bootstrap? ⇒ true|false
True if the command requires Brut to fully bootstrap and start itself up. Bootstrapping isn't running a web server but it will
do everything else, including connecting too all databases. Your command should return true for this if it needs to access a database
or make API calls outside Brut::CLI. If this returns false, Brut's configuration options will still be available.
By default, this returns the value for bootstrap? of the default_command. If there is no default_command, this returns false.
30 |
# File 'lib/brut/cli/commands/base_command.rb', line 30 def bootstrap? = default_command&.bootstrap? || false |
#commands ⇒ Array<Brut::CLI::Commands::BaseCommand>
Returns a list of commands that represent the subcommands available to this command. By default, this will return all commands that are inner classes of this command.
99 100 101 102 103 104 105 |
# File 'lib/brut/cli/commands/base_command.rb', line 99 def commands self.class.constants.map { |name| self.class.const_get(name) }.select { |constant| constant.kind_of?(Class) && constant.ancestors.include?(Brut::CLI::Commands::BaseCommand) }.map(&:new) end |
#default_command ⇒ Brut::CLI::Commands::BaseCommand|nil
Command to run if none provided on the command line.
91 |
# File 'lib/brut/cli/commands/base_command.rb', line 91 def default_command = self.commands.detect { it.class == default_command_class } |
#default_command_class ⇒ Object
93 |
# File 'lib/brut/cli/commands/base_command.rb', line 93 def default_command_class = nil |
#default_rack_env ⇒ String|nil
The default RACK_ENV to use for this command. This value is used when no RACK_ENV is present in the UNIX environment
and when --env has not been used on the command line. Do note that setting this in an app or parent command does
not translate to the subcommands.
38 |
# File 'lib/brut/cli/commands/base_command.rb', line 38 def default_rack_env = default_command&.default_rack_env |
#description ⇒ String
Returns description of this command for use in help output.
41 |
# File 'lib/brut/cli/commands/base_command.rb', line 41 def description = "" |
#env ⇒ Object
Convienience methods to defer to Brut::CLI::Commands::ExecutionContext#env. You should use this over ENV.
148 |
# File 'lib/brut/cli/commands/base_command.rb', line 148 def env = self.execution_context.env |
#env_vars ⇒ Array<Array<String>>
Returns Array of arrays documenting which environment variables affect this command's behavior. The array should have two elements: the env var name as a string, and a string documenting its purpose. This is used for documentation only.
49 |
# File 'lib/brut/cli/commands/base_command.rb', line 49 def env_vars = [] |
#execute(execution_context) ⇒ Object
Execute the command in the given context. This is the method to call to execute a command programmatically, however you
should not override this method. Instead, override .run to provide your command's logic.
to standard streams, the parsed options, and unparsed arguments.
17 18 19 20 |
# File 'lib/brut/cli/commands/base_command.rb', line 17 def execute(execution_context) @execution_context = execution_context self.run end |
#name ⇒ String
The name of the command that the developer should use on the command line. By default, this is the underscorized version of the class' simple name.
10 |
# File 'lib/brut/cli/commands/base_command.rb', line 10 def name = RichString.new(self.class.name.split(/::/).last).underscorized |
#options ⇒ Object
Convienience methods to defer to Brut::CLI::Commands::ExecutionContext#options.
145 146 147 |
# File 'lib/brut/cli/commands/base_command.rb', line 145 def = self.execution_context. # Convienience methods to defer to `Brut::CLI::Commands::ExecutionContext#env`. You should use this over `ENV`. # @!visibility public |
#opts ⇒ Array<Array<Object>>
Used to specify the command line options for the command.
55 |
# File 'lib/brut/cli/commands/base_command.rb', line 55 def opts = [] |
#puts(*args) ⇒ Object
Convienience methods to defer to Brut::CLI::Commands::ExecutionContext#stdout's puts.
129 130 131 |
# File 'lib/brut/cli/commands/base_command.rb', line 129 def puts(*args) self.execution_context.stdout.puts(*args) end |
#run ⇒ Integer|Object|StandardError
Runs whatever logic this command exists to execute. This is the method you must implement, however #execute is the public
API and what you should call if you want to programmatically execute a command.
The default implementation will generate an error, which is suitable for an app or namespace command that require a subcommand.
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/brut/cli/commands/base_command.rb', line 164 def run if argv[0] stderr.puts "No such command '#{argv[0]}'" return 1 end stderr.puts "Command is required" 1 end |
#stderr ⇒ Object
Convienience methods to defer to Brut::CLI::Commands::ExecutionContext#stderr. You should use this over STDERR or $stderr.
135 136 137 |
# File 'lib/brut/cli/commands/base_command.rb', line 135 def stderr = self.execution_context.stderr # Convienience methods to defer to `Brut::CLI::Commands::ExecutionContext#stdin`. You should use this over `STDIN` of `$stdin`. # @!visibility public |
#stdin ⇒ Object
Convienience methods to defer to Brut::CLI::Commands::ExecutionContext#stdin. You should use this over STDIN of $stdin.
138 139 140 141 |
# File 'lib/brut/cli/commands/base_command.rb', line 138 def stdin = self.execution_context.stdin # Convienience methods to defer to `Brut::CLI::Commands::ExecutionContext#stdout`. You should use this over `STDOUT` of `$stdout`. Note # that if you just want to output a string, use `puts`. # @!visibility public |
#stdout ⇒ Object
Convienience methods to defer to Brut::CLI::Commands::ExecutionContext#stdout. You should use this over STDOUT of $stdout. Note
that if you just want to output a string, use puts.
142 143 144 |
# File 'lib/brut/cli/commands/base_command.rb', line 142 def stdout = self.execution_context.stdout # Convienience methods to defer to `Brut::CLI::Commands::ExecutionContext#options`. # @!visibility public |
#system!(*args, &block) ⇒ Object
Convienience methods to defer to Brut::CLI::Commands::ExecutionContext's Brut::CLI::Executor#system!.
123 124 125 |
# File 'lib/brut/cli/commands/base_command.rb', line 123 def system!(*args,&block) self.execution_context.executor.system!(*args,&block) end |