Class: Gem::Guardian::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/gem/guardian/cli.rb

Overview

Command-line entry point for gem-guardian. rubocop:disable Metrics/ClassLength

Defined Under Namespace

Classes: LockfileDataView

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv, stdout: $stdout, stderr: $stderr, verifier_class: Verifier, lockfile_parser_class: LockfileParser, provenance_verifier_class: ProvenanceVerifier, report_builder_class: ReportBuilder) ⇒ CLI

Returns a new instance of CLI.



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/gem/guardian/cli.rb', line 73

def initialize(argv, stdout: $stdout, stderr: $stderr, verifier_class: Verifier,
               lockfile_parser_class: LockfileParser, provenance_verifier_class: ProvenanceVerifier,
               report_builder_class: ReportBuilder)
  @argv = argv.dup
  @stdout = stdout
  @stderr = stderr
  @verifier_class = verifier_class
  @lockfile_parser_class = lockfile_parser_class
  @provenance_verifier_class = provenance_verifier_class
  @report_builder_class = report_builder_class
  @result_printer = ResultPrinter.new(stdout:)
end

Instance Attribute Details

#checksumsHash{Dependency => Hash{String => String}} (readonly)

Returns checksum algorithms keyed by dependency.

Returns:

  • (Hash{Dependency => Hash{String => String}})

    checksum algorithms keyed by dependency



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/gem/guardian/cli.rb', line 29

LockfileDataView = Data.define(:dependencies, :checksums, :checksums_section_present) do
  # Looks up a checksum for a dependency and algorithm.
  #
  # @param dependency [Dependency] dependency to look up
  # @param algorithm [String] checksum algorithm name, currently usually
  #   +"sha256"+
  # @return [String, nil] checksum digest when present, otherwise +nil+
  def checksum_for(dependency, algorithm = "sha256")
    checksums.fetch(dependency, {}).fetch(algorithm, nil)
  end

  # Returns only SHA256 checksums from the filtered lockfile data.
  #
  # @return [Hash{Dependency => String}] selected dependencies mapped to
  #   their SHA256 digest
  def sha256_checksums
    checksums.each_with_object({}) do |(dependency, algorithms), memo|
      digest = algorithms["sha256"]
      memo[dependency] = digest if digest
    end
  end

  # Lists selected dependencies that do not have SHA256 lockfile coverage.
  #
  # @return [Array<Dependency>] dependencies missing a SHA256 checksum in
  #   the lockfile view
  def missing_checksum_dependencies
    dependencies.reject { |dependency| sha256_checksums.key?(dependency) }
  end

  # Indicates whether the original lockfile contained a +CHECKSUMS+
  # section.
  #
  # @return [Boolean] +true+ when the source lockfile had checksum metadata
  def checksums_present?
    checksums_section_present
  end
end

#checksums_section_presentBoolean (readonly)

Returns whether the source lockfile contained a +CHECKSUMS+ section.

Returns:

  • (Boolean)

    whether the source lockfile contained a +CHECKSUMS+ section



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/gem/guardian/cli.rb', line 29

LockfileDataView = Data.define(:dependencies, :checksums, :checksums_section_present) do
  # Looks up a checksum for a dependency and algorithm.
  #
  # @param dependency [Dependency] dependency to look up
  # @param algorithm [String] checksum algorithm name, currently usually
  #   +"sha256"+
  # @return [String, nil] checksum digest when present, otherwise +nil+
  def checksum_for(dependency, algorithm = "sha256")
    checksums.fetch(dependency, {}).fetch(algorithm, nil)
  end

  # Returns only SHA256 checksums from the filtered lockfile data.
  #
  # @return [Hash{Dependency => String}] selected dependencies mapped to
  #   their SHA256 digest
  def sha256_checksums
    checksums.each_with_object({}) do |(dependency, algorithms), memo|
      digest = algorithms["sha256"]
      memo[dependency] = digest if digest
    end
  end

  # Lists selected dependencies that do not have SHA256 lockfile coverage.
  #
  # @return [Array<Dependency>] dependencies missing a SHA256 checksum in
  #   the lockfile view
  def missing_checksum_dependencies
    dependencies.reject { |dependency| sha256_checksums.key?(dependency) }
  end

  # Indicates whether the original lockfile contained a +CHECKSUMS+
  # section.
  #
  # @return [Boolean] +true+ when the source lockfile had checksum metadata
  def checksums_present?
    checksums_section_present
  end
end

#dependenciesArray<Dependency> (readonly)

Returns dependencies selected for verification.

Returns:

  • (Array<Dependency>)

    dependencies selected for verification



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/gem/guardian/cli.rb', line 29

LockfileDataView = Data.define(:dependencies, :checksums, :checksums_section_present) do
  # Looks up a checksum for a dependency and algorithm.
  #
  # @param dependency [Dependency] dependency to look up
  # @param algorithm [String] checksum algorithm name, currently usually
  #   +"sha256"+
  # @return [String, nil] checksum digest when present, otherwise +nil+
  def checksum_for(dependency, algorithm = "sha256")
    checksums.fetch(dependency, {}).fetch(algorithm, nil)
  end

  # Returns only SHA256 checksums from the filtered lockfile data.
  #
  # @return [Hash{Dependency => String}] selected dependencies mapped to
  #   their SHA256 digest
  def sha256_checksums
    checksums.each_with_object({}) do |(dependency, algorithms), memo|
      digest = algorithms["sha256"]
      memo[dependency] = digest if digest
    end
  end

  # Lists selected dependencies that do not have SHA256 lockfile coverage.
  #
  # @return [Array<Dependency>] dependencies missing a SHA256 checksum in
  #   the lockfile view
  def missing_checksum_dependencies
    dependencies.reject { |dependency| sha256_checksums.key?(dependency) }
  end

  # Indicates whether the original lockfile contained a +CHECKSUMS+
  # section.
  #
  # @return [Boolean] +true+ when the source lockfile had checksum metadata
  def checksums_present?
    checksums_section_present
  end
end

Class Method Details

.start(argv) ⇒ Object

Starts the CLI with the provided argv.



69
70
71
# File 'lib/gem/guardian/cli.rb', line 69

def self.start(argv)
  new(argv).run
end

Instance Method Details

#runObject

Dispatches the requested subcommand and returns an exit status.



87
88
89
# File 'lib/gem/guardian/cli.rb', line 87

def run
  dispatch(@argv.shift)
end