class Chelsea::CLI

This class provides an interface to the oss index, gems and deps

Public Class Methods

new(opts) click to toggle source
# File lib/chelsea/cli.rb, line 32
def initialize(opts)
  @opts = opts
  @pastel = Pastel.new
  _validate_arguments
  _show_logo # Move to formatter
end
version() click to toggle source

rubocop:enable Metrics/CyclomaticComplexity

# File lib/chelsea/cli.rb, line 62
def self.version
  Chelsea::VERSION
end

Public Instance Methods

process!() click to toggle source

rubocop:disable Metrics/CyclomaticComplexity

# File lib/chelsea/cli.rb, line 40
def process! # rubocop:disable Metrics/AbcSize, Metrics/MethodLength,  Metrics/PerceivedComplexity
  if @opts.config?
    _set_config # move to init
  elsif @opts.clear?
    require_relative 'db'
    Chelsea::DB.new.clear_cache
    puts 'OSS Index cache cleared'
  elsif @opts.file? && @opts.iq?
    dependencies = _process_file_iq
    _submit_sbom(dependencies)
  elsif !@opts.file? && @opts.iq?
    abort 'Missing the --file argument. It is required with the --iq argument.'
  elsif @opts.file?
    _process_file
  elsif @opts.help? # quit on opts.help earlier
    puts _cli_flags # this doesn't exist
  else
    abort 'Missing arguments! Chelsea did nothing. Try providing the --file <Gemfile.lock> argument.'
  end
end

Private Instance Methods

_flags() click to toggle source
# File lib/chelsea/cli.rb, line 142
def _flags
  # Seems wrong, should all be handled by bin
  %i[file help config]
end
_flags_error() click to toggle source
# File lib/chelsea/cli.rb, line 125
def _flags_error
  switches = _flags.collect { |f| "--#{f}" }
  abort "please set one of #{switches}"
end
_flags_set?() click to toggle source
# File lib/chelsea/cli.rb, line 136
def _flags_set?
  # I'm still unsure what this is trying to express
  valid_flags = _flags.collect { |arg| @opts[arg] }.compact
  valid_flags.count > 1
end
_load_config() click to toggle source
# File lib/chelsea/cli.rb, line 153
def _load_config
  config = Chelsea::Config.new
  config.oss_index_config
end
_process_file() click to toggle source
# File lib/chelsea/cli.rb, line 106
def _process_file
  gems = Chelsea::Gems.new(
    file: @opts[:file],
    verbose: @opts[:verbose],
    options: @opts
  )
  gems.execute ? (exit 1) : (exit 0)
end
_process_file_iq() click to toggle source
# File lib/chelsea/cli.rb, line 115
def _process_file_iq
  gems = Chelsea::Gems.new(
    file: @opts[:file],
    verbose: @opts[:verbose],
    options: @opts
  )
  gems.collect_iq
  gems
end
_set_config() click to toggle source
# File lib/chelsea/cli.rb, line 158
def _set_config
  Chelsea.read_oss_index_config_from_command_line
end
_submit_sbom(gems) click to toggle source
# File lib/chelsea/cli.rb, line 68
def _submit_sbom(gems) # rubocop:disable Metrics/MethodLength
  iq = Chelsea::IQClient.new(
    options: {
      public_application_id: @opts[:application],
      server_url: @opts[:server],
      username: @opts[:iquser],
      auth_token: @opts[:iqpass],
      stage: @opts[:stage]
    }
  )
  bom = Chelsea::Bom.new(gems.deps.dependencies).collect

  status_url = iq.post_sbom(bom)

  return unless status_url

  msg, color, exit_code = iq.poll_status(status_url)
  show_status(msg, color)
  # this may not be very ruby-esque, but `return exit_code` and `exit_code` didn't result in the desired exit status
  exit exit_code
end
_validate_arguments() click to toggle source
# File lib/chelsea/cli.rb, line 130
def _validate_arguments
  return unless !_flags_set? && !@opts.file?

  _flags_error
end
show_status(msg, color) click to toggle source
# File lib/chelsea/cli.rb, line 90
def show_status(msg, color)
  case color
  when Chelsea::IQClient::COLOR_FAILURE
    puts @pastel.red.bold(msg)
  when Chelsea::IQClient::COLOR_WARNING
    # want yellow, but that doesn't print
    # puts @pastel.color.bold(msg, color)
    puts @pastel.blue.blue(msg)
  when Chelsea::IQClient::COLOR_NONE
    # want yellow, but that doesn't print
    puts @pastel.green.bold(msg)
  else
    puts @pastel.bold(msg)
  end
end