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



132
133
134
# File 'lib/cpl.rb', line 132

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

.check_cpl_versionObject

rubocop:disable Metrics/MethodLength



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

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



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

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



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

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)


107
108
109
# File 'lib/cpl.rb', line 107

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



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

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)


112
113
114
115
116
# File 'lib/cpl.rb', line 112

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

  super(word, type)
end

.start(*args) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/cpl.rb', line 47

def self.start(*args)
  check_cpln_version
  check_cpl_version
  fix_help_option

  super(*args)
end