Class: Bashly::Script::Command

Inherits:
Base
  • Object
show all
Includes:
CommandScopes, Completions
Defined in:
lib/bashly/script/command.rb

Constant Summary

Constants inherited from Base

Base::OPTION_KEYS

Instance Attribute Summary

Attributes inherited from Base

#options

Instance Method Summary collapse

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

#render, #strings

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_nameObject

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

#aliasesObject

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

#argsObject

Returns an array of Arguments



22
23
24
25
26
27
# File 'lib/bashly/script/command.rb', line 22

def args
  return [] unless options["args"]
  options["args"].map do |options|
    Argument.new options
  end
end

#caption_stringObject

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_allObject



34
35
36
# File 'lib/bashly/script/command.rb', line 34

def catch_all
  @catch_all ||= CatchAll.from_config options['catch_all']
end

#commandsObject

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 options["commands"]
  options["commands"].map do |options|
    options['parents'] = parents + [name]
    Command.new options
  end
end

#environment_variablesObject

Returns an array of EnvironmentVariables



48
49
50
51
52
53
# File 'lib/bashly/script/command.rb', line 48

def environment_variables
  return [] unless options["environment_variables"]
  options["environment_variables"].map do |options|
    EnvironmentVariable.new options
  end
end

#filenameObject

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
  options["filename"] || "#{action_name.to_underscore}_command.sh"
end

#flagsObject

Returns an array of Flags



62
63
64
65
66
67
# File 'lib/bashly/script/command.rb', line 62

def flags
  return [] unless options["flags"]
  options["flags"].map do |options|
    Flag.new options
  end
end

#full_nameObject

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_nameObject

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

#parentsObject

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
  options['parents'] || []
end

#root_command?Boolean

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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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_stringObject

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_libObject

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