Class: Bashly::Script::Command
- Includes:
- CommandScopes, Completions
- Defined in:
- lib/bashly/script/command.rb
Constant Summary
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#action_name ⇒ Object
Returns the name to be used as an action.
-
#aliases ⇒ Object
Returns all the possible aliases for this command.
-
#args ⇒ Object
Returns an array of Arguments.
-
#caption_string ⇒ Object
Returns a string suitable to be a headline.
- #catch_all ⇒ Object
-
#commands ⇒ Object
Returns an array of the Commands.
-
#environment_variables ⇒ Object
Returns an array of EnvironmentVariables.
-
#filename ⇒ Object
Returns the bash filename that is expected to hold the user code for this command.
-
#flags ⇒ Object
Returns an array of Flags.
-
#full_name ⇒ Object
Returns the name of the command, including its parent name (in case this is a subcommand).
-
#function_name ⇒ Object
Returns a unique name, suitable to be used in a bash function.
-
#load_user_file(file, placeholder: true) ⇒ Object
Reads a file from the userspace (Settings.source_dir) and returns its contents.
-
#parents ⇒ Object
Returns an array of all parents.
-
#root_command? ⇒ Boolean
Returns trus if this is the root command (no parents).
-
#short_flag_exist?(flag) ⇒ Boolean
Returns true if one of the flags matches the provided short code.
-
#usage_string ⇒ Object
Returns a constructed string suitable for Usage pattern.
-
#user_lib ⇒ Object
Returns an array of files to include as is inside the script This is meant to provide the user with the ability to add custom functions.
Methods included from CommandScopes
#command_names, #deep_commands, #default_args, #default_command, #default_environment_variables, #default_flags, #required_args, #required_environment_variables, #required_flags, #whitelisted_args, #whitelisted_flags
Methods included from Completions
#completion_data, #completion_function, #completion_script
Methods inherited from Base
#help, #initialize, #method_missing, #optional, #respond_to_missing?, #summary
Methods included from Renderable
Constructor Details
This class inherits a constructor from Bashly::Script::Base
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Bashly::Script::Base
Instance Method Details
#action_name ⇒ Object
Returns the name to be used as an action.
-
If it is the root command, the action is “root”
-
Else, it is all the parents, except the first one (root) joined by space. For example, for a command like “docker container run” the action name is “container run”.
12 13 14 |
# File 'lib/bashly/script/command.rb', line 12 def action_name parents.any? ? (parents[1..-1] + [name]).join(' ') : "root" end |
#aliases ⇒ Object
Returns all the possible aliases for this command
17 18 19 |
# File 'lib/bashly/script/command.rb', line 17 def aliases short ? [name, short] : [name] end |
#args ⇒ Object
Returns an array of Arguments
22 23 24 25 26 27 |
# File 'lib/bashly/script/command.rb', line 22 def args return [] unless ["args"] ["args"].map do || Argument.new end end |
#caption_string ⇒ Object
Returns a string suitable to be a headline
30 31 32 |
# File 'lib/bashly/script/command.rb', line 30 def caption_string help ? "#{full_name} - #{summary}" : full_name end |
#catch_all ⇒ Object
34 35 36 |
# File 'lib/bashly/script/command.rb', line 34 def catch_all @catch_all ||= CatchAll.from_config ['catch_all'] end |
#commands ⇒ Object
Returns an array of the Commands
39 40 41 42 43 44 45 |
# File 'lib/bashly/script/command.rb', line 39 def commands return [] unless ["commands"] ["commands"].map do || ['parents'] = parents + [name] Command.new end end |
#environment_variables ⇒ Object
Returns an array of EnvironmentVariables
48 49 50 51 52 53 |
# File 'lib/bashly/script/command.rb', line 48 def environment_variables return [] unless ["environment_variables"] ["environment_variables"].map do || EnvironmentVariable.new end end |
#filename ⇒ Object
Returns the bash filename that is expected to hold the user code for this command
57 58 59 |
# File 'lib/bashly/script/command.rb', line 57 def filename ["filename"] || "#{action_name.to_underscore}_command.sh" end |
#flags ⇒ Object
Returns an array of Flags
62 63 64 65 66 67 |
# File 'lib/bashly/script/command.rb', line 62 def flags return [] unless ["flags"] ["flags"].map do || Flag.new end end |
#full_name ⇒ Object
Returns the name of the command, including its parent name (in case this is a subcommand)
76 77 78 |
# File 'lib/bashly/script/command.rb', line 76 def full_name parents.any? ? (parents + [name]).join(' ') : name end |
#function_name ⇒ Object
Returns a unique name, suitable to be used in a bash function
70 71 72 |
# File 'lib/bashly/script/command.rb', line 70 def function_name full_name.to_underscore end |
#load_user_file(file, placeholder: true) ⇒ Object
Reads a file from the userspace (Settings.source_dir) and returns its contents. If the file is not found, returns a string with a hint.
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/bashly/script/command.rb', line 83 def load_user_file(file, placeholder: true) path = "#{Settings.source_dir}/#{file}" default_content = placeholder ? "echo \"error: cannot load file\"" : '' content = if File.exist? path File.read(path).remove_front_matter else default_content end "# :#{path}\n#{content}" end |
#parents ⇒ Object
Returns an array of all parents. For example, the command “docker container run” will have [docker, container] as its parents
98 99 100 |
# File 'lib/bashly/script/command.rb', line 98 def parents ['parents'] || [] end |
#root_command? ⇒ Boolean
Returns trus if this is the root command (no parents)
103 104 105 |
# File 'lib/bashly/script/command.rb', line 103 def root_command? parents.empty? end |
#short_flag_exist?(flag) ⇒ Boolean
Returns true if one of the flags matches the provided short code
108 109 110 |
# File 'lib/bashly/script/command.rb', line 108 def short_flag_exist?(flag) flags.select { |f| f.short == flag }.any? end |
#usage_string ⇒ Object
Returns a constructed string suitable for Usage pattern
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/bashly/script/command.rb', line 113 def usage_string result = [full_name] result << "[command]" if commands.any? args.each do |arg| result << arg.usage_string end result << "[options]" unless flags.empty? result << catch_all.usage_string if catch_all.enabled? result.join " " end |
#user_lib ⇒ Object
Returns an array of files to include as is inside the script This is meant to provide the user with the ability to add custom functions
127 128 129 |
# File 'lib/bashly/script/command.rb', line 127 def user_lib @user_lib ||= Dir["#{Settings.full_lib_dir}/**/*.sh"].sort end |