summaryrefslogtreecommitdiff
path: root/qa/qa/runtime/gpg.rb
blob: 9f8baf7e5807dbfcc94f3247f1c16a8142d518b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# frozen_string_literal: true

module QA
  module Runtime
    class GPG
      attr_reader :key, :key_id

      def initialize
        @key_id = 'B8358D73048DACC4'
        import_key(File.expand_path('qa/ee/fixtures/gpg/admin.asc'))
        @key = collect_key.first
      end

      private

      def import_key(path)
        import_key = "gpg --import #{path}"
        execute(import_key)
      end

      def collect_key
        get_ascii_format = "gpg --armor --export #{@key_id}"
        execute(get_ascii_format)
      end

      def execute(command)
        Open3.capture2e(*command) do |stdin, out, wait|
          out.each_char { |char| print char }

          if wait.value.exited? && wait.value.exitstatus.nonzero?
            raise CommandError, "Command `#{command}` failed!"
          end
        end
      end
    end
  end
end