Class: Bashly::Models::Command

Inherits:
Base
  • Object
show all
Defined in:
lib/bashly/models/command.rb

Constant Summary

Constants inherited from Base

Base::OPTION_KEYS

Instance Attribute Summary

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Base

#help, #initialize, #method_missing, #optional, #respond_to?, #summary

Methods included from Renderable

#render, #strings

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_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 tone (root) joined by space. For example, for a command like “docker container run” the action name is “container run”.



9
10
11
# File 'lib/bashly/models/command.rb', line 9

def action_name
  parents.any? ? (parents[1..-1] + [name]).join(' ') : "root"
end

#aliasesObject

Returns all the possible aliases for this command



14
15
16
# File 'lib/bashly/models/command.rb', line 14

def aliases
  short ? [name, short] : [name]
end

#argsObject

Returns an array of Arguments



19
20
21
22
23
24
# File 'lib/bashly/models/command.rb', line 19

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



27
28
29
# File 'lib/bashly/models/command.rb', line 27

def caption_string
  help ? "#{full_name} - #{summary}" : full_name
end

#command_namesObject

Returns only the names of the Commands



32
33
34
# File 'lib/bashly/models/command.rb', line 32

def command_names
  commands.map &:name
end

#commandsObject

Returns an array of the Commands



37
38
39
40
41
42
43
# File 'lib/bashly/models/command.rb', line 37

def commands
  return [] unless options["commands"]
  options["commands"].map do |options|
    options['parents'] = parents + [name]
    Command.new options
  end
end

#deep_commandsObject

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



47
48
49
50
51
52
53
54
55
56
# File 'lib/bashly/models/command.rb', line 47

def deep_commands
  result = []
  commands.each do |command|
    result << command
    if command.commands.any?
      result += command.deep_commands
    end
  end
  result
end

#default_argsObject

Returns an array of all the default Args



59
60
61
# File 'lib/bashly/models/command.rb', line 59

def default_args
  args.select &:default
end

#default_commandObject

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



65
66
67
# File 'lib/bashly/models/command.rb', line 65

def default_command
  commands.find { |c| c.default }
end

#default_flagsObject

Returns an array of all the default Flags



70
71
72
# File 'lib/bashly/models/command.rb', line 70

def default_flags
  flags.select &:default
end

#environment_variablesObject

Returns an array of EnvironmentVariables



75
76
77
78
79
80
# File 'lib/bashly/models/command.rb', line 75

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



84
85
86
# File 'lib/bashly/models/command.rb', line 84

def filename
  "#{action_name.to_underscore}_command.sh"
end

#flagsObject

Returns an array of Flags



89
90
91
92
93
94
# File 'lib/bashly/models/command.rb', line 89

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)



103
104
105
# File 'lib/bashly/models/command.rb', line 103

def full_name
  parents.any? ? (parents + [name]).join(' ') : name
end

#function_nameObject

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



97
98
99
# File 'lib/bashly/models/command.rb', line 97

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.



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/bashly/models/command.rb', line 110

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

#parentsObject

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



125
126
127
# File 'lib/bashly/models/command.rb', line 125

def parents
  options['parents'] || []
end

#required_argsObject

Returns an array of all the required Arguments



130
131
132
# File 'lib/bashly/models/command.rb', line 130

def required_args
  args.select &:required
end

#required_environment_variablesObject

Returns an array of all the required EnvironmentVariables



135
136
137
# File 'lib/bashly/models/command.rb', line 135

def required_environment_variables
  environment_variables.select &:required
end

#required_flagsObject

Returns an array of all the required Flags



140
141
142
# File 'lib/bashly/models/command.rb', line 140

def required_flags
  flags.select &:required
end

#root_command?Boolean

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

Returns:

  • (Boolean)


145
146
147
# File 'lib/bashly/models/command.rb', line 145

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)


150
151
152
# File 'lib/bashly/models/command.rb', line 150

def short_flag_exist?(flag)
  flags.select { |f| f.short == flag }.any?
end

#usage_stringObject

Returns a constructed string suitable for Usage pattern



155
156
157
158
159
160
161
162
163
# File 'lib/bashly/models/command.rb', line 155

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



168
169
170
# File 'lib/bashly/models/command.rb', line 168

def user_lib
  @user_lib ||= Dir["#{Settings.source_dir}/lib/**/*.sh"]
end

#verifyObject

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



174
175
176
# File 'lib/bashly/models/command.rb', line 174

def verify
  verify_commands if commands.any?
end