Class: GDKBox::SSHConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/gdkbox/ssh_config.rb

Overview

Generates an SSH config fragment describing every box, and wires it into the user's ~/.ssh/config via an Include directive.

The fragment is regenerated wholesale from the current set of boxes on every change, which keeps it consistent and easy to reason about. Each box becomes a Host gdkbox-<name> entry that VS Code Remote-SSH and a plain ssh gdkbox-<name> can both use.

Constant Summary collapse

HEADER =
"# Managed by gdkbox - generated file, do not edit by hand"

Instance Method Summary collapse

Constructor Details

#initialize(config:) ⇒ SSHConfig

Returns a new instance of SSHConfig.



16
17
18
# File 'lib/gdkbox/ssh_config.rb', line 16

def initialize(config:)
  @config = config
end

Instance Method Details

#ensure_include!Object

Prepend an Include directive to ~/.ssh/config if it is not present.



43
44
45
46
47
48
49
50
51
# File 'lib/gdkbox/ssh_config.rb', line 43

def ensure_include!
  path = @config.user_ssh_config
  include_line = "Include #{@config.ssh_config_path}"
  existing = File.exist?(path) ? File.read(path) : ""
  return if existing.include?(include_line)

  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, "#{include_line}\n#{existing}")
end

#render(boxes) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gdkbox/ssh_config.rb', line 26

def render(boxes)
  lines = [HEADER, ""]
  boxes.sort_by { |box| box["name"] }.each do |box|
    lines << "Host #{@config.ssh_host_alias(box['name'])}"
    lines << "  HostName 127.0.0.1"
    lines << "  Port #{box['ssh_port']}"
    lines << "  User #{box['ssh_user']}"
    lines << "  IdentityFile #{@config.private_key_path}"
    lines << "  IdentitiesOnly yes"
    lines << "  StrictHostKeyChecking no"
    lines << "  UserKnownHostsFile /dev/null"
    lines << ""
  end
  "#{lines.join("\n").rstrip}\n"
end

#write(boxes) ⇒ Object



20
21
22
23
24
# File 'lib/gdkbox/ssh_config.rb', line 20

def write(boxes)
  @config.ensure_dirs!
  File.write(@config.ssh_config_path, render(boxes))
  ensure_include!
end