Class: Sconb::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/sconb/cli.rb

Instance Method Summary collapse

Instance Method Details

#dump(regexp_str = '.*') ⇒ Object



17
18
19
20
# File 'lib/sconb/cli.rb', line 17

def dump(regexp_str = '.*')
  path = options[:config]
  puts JSON.pretty_generate(Sconb::SSHConfig.load(path, regexp_str, options))
end

#keyregenObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sconb/cli.rb', line 56

def keyregen
  json = stdin_read
  configs = JSON.parse(json)
  configs.each do |_host, config|
    config.each do |key, value|
      next unless key.downcase == 'identityfilecontent'
      identity_files = config['IdentityFile']
      value.each_with_index do |keycontent, i|
        identity_file = File.expand_path(identity_files[i])
        if File.exist?(identity_file) && !options[:force]
          raise Thor::Error, "Error: #{identity_files[i]} is exists. If you want to overwrite, use --force option."
        end
        puts "Regenerate #{identity_files[i]} ..."
        File.open(identity_file, 'w') do |file|
          file.write keycontent
        end
        File.chmod(0600, identity_file)
      end
    end
  end
end

#restoreObject



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
# File 'lib/sconb/cli.rb', line 23

def restore
  ssh_configs = []
  json = stdin_read
  configs = JSON.parse(json)
  configs.each do |host, config|
    ssh_config = ''
    header = if host !~ /^Match /
               "Host #{host}\n"
             else
               "#{host}\n"
             end
    ssh_config << header
    config.each do |key, value|
      next if key.downcase == 'host' || key.downcase == 'match' || key.downcase == 'identityfilecontent'
      if key.downcase == 'identityfile'
        value.each_with_index do |keyfile, _i|
          ssh_config << "  #{key} #{keyfile}\n"
        end
      else
        ssh_config << "  #{key} #{value}\n"
      end
    end
    ssh_configs.push ssh_config
  end
  puts ssh_configs.join("\n")
end