Class: Bashly::Models::Command
- Includes:
- Completions
- Defined in:
- lib/bashly/models/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_help ⇒ Object
Returns a used defined help string for the catch_all directive.
-
#catch_all_label ⇒ Object
Returns a label for the catch_all directive.
-
#command_names ⇒ Object
Returns only the names of the Commands.
-
#commands ⇒ Object
Returns an array of the Commands.
-
#deep_commands ⇒ Object
Returns a flat array containing all the commands in this tree.
-
#default_args ⇒ Object
Returns an array of all the default Args.
-
#default_command ⇒ Object
If any of this command’s subcommands has the default option set to true, this default command will be returned, nil otherwise.
-
#default_flags ⇒ Object
Returns an array of all the default Flags.
-
#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.
-
#required_args ⇒ Object
Returns an array of all the required Arguments.
-
#required_environment_variables ⇒ Object
Returns an array of all the required EnvironmentVariables.
-
#required_flags ⇒ Object
Returns an array of all the required Flags.
-
#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.
-
#verify ⇒ Object
Raise an exception if there are some serious issues with the command definition.
-
#whitelisted_args ⇒ Object
Returns an array of all the args with a whitelist.
-
#whitelisted_flags ⇒ Object
Returns an array of all the flags with a whitelist arg.
Methods included from Completions
#completion_data, #completion_function, #completion_script
Methods inherited from Base
#help, #initialize, #method_missing, #optional, #respond_to?, #summary
Methods included from Renderable
Constructor Details
This class inherits a constructor from Bashly::Models::Base
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Bashly::Models::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 tone (root) joined by space. For example, for a command like “docker container run” the action name is “container run”.
11 12 13 |
# File 'lib/bashly/models/command.rb', line 11 def action_name parents.any? ? (parents[1..-1] + [name]).join(' ') : "root" end |
#aliases ⇒ Object
Returns all the possible aliases for this command
16 17 18 |
# File 'lib/bashly/models/command.rb', line 16 def aliases short ? [name, short] : [name] end |
#args ⇒ Object
Returns an array of Arguments
21 22 23 24 25 26 |
# File 'lib/bashly/models/command.rb', line 21 def args return [] unless ["args"] ["args"].map do || Argument.new end end |
#caption_string ⇒ Object
Returns a string suitable to be a headline
29 30 31 |
# File 'lib/bashly/models/command.rb', line 29 def caption_string help ? "#{full_name} - #{summary}" : full_name end |
#catch_all_help ⇒ Object
Returns a used defined help string for the catch_all directive
47 48 49 50 51 52 53 54 55 |
# File 'lib/bashly/models/command.rb', line 47 def catch_all_help return nil unless catch_all if catch_all.is_a?(Hash) and catch_all['help'].is_a?(String) catch_all['help'] else nil end end |
#catch_all_label ⇒ Object
Returns a label for the catch_all directive
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/bashly/models/command.rb', line 34 def catch_all_label return nil unless catch_all if catch_all.is_a? String "#{catch_all.upcase}..." elsif catch_all.is_a?(Hash) and catch_all['label'].is_a?(String) "#{catch_all['label'].upcase}..." else "..." end end |
#command_names ⇒ Object
Returns only the names of the Commands
58 59 60 |
# File 'lib/bashly/models/command.rb', line 58 def command_names commands.map &:name end |
#commands ⇒ Object
Returns an array of the Commands
63 64 65 66 67 68 69 |
# File 'lib/bashly/models/command.rb', line 63 def commands return [] unless ["commands"] ["commands"].map do || ['parents'] = parents + [name] Command.new end end |
#deep_commands ⇒ Object
Returns a flat array containing all the commands in this tree. This includes self + children + grandchildres + …
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/bashly/models/command.rb', line 73 def deep_commands result = [] commands.each do |command| result << command if command.commands.any? result += command.deep_commands end end result end |
#default_args ⇒ Object
Returns an array of all the default Args
85 86 87 |
# File 'lib/bashly/models/command.rb', line 85 def default_args args.select &:default end |
#default_command ⇒ Object
If any of this command’s subcommands has the default option set to true, this default command will be returned, nil otherwise.
91 92 93 |
# File 'lib/bashly/models/command.rb', line 91 def default_command commands.find { |c| c.default } end |
#default_flags ⇒ Object
Returns an array of all the default Flags
96 97 98 |
# File 'lib/bashly/models/command.rb', line 96 def default_flags flags.select &:default end |
#environment_variables ⇒ Object
Returns an array of EnvironmentVariables
101 102 103 104 105 106 |
# File 'lib/bashly/models/command.rb', line 101 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
110 111 112 |
# File 'lib/bashly/models/command.rb', line 110 def filename "#{action_name.to_underscore}_command.sh" end |
#flags ⇒ Object
Returns an array of Flags
115 116 117 118 119 120 |
# File 'lib/bashly/models/command.rb', line 115 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)
129 130 131 |
# File 'lib/bashly/models/command.rb', line 129 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
123 124 125 |
# File 'lib/bashly/models/command.rb', line 123 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.
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/bashly/models/command.rb', line 136 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 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
151 152 153 |
# File 'lib/bashly/models/command.rb', line 151 def parents ['parents'] || [] end |
#required_args ⇒ Object
Returns an array of all the required Arguments
156 157 158 |
# File 'lib/bashly/models/command.rb', line 156 def required_args args.select &:required end |
#required_environment_variables ⇒ Object
Returns an array of all the required EnvironmentVariables
161 162 163 |
# File 'lib/bashly/models/command.rb', line 161 def required_environment_variables environment_variables.select &:required end |
#required_flags ⇒ Object
Returns an array of all the required Flags
166 167 168 |
# File 'lib/bashly/models/command.rb', line 166 def required_flags flags.select &:required end |
#root_command? ⇒ Boolean
Returns trus if this is the root command (no parents)
171 172 173 |
# File 'lib/bashly/models/command.rb', line 171 def root_command? parents.empty? end |
#short_flag_exist?(flag) ⇒ Boolean
Returns true if one of the flags matches the provided short code
176 177 178 |
# File 'lib/bashly/models/command.rb', line 176 def short_flag_exist?(flag) flags.select { |f| f.short == flag }.any? end |
#usage_string ⇒ Object
Returns a constructed string suitable for Usage pattern
181 182 183 184 185 186 187 188 189 190 |
# File 'lib/bashly/models/command.rb', line 181 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_label}]" if catch_all 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
195 196 197 |
# File 'lib/bashly/models/command.rb', line 195 def user_lib @user_lib ||= Dir["#{Settings.source_dir}/lib/**/*.sh"] end |
#verify ⇒ Object
Raise an exception if there are some serious issues with the command definition.
201 202 203 |
# File 'lib/bashly/models/command.rb', line 201 def verify verify_commands if commands.any? end |
#whitelisted_args ⇒ Object
Returns an array of all the args with a whitelist
206 207 208 |
# File 'lib/bashly/models/command.rb', line 206 def whitelisted_args args.select &:allowed end |
#whitelisted_flags ⇒ Object
Returns an array of all the flags with a whitelist arg
211 212 213 |
# File 'lib/bashly/models/command.rb', line 211 def whitelisted_flags flags.select &:allowed end |