1# frozen_string_literal: true
2
3module QA
4  module Runtime
5    class GPG
6      attr_reader :key, :key_id
7
8      def initialize
9        @key_id = 'B8358D73048DACC4'
10        import_key(File.expand_path('qa/ee/fixtures/gpg/admin.asc'))
11        @key = collect_key.first
12      end
13
14      private
15
16      def import_key(path)
17        import_key = "gpg --import #{path}"
18        execute(import_key)
19      end
20
21      def collect_key
22        get_ascii_format = "gpg --armor --export #{@key_id}"
23        execute(get_ascii_format)
24      end
25
26      def execute(command)
27        Open3.capture2e(*command) do |stdin, out, wait|
28          out.each_char { |char| print char }
29
30          if wait.value.exited? && wait.value.exitstatus.nonzero?
31            raise CommandError, "Command `#{command}` failed!"
32          end
33        end
34      end
35    end
36  end
37end
38