Class: Legion::CLI::Marketplace

Inherits:
Thor
  • Object
show all
Defined in:
lib/legion/cli/marketplace_command.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/legion/cli/marketplace_command.rb', line 8

def self.exit_on_failure?
  true
end

Instance Method Details

#approve(name) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/legion/cli/marketplace_command.rb', line 168

def approve(name)
  require 'legion/registry'
  out = formatter

  Legion::Registry.approve(name, notes: options[:notes])

  if options[:json]
    out.json(success: true, name: name, status: 'approved')
  else
    out.success("'#{name}' approved")
    out.detail({ 'Notes' => options[:notes] }) if options[:notes]
  end
rescue ArgumentError => e
  out.error(e.message)
end

#deprecate(name) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/legion/cli/marketplace_command.rb', line 213

def deprecate(name)
  require 'legion/registry'
  out = formatter

  sunset = parse_sunset_date(options[:sunset_date])
  Legion::Registry.deprecate(name, successor: options[:successor], sunset_date: sunset)

  if options[:json]
    out.json(success: true, name: name, status: 'deprecated',
             successor: options[:successor], sunset_date: options[:sunset_date])
  else
    out.success("'#{name}' marked as deprecated")
    detail_hash = {}
    detail_hash['Successor']   = options[:successor]   if options[:successor]
    detail_hash['Sunset Date'] = options[:sunset_date] if options[:sunset_date]
    out.detail(detail_hash) unless detail_hash.empty?
  end
rescue ArgumentError => e
  out.error(e.message)
end

#info(name) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/legion/cli/marketplace_command.rb', line 46

def info(name)
  require 'legion/registry'
  out = formatter
  entry = Legion::Registry.lookup(name)

  unless entry
    out.error("Extension '#{name}' not found")
    return
  end

  if options[:json]
    out.json(entry.to_h)
  else
    out.header("Extension: #{entry.name}")
    out.spacer
    out.detail(entry.to_h.compact)
  end
end

#install(name) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/legion/cli/marketplace_command.rb', line 240

def install(name)
  require 'legion/registry'
  require 'legion/extensions/gem_source'
  out = formatter

  unless name.start_with?('lex-')
    out.error("Extension name must start with 'lex-'")
    return
  end

  begin
    Connection.ensure_settings(resolve_secrets: false)
    Legion::Extensions::GemSource.setup!
  rescue StandardError => e
    Legion::Logging.debug("marketplace install: settings not available: #{e.message}") if defined?(Legion::Logging)
  end

  result = if options[:source]
             Legion::Extensions::GemSource.install_gem(name, source_override: options[:source])
           else
             Legion::Extensions::GemSource.install_gem(name)
           end

  if result[:success]
    entry = Legion::Registry::Entry.new(name: name, status: :active, airb_status: 'pending')
    Legion::Registry.register(entry)
    out.success("'#{name}' installed successfully")
  else
    out.error("Failed to install '#{name}'")
    puts result[:output] if result[:output]
  end
end

#listObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/legion/cli/marketplace_command.rb', line 73

def list
  require 'legion/registry'
  out = formatter
  extensions = build_extension_list

  if extensions.empty?
    out.warn('No extensions registered')
    return
  end

  if options[:json]
    out.json(extensions.map(&:to_h))
  else
    rows = extensions.map { |e| [e.name, e.version.to_s, e.status.to_s, e.risk_tier] }
    out.table(%w[Name Version Status Tier], rows)
    puts "  #{extensions.size} extension(s)"
  end
end

#publishObject



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/legion/cli/marketplace_command.rb', line 278

def publish
  require 'legion/registry'
  require 'legion/registry/security_scanner'
  out = formatter

  gemspec_files = Dir.glob('*.gemspec')
  if gemspec_files.empty?
    out.error('No gemspec found — publish aborted')
    return
  end

  gemspec_path = gemspec_files.first
  gem_name = File.basename(gemspec_path, '.gemspec')

  unless Kernel.system('bundle', 'exec', 'rspec')
    out.error('Specs failed — publish aborted')
    return
  end

  unless Kernel.system('bundle', 'exec', 'rubocop')
    out.error('Rubocop failed — publish aborted')
    return
  end

  unless Kernel.system('gem', 'build', gemspec_path)
    out.error("Failed to build gem '#{gem_name}'")
    return
  end

  gem_files = Dir.glob("#{gem_name}-*.gem")
  if gem_files.empty?
    out.error('No built gem file found after build')
    return
  end

  gem_file = gem_files.max_by { |f| File.mtime(f) }

  unless Kernel.system('gem', 'push', gem_file)
    out.error("Failed to push '#{gem_file}'")
    return
  end

  scanner = Legion::Registry::SecurityScanner.new
  scan_result = scanner.scan(name: gem_file)

  version = gem_file.sub("#{gem_name}-", '').sub('.gem', '')
  entry = Legion::Registry::Entry.new(name: gem_name, version: version,
                                      status: :active, airb_status: 'pending')
  Legion::Registry.register(entry)

  out.success("'#{gem_name}' v#{version} published — security: #{scan_result[:passed] ? 'passed' : 'failed'}")
end

#reject(name) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/legion/cli/marketplace_command.rb', line 190

def reject(name)
  require 'legion/registry'
  out = formatter

  Legion::Registry.reject(name, reason: options[:reason])

  if options[:json]
    out.json(success: true, name: name, status: 'rejected')
  else
    out.success("'#{name}' rejected")
    out.detail({ 'Reason' => options[:reason] }) if options[:reason]
  end
rescue ArgumentError => e
  out.error(e.message)
end

#reviewObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/legion/cli/marketplace_command.rb', line 143

def review
  require 'legion/registry'
  out = formatter
  pending = Legion::Registry.pending_reviews

  if pending.empty?
    out.warn('No extensions pending review')
    return
  end

  if options[:json]
    out.json(pending.map(&:to_h))
  else
    rows = pending.map { |e| [e.name, e.version.to_s, e.author.to_s, e..to_s] }
    out.table(%w[Name Version Author Submitted], rows)
    puts "  #{pending.size} pending review(s)"
  end
end

#scan(name) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/legion/cli/marketplace_command.rb', line 97

def scan(name)
  require 'legion/registry/security_scanner'
  out = formatter
  scanner = Legion::Registry::SecurityScanner.new
  result  = scanner.scan(name: name)

  if options[:json]
    out.json(result)
  else
    result[:checks].each do |check|
      color = check[:status] == :fail ? :critical : :nominal
      puts "  #{out.colorize(check[:check].to_s.ljust(25), color)} #{check[:status]} - #{check[:details]}"
    end
    if result[:passed]
      out.success('Scan PASSED')
    else
      out.error('Scan FAILED')
    end
  end
end

#search(query) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/legion/cli/marketplace_command.rb', line 20

def search(query)
  require 'legion/registry'
  out = formatter
  results = Legion::Registry.search(query)

  if results.empty?
    out.warn("No extensions found matching '#{query}'")
    return
  end

  if options[:json]
    out.json(results.map(&:to_h))
  else
    rows = results.map do |e|
      status_label = e.approved? ? 'approved' : (e.status || e.airb_status).to_s
      [e.name, e.version.to_s, status_label, (e.description || '')[0..60]]
    end
    out.table(%w[Name Version Status Description], rows)
  end
end

#stats(name) ⇒ Object



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/legion/cli/marketplace_command.rb', line 336

def stats(name)
  require 'legion/registry'
  out = formatter
  data = Legion::Registry.usage_stats(name)

  unless data
    out.error("Extension '#{name}' not found")
    return
  end

  if options[:json]
    out.json(data)
  else
    out.header("Usage Stats: #{name}")
    out.spacer
    out.detail({
                 'Install Count'    => data[:install_count].to_s,
                 'Active Instances' => data[:active_instances].to_s,
                 'Downloads (7d)'   => data[:downloads_7d].to_s,
                 'Downloads (30d)'  => data[:downloads_30d].to_s,
                 'Last Updated'     => data[:last_updated].to_s
               })
  end
end

#submit(name) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/legion/cli/marketplace_command.rb', line 123

def submit(name)
  require 'legion/registry'
  out = formatter

  Legion::Registry.submit_for_review(name)

  if options[:json]
    out.json(success: true, name: name, status: 'pending_review')
  else
    out.success("'#{name}' submitted for review")
  end
rescue ArgumentError => e
  out.error(e.message)
end