Class: Terraspace::Command

Inherits:
Thor
  • Object
show all
Extended by:
Util::Logging
Defined in:
lib/terraspace/command.rb

Constant Summary collapse

@@initial_dispatch_command =
nil

Class Method Summary collapse

Methods included from Util::Logging

logger

Class Method Details

.alter_command_description(command) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/terraspace/command.rb', line 140

def alter_command_description(command)
  return unless command

  # Add description to beginning of long_description
  long_desc = if command.long_description
      "#{command.description}\n\n#{command.long_description}"
    else
      command.description
    end

  # add reference url to end of the long_description
  unless website.empty?
    full_command = [command.ancestor_name, command.name].compact.join('-')
    url = "#{website}/reference/terraspace-#{full_command}"
    long_desc += "\n\nHelp also available at: #{url}"
  end

  command.long_description = long_desc
end

.check_project!(command_name) ⇒ Object



116
117
118
119
120
121
122
123
124
125
# File 'lib/terraspace/command.rb', line 116

def check_project!(command_name)
  return if subcommand?
  return if command_name.nil?
  return if help_flags.include?(Terraspace.argv.last) # IE: -h help
  return if %w[-h -v --version check_setup completion completion_script help new setup test version].include?(command_name)
  return if File.exist?("#{Terraspace.root}/config/app.rb")
  return unless Terraspace.check_project
  logger.error "ERROR: It doesn't look like this is a terraspace project. Are you sure you are in a terraspace project?".color(:red)
  ENV['TS_TEST'] ? raise : exit(1)
end

.check_standalone_install!Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/terraspace/command.rb', line 84

def check_standalone_install!
  return unless opt?
  version_manager = "rvm" if rvm?
  version_manager = "rbenv" if rbenv?
  if rbenv? || rvm?
    $stderr.puts <<~EOL.color(:yellow)
      WARN: It looks like a standalone Terraspace install and #{version_manager} is also in use.
      Different gems from the standalone Terraspace install and #{version_manager} can cause all kinds of trouble.
      Please install Terraspace as a gem instead and remove the standalone Terraspace /opt/terraspace installation.
      See: https://terraspace.cloud/docs/install/gem/
    EOL
  end
end

.command_help(shell, command_name) ⇒ Object

Override command_help to include the description at the top of the long_description.



133
134
135
136
137
138
# File 'lib/terraspace/command.rb', line 133

def command_help(shell, command_name)
  meth = normalize_command_name(command_name)
  command = all_commands[meth]
  alter_command_description(command)
  super
end

.dispatch(m, args, options, config) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/terraspace/command.rb', line 42

def dispatch(m, args, options, config)
  # Terraspace.argv provides consistency when terraspace is being called by rspec-terraspace test harness
  Terraspace.argv = args.clone # important to clone since Thor removes the first argv

  unless @@initial_dispatch_command
    @@initial_dispatch_command = "$ terraspace #{args.join(' ')}\n"
    Terraspace::Logger.buffer << @@initial_dispatch_command
  end

  check_standalone_install!
  check_project!(args.first)

  # Allow calling for help via:
  #   terraspace command help
  #   terraspace command -h
  #   terraspace command --help
  #   terraspace command -D
  #
  # as well thor's normal way:
  #
  #   terraspace help command
  if args.length > 1 && !(args & help_flags).empty?
    args -= help_flags
    args.insert(-2, "help")
  end

  #   terraspace version
  #   terraspace --version
  #   terraspace -v
  version_flags = ["--version", "-v"]
  if args.length == 1 && !(args & version_flags).empty?
    args = ["version"]
  end

  super
end

.exit_on_failure?Boolean

github.com/erikhuda/thor/issues/244 Deprecation warning: Thor exit with status 0 on errors. To keep this behavior, you must define ‘exit_on_failure?` in `Lono::CLI` You can silence deprecations warning by setting the environment variable THOR_SILENCE_DEPRECATION.

Returns:

  • (Boolean)


169
170
171
# File 'lib/terraspace/command.rb', line 169

def exit_on_failure?
  true
end

.help_flagsObject



79
80
81
# File 'lib/terraspace/command.rb', line 79

def help_flags
  Thor::HELP_MAPPINGS + ["help"]
end

.initial_dispatch_commandObject



33
34
35
# File 'lib/terraspace/command.rb', line 33

def initial_dispatch_command
  @@initial_dispatch_command
end

.opt?Boolean

Returns:

  • (Boolean)


98
99
100
101
102
# File 'lib/terraspace/command.rb', line 98

def opt?
  paths = ENV['PATH'].split(':')
  opt = paths.detect { |p| p.include?('/opt/terraspace') }
  opt && File.exist?('/opt/terraspace')
end

.rbenv?Boolean

Returns:

  • (Boolean)


110
111
112
113
114
# File 'lib/terraspace/command.rb', line 110

def rbenv?
  paths = ENV['PATH'].split(':')
  rbenv = paths.detect { |p| p.include?('/rbenv/') || p.include?('/.rbenv/') }
  rbenv && system("type rbenv > /dev/null 2>&1")
end

.reset_dispatch_commandObject

use by test framework



38
39
40
# File 'lib/terraspace/command.rb', line 38

def reset_dispatch_command
  @@initial_dispatch_command = nil
end

.rvm?Boolean

Returns:

  • (Boolean)


104
105
106
107
108
# File 'lib/terraspace/command.rb', line 104

def rvm?
  paths = ENV['PATH'].split(':')
  rvm = paths.detect { |p| p.include?('/rvm/') || p.include?('/.rvm/') }
  rvm && system("type rvm > /dev/null 2>&1")
end

.subcommand?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/terraspace/command.rb', line 127

def subcommand?
  !!caller.detect { |l| l.include?('in subcommand') }
end

.websiteObject

meant to be overriden



162
163
164
# File 'lib/terraspace/command.rb', line 162

def website
  "https://terraspace.cloud"
end