class Bashly::Models::Command

Public Instance Methods

action_name() click to toggle source

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”.

# File lib/bashly/models/command.rb, line 11
def action_name
  parents.any? ? (parents[1..-1] + [name]).join(' ') : "root"
end
aliases() click to toggle source

Returns all the possible aliases for this command

# File lib/bashly/models/command.rb, line 16
def aliases
  short ? [name, short] : [name]
end
args() click to toggle source

Returns an array of Arguments

# File lib/bashly/models/command.rb, line 21
def args
  return [] unless options["args"]
  options["args"].map do |options|
    Argument.new options
  end
end
caption_string() click to toggle source

Returns a string suitable to be a headline

# File lib/bashly/models/command.rb, line 29
def caption_string
  help ? "#{full_name} - #{summary}" : full_name
end
catch_all_help() click to toggle source

Returns a used defined help string for the catch_all directive

# 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() click to toggle source

Returns a label for the catch_all directive

# 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
catch_all_required?() click to toggle source

Returns true if catch_all is required

# File lib/bashly/models/command.rb, line 58
def catch_all_required?
  catch_all.is_a?(Hash) and catch_all['required']
end
catch_all_usage() click to toggle source

Returns a string suitable for catch_all Usage pattern

# File lib/bashly/models/command.rb, line 63
def catch_all_usage
  return nil unless catch_all
  catch_all_required? ? catch_all_label : "[#{catch_all_label}]"
end
command_names() click to toggle source

Returns only the names of the Commands

# File lib/bashly/models/command.rb, line 69
def command_names
  commands.map &:name
end
commands() click to toggle source

Returns an array of the Commands

# File lib/bashly/models/command.rb, line 74
def commands
  return [] unless options["commands"]
  options["commands"].map do |options|
    options['parents'] = parents + [name]
    Command.new options
  end
end
deep_commands() click to toggle source

Returns a flat array containing all the commands in this tree. This includes self + children + grandchildres + …

# File lib/bashly/models/command.rb, line 84
def deep_commands
  result = []
  commands.each do |command|
    result << command
    if command.commands.any?
      result += command.deep_commands
    end
  end
  result
end
default_args() click to toggle source

Returns an array of all the default Args

# File lib/bashly/models/command.rb, line 96
def default_args
  args.select &:default
end
default_command() click to toggle source

If any of this command's subcommands has the default option set to true, this default command will be returned, nil otherwise.

# File lib/bashly/models/command.rb, line 102
def default_command
  commands.find { |c| c.default }
end
default_environment_variables() click to toggle source

Returns an array of all the default Environment Variables

# File lib/bashly/models/command.rb, line 107
def default_environment_variables
  environment_variables.select &:default
end
default_flags() click to toggle source

Returns an array of all the default Flags

# File lib/bashly/models/command.rb, line 112
def default_flags
  flags.select &:default
end
environment_variables() click to toggle source

Returns an array of EnvironmentVariables

# File lib/bashly/models/command.rb, line 117
def environment_variables
  return [] unless options["environment_variables"]
  options["environment_variables"].map do |options|
    EnvironmentVariable.new options
  end
end
filename() click to toggle source

Returns the bash filename that is expected to hold the user code for this command

# File lib/bashly/models/command.rb, line 126
def filename
  "#{action_name.to_underscore}_command.sh"
end
flags() click to toggle source

Returns an array of Flags

# File lib/bashly/models/command.rb, line 131
def flags
  return [] unless options["flags"]
  options["flags"].map do |options|
    Flag.new options
  end
end
full_name() click to toggle source

Returns the name of the command, including its parent name (in case this is a subcommand)

# File lib/bashly/models/command.rb, line 145
def full_name
  parents.any? ? (parents + [name]).join(' ') : name
end
function_name() click to toggle source

Returns a unique name, suitable to be used in a bash function

# File lib/bashly/models/command.rb, line 139
def function_name
  full_name.to_underscore
end
load_user_file(file, placeholder: true) click to toggle source

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.

# File lib/bashly/models/command.rb, line 152
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() click to toggle source

Returns an array of all parents. For example, the command “docker container run” will have [docker, container] as its parents

# File lib/bashly/models/command.rb, line 167
def parents
  options['parents'] || []
end
required_args() click to toggle source

Returns an array of all the required Arguments

# File lib/bashly/models/command.rb, line 172
def required_args
  args.select &:required
end
required_environment_variables() click to toggle source

Returns an array of all the required EnvironmentVariables

# File lib/bashly/models/command.rb, line 177
def required_environment_variables
  environment_variables.select &:required
end
required_flags() click to toggle source

Returns an array of all the required Flags

# File lib/bashly/models/command.rb, line 182
def required_flags
  flags.select &:required
end
root_command?() click to toggle source

Returns trus if this is the root command (no parents)

# File lib/bashly/models/command.rb, line 187
def root_command?
  parents.empty?
end
short_flag_exist?(flag) click to toggle source

Returns true if one of the flags matches the provided short code

# File lib/bashly/models/command.rb, line 192
def short_flag_exist?(flag)
  flags.select { |f| f.short == flag }.any?
end
usage_string() click to toggle source

Returns a constructed string suitable for Usage pattern

# File lib/bashly/models/command.rb, line 197
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 if catch_all
  result.join " "
end
user_lib() click to toggle source

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

# File lib/bashly/models/command.rb, line 211
def user_lib
  @user_lib ||= Dir["#{Settings.source_dir}/lib/**/*.sh"].sort
end
verify() click to toggle source

Raise an exception if there are some serious issues with the command definition.

# File lib/bashly/models/command.rb, line 217
def verify
  verify_commands if commands.any?
end
whitelisted_args() click to toggle source

Returns an array of all the args with a whitelist

# File lib/bashly/models/command.rb, line 222
def whitelisted_args
  args.select &:allowed
end
whitelisted_flags() click to toggle source

Returns an array of all the flags with a whitelist arg

# File lib/bashly/models/command.rb, line 227
def whitelisted_flags
  flags.select &:allowed
end

Private Instance Methods

verify_commands() click to toggle source
# File lib/bashly/models/command.rb, line 233
def verify_commands
  if args.any? or flags.any?
    raise ConfigurationError, "Error in the !txtgrn!#{full_name}!txtrst! command.\nThe !txtgrn!commands!txtrst! key cannot be at the same level as the !txtgrn!args!txtrst! or !txtgrn!flags!txtrst! keys."
  end
end