Class: ICSP::Commands::Certificate::List

Inherits:
BaseCommand show all
Defined in:
lib/commands/certificate/list.rb

Instance Attribute Summary

Attributes inherited from BaseCommand

#arguments, #config, #options, #prompt

Instance Method Summary collapse

Methods inherited from BaseCommand

#initialize

Constructor Details

This class inherits a constructor from ICSP::Commands::BaseCommand

Instance Method Details

#available_storesObject



65
66
67
# File 'lib/commands/certificate/list.rb', line 65

def available_stores
  %w[uMy root ca]
end

#certmgrObject



7
8
9
# File 'lib/commands/certificate/list.rb', line 7

def certmgr
  @certmgr ||= @config.certmgr
end

#executeObject



11
12
13
14
15
16
17
# File 'lib/commands/certificate/list.rb', line 11

def execute
  store = prompt.select('Select store:', available_stores)
  result = ::ICSP::Shell.new("#{certmgr} -list -store #{store}", convert_to_utf8: false).execute
  exit(result.exit_code) unless result.ok

  result
end


61
62
63
# File 'lib/commands/certificate/list.rb', line 61

def print
  puts execute
end

#selectObject



19
20
21
22
23
24
25
26
27
28
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
# File 'lib/commands/certificate/list.rb', line 19

def select
  list_divider = '============================================================================='
  i = 1
  certificate_hash = {}
  certificates = []
  divider_count = 0
  execute.output_lines.each do |line|
    index_line = "#{i}-------"
    next_index_line = "#{i + 1}-------"
    if line == index_line || next_index_line || line == list_divider
      if line == "#{i}-------"
        certificate_hash = {}
        certificate_hash['index'] = i
        next
      end

      if line == "#{i + 1}-------"
        certificates << OpenStruct.new(certificate_hash.dup)
        i += 1
        certificate_hash = {}
        certificate_hash['index'] = i
      end

      if line == list_divider
        divider_count += 1
        break if divider_count == 2
      end
    end

    next unless certificate_hash['index']

    attribute = line.split(' : ').map(&:strip)
    key = attribute.first
    value = attribute[1..].join(' : ')

    certificate_hash[key] = value if key
  end

  choices = certificates.map { |certificate| [certificate['Subject'], certificate['SHA1 Hash']] }.to_h
  @prompt.select('Choose certificate:', choices)
end