# frozen_string_literal: true

# Custom resource based on the InSpec resource DSL
class GpgKey < Inspec.resource(1)
  name 'gpg_key'
  desc 'check whether a named gpg key has been imported'
  supports os: 'linux'

  example "
    describe gpg_key('security@hashicorp.com') do
      it { should be_imported }
    end
  "

  def initialize(key_or_name)
    @key_or_name = key_or_name
  end

  def imported?
    keys = inspec.backend.run_command("sudo -u root -i gpg2 --list-keys \"#{@key_or_name}\"")
    keys = keys.stdout.split("\n")
    !keys.empty? && keys.any? { |h| h.match?(/HashiCorp/) }
  end

  def to_s
    "GPG Key Imported: #{@key_or_name}"
  end
end
