Class: Command::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/command/base.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

USAGE =

Used to call the command (‘cpl NAME`) NAME = “” Displayed when running `cpl help` or `cpl help NAME` (defaults to `NAME`)

""
REQUIRES_ARGS =

Throws error if ‘true` and no arguments are passed to the command or if `false` and arguments are passed to the command

false
DEFAULT_ARGS =

Default arguments if none are passed to the command

[].freeze
OPTIONS =

Options for the command (use option methods below)

[].freeze
EXAMPLES =

Displayed when running ‘cpl help` DESCRIPTION = “” Displayed when running `cpl help NAME` LONG_DESCRIPTION = “” Displayed along with `LONG_DESCRIPTION` when running `cpl help NAME`

""
HIDE =

If ‘true`, hides the command from `cpl help`

false
NO_IMAGE_AVAILABLE =
"NO_IMAGE_AVAILABLE"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Base

Returns a new instance of Base.



29
30
31
# File 'lib/command/base.rb', line 29

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/command/base.rb', line 5

def config
  @config
end

Class Method Details

.all_commandsObject



33
34
35
36
37
38
39
# File 'lib/command/base.rb', line 33

def self.all_commands
  Dir["#{__dir__}/*.rb"].each_with_object({}) do |file, result|
    filename = File.basename(file, ".rb")
    classname = File.read(file).match(/^\s+class (\w+) < Base($| .*$)/)&.captures&.first
    result[filename.to_sym] = Object.const_get("::Command::#{classname}") if classname
  end
end

.all_optionsObject



156
157
158
# File 'lib/command/base.rb', line 156

def self.all_options
  methods.grep(/_option$/).map { |method| send(method.to_s) }
end

.all_options_by_key_nameObject



160
161
162
163
164
165
# File 'lib/command/base.rb', line 160

def self.all_options_by_key_name
  all_options.each_with_object({}) do |option, result|
    option[:params][:aliases]&.each { |current_alias| result[current_alias.to_s] = option }
    result["--#{option[:name]}"] = option
  end
end

.app_option(required: false) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/command/base.rb', line 54

def self.app_option(required: false)
  {
    name: :app,
    params: {
      aliases: ["-a"],
      banner: "APP_NAME",
      desc: "Application name",
      type: :string,
      required: required
    }
  }
end

.commit_option(required: false) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/command/base.rb', line 93

def self.commit_option(required: false)
  {
    name: :commit,
    params: {
      aliases: ["-c"],
      banner: "COMMIT_HASH",
      desc: "Commit hash",
      type: :string,
      required: required
    }
  }
end

.image_option(required: false) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/command/base.rb', line 80

def self.image_option(required: false)
  {
    name: :image,
    params: {
      aliases: ["-i"],
      banner: "IMAGE_NAME",
      desc: "Image name",
      type: :string,
      required: required
    }
  }
end

.org_option(required: false) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/command/base.rb', line 41

def self.org_option(required: false)
  {
    name: :org,
    params: {
      aliases: ["-o"],
      banner: "ORG_NAME",
      desc: "Organization name",
      type: :string,
      required: required
    }
  }
end

.skip_confirm_option(required: false) ⇒ Object



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

def self.skip_confirm_option(required: false)
  {
    name: :yes,
    params: {
      aliases: ["-y"],
      banner: "SKIP_CONFIRM",
      desc: "Skip confirmation",
      type: :boolean,
      required: required
    }
  }
end

.upstream_token_option(required: false) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/command/base.rb', line 106

def self.upstream_token_option(required: false)
  {
    name: :upstream_token,
    params: {
      aliases: ["-t"],
      banner: "UPSTREAM_TOKEN",
      desc: "Upstream token",
      type: :string,
      required: required
    }
  }
end

.use_local_token_option(required: false) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'lib/command/base.rb', line 145

def self.use_local_token_option(required: false)
  {
    name: :use_local_token,
    params: {
      desc: "Override remote CPLN_TOKEN with local token",
      type: :boolean,
      required: required
    }
  }
end

.version_option(required: false) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/command/base.rb', line 132

def self.version_option(required: false)
  {
    name: :version,
    params: {
      aliases: ["-v"],
      banner: "VERSION",
      desc: "Displays the current version of the CLI",
      type: :boolean,
      required: required
    }
  }
end

.workload_option(required: false) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/command/base.rb', line 67

def self.workload_option(required: false)
  {
    name: :workload,
    params: {
      aliases: ["-w"],
      banner: "WORKLOAD_NAME",
      desc: "Workload name",
      type: :string,
      required: required
    }
  }
end

Instance Method Details

#args_join(args) ⇒ Object

NOTE: use simplified variant atm, as shelljoin do different escaping TODO: most probably need better logic for escaping various quotes



224
225
226
# File 'lib/command/base.rb', line 224

def args_join(args)
  args.join(" ")
end

#cpObject



258
259
260
# File 'lib/command/base.rb', line 258

def cp
  @cp ||= Controlplane.new(config)
end

#ensure_workload_deleted(workload) ⇒ Object



186
187
188
189
# File 'lib/command/base.rb', line 186

def ensure_workload_deleted(workload)
  progress.puts "- Ensure workload is deleted"
  cp.workload_delete(workload)
end

#latest_image(app = config.app, org = config.org) ⇒ Object



203
204
205
206
207
208
209
210
# File 'lib/command/base.rb', line 203

def latest_image(app = config.app, org = config.org)
  @latest_image ||= {}
  @latest_image[app] ||=
    begin
      items = cp.image_query(app, org)["items"]
      latest_image_from(items, app_name: app)
    end
end

#latest_image_from(items, app_name: config.app, name_only: true) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/command/base.rb', line 191

def latest_image_from(items, app_name: config.app, name_only: true)
  matching_items = items.filter { |item| item["name"].start_with?("#{app_name}:") }

  # Or special string to indicate no image available
  if matching_items.empty?
    "#{app_name}:#{NO_IMAGE_AVAILABLE}"
  else
    latest_item = matching_items.max_by { |item| extract_image_number(item["name"]) }
    name_only ? latest_item["name"] : latest_item
  end
end

#latest_image_next(app = config.app, org = config.org) ⇒ Object



212
213
214
215
216
217
218
219
220
# File 'lib/command/base.rb', line 212

def latest_image_next(app = config.app, org = config.org)
  @latest_image_next ||= {}
  @latest_image_next[app] ||= begin
    image = latest_image(app, org).split(":").first
    image += ":#{extract_image_number(latest_image) + 1}"
    image += "_#{config.options[:commit]}" if config.options[:commit]
    image
  end
end

#perform(cmd) ⇒ Object



262
263
264
# File 'lib/command/base.rb', line 262

def perform(cmd)
  system(cmd) || exit(false)
end

#progressObject



228
229
230
# File 'lib/command/base.rb', line 228

def progress
  $stderr
end

#step(message, abort_on_error: true) ⇒ Object

rubocop:disable Metrics/MethodLength



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/command/base.rb', line 232

def step(message, abort_on_error: true) # rubocop:disable Metrics/MethodLength
  progress.print("#{message}...")

  Shell.use_tmp_stderr do
    success = false

    begin
      success = yield
    rescue RuntimeError => e
      message = e.message
      if abort_on_error
        progress.puts(" #{Shell.color('failed!', :red)}\n\n")
        Shell.abort(message)
      else
        Shell.write_to_tmp_stderr(message)
      end
    end

    if success
      progress.puts(" #{Shell.color('done!', :green)}")
    else
      progress.puts(" #{Shell.color('failed!', :red)}\n\n#{Shell.read_from_tmp_stderr}\n\n")
    end
  end
end

#wait_for(title) ⇒ Object



167
168
169
170
171
172
173
174
# File 'lib/command/base.rb', line 167

def wait_for(title)
  progress.print "- Waiting for #{title}"
  until yield
    progress.print(".")
    sleep(1)
  end
  progress.puts
end

#wait_for_replica(workload, location) ⇒ Object



180
181
182
183
184
# File 'lib/command/base.rb', line 180

def wait_for_replica(workload, location)
  wait_for("replica") do
    cp.workload_get_replicas_safely(workload, location: location)&.dig("items", 0)
  end
end

#wait_for_workload(workload) ⇒ Object



176
177
178
# File 'lib/command/base.rb', line 176

def wait_for_workload(workload)
  wait_for("workload to start") { cp.fetch_workload(workload) }
end