Class: Cpl::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/cpl.rb

Overview

rubocop:disable Metrics/ClassLength

Class Method Summary collapse

Class Method Details

.all_base_commandsObject



134
135
136
# File 'lib/cpl.rb', line 134

def self.all_base_commands
  ::Command::Base.all_commands.merge(deprecated_commands)
end

.check_cpl_versionObject

rubocop:disable Metrics/MethodLength



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cpl.rb', line 77

def self.check_cpl_version # rubocop:disable Metrics/MethodLength
  return if @checked_cpl_version

  @checked_cpl_version = true

  result = `gem search ^cpl$ --remote 2>/dev/null`
  return unless $CHILD_STATUS.success?

  matches = result.match(/cpl \((.+)\)/)
  return unless matches

  version = Cpl::VERSION
  latest_version = matches[1]
  return unless Gem::Version.new(version) < Gem::Version.new(latest_version)

  ::Shell.warn("You are not using the latest 'cpl' version. Please update it with 'gem update cpl'.")
  $stderr.puts
end

.check_cpln_versionObject

rubocop:disable Metrics/MethodLength



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cpl.rb', line 57

def self.check_cpln_version # rubocop:disable Metrics/MethodLength
  return if @checked_cpln_version

  @checked_cpln_version = true

  result = `cpln --version 2>/dev/null`
  if $CHILD_STATUS.success?
    data = JSON.parse(result)

    version = data["npm"]
    min_version = Cpl::MIN_CPLN_VERSION
    if Gem::Version.new(version) < Gem::Version.new(min_version)
      ::Shell.abort("Current 'cpln' version: #{version}. Minimum supported version: #{min_version}. " \
                    "Please update it with 'npm update -g @controlplane/cli'.")
    end
  else
    ::Shell.abort("Can't find 'cpln' executable. Please install it with 'npm install -g @controlplane/cli'.")
  end
end

.deprecated_commandsObject



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/cpl.rb', line 120

def self.deprecated_commands
  @deprecated_commands ||= begin
    deprecated_commands_file_path = "#{__dir__}/deprecated_commands.json"
    deprecated_commands_data = File.binread(deprecated_commands_file_path)
    deprecated_commands = JSON.parse(deprecated_commands_data)
    deprecated_commands.to_h do |old_command_name, new_command_name|
      file_name = new_command_name.gsub(/[^A-Za-z]/, "_")
      class_name = file_name.split("_").map(&:capitalize).join

      [old_command_name, Object.const_get("::Command::#{class_name}")]
    end
  end
end

.exit_on_failure?Boolean

Needed to silence deprecation warning

Returns:

  • (Boolean)


109
110
111
# File 'lib/cpl.rb', line 109

def self.exit_on_failure?
  true
end

.fix_help_optionObject

This is so that we’re able to run ‘cpl COMMAND –help` to print the help (it basically changes it to `cpl –help COMMAND`, which Thor recognizes) Based on stackoverflow.com/questions/49042591/how-to-add-help-h-flag-to-thor-command



99
100
101
102
103
104
105
106
# File 'lib/cpl.rb', line 99

def self.fix_help_option
  help_mappings = Thor::HELP_MAPPINGS + ["help"]
  matches = help_mappings & ARGV
  matches.each do |match|
    ARGV.delete(match)
    ARGV.unshift(match)
  end
end

.is_thor_reserved_word?(word, type) ⇒ Boolean

Needed to be able to use “run” as a command

Returns:

  • (Boolean)


114
115
116
117
118
# File 'lib/cpl.rb', line 114

def self.is_thor_reserved_word?(word, type) # rubocop:disable Naming/PredicateName
  return false if word == "run"

  super(word, type)
end

.show_info_header(config) ⇒ Object

rubocop:disable Metrics/MethodLength



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/cpl.rb', line 202

def self.show_info_header(config) # rubocop:disable Metrics/MethodLength
  return if @showed_info_header

  rows = {}
  rows["ORG"] = config.org || "NOT PROVIDED!"
  rows["ORG"] += " (comes from CPLN_ORG env var)" if config.org_comes_from_env
  rows["APP"] = config.app || "NOT PROVIDED!"
  rows["APP"] += " (comes from CPLN_APP env var)" if config.app_comes_from_env

  rows.each do |key, value|
    puts "#{key}: #{value}"
  end

  @showed_info_header = true

  # Add a newline after the info header
  puts
end

.start(*args) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/cpl.rb', line 49

def self.start(*args)
  check_cpln_version
  check_cpl_version
  fix_help_option

  super(*args)
end