Class: VagrantPlugins::SshConfigManager::IncludeManager
- Inherits:
-
Object
- Object
- VagrantPlugins::SshConfigManager::IncludeManager
- Defined in:
- lib/vagrant-ssh-config-manager/include_manager.rb
Constant Summary collapse
- PLUGIN_MARKER_START =
"# BEGIN vagrant-ssh-config-manager"- PLUGIN_MARKER_END =
"# END vagrant-ssh-config-manager"
Instance Method Summary collapse
-
#add_include_directive ⇒ Object
Add Include directive to main SSH config file.
-
#find_include_location(content) ⇒ Object
Parse SSH config file and find optimal location for Include.
-
#handle_empty_main_config ⇒ Object
Handle edge case: empty main config file.
-
#include_directive_exists? ⇒ Boolean
Check if Include directive exists in main SSH config.
-
#initialize(config) ⇒ IncludeManager
constructor
Initialize IncludeManager with configuration.
-
#main_config_writable? ⇒ Boolean
Check if main SSH config file is write-protected.
-
#manage_include_directive ⇒ Object
Manage Include directive based on current state.
-
#remove_include_directive ⇒ Object
Remove Include directive from main SSH config file.
-
#should_remove_include_directive? ⇒ Boolean
Check if Include directive should be removed (no config files exist).
-
#ssh_config_file ⇒ Object
Get the SSH config file path (always ~/.ssh/config).
-
#validate_main_config ⇒ Object
Validate SSH config file format.
Constructor Details
#initialize(config) ⇒ IncludeManager
Initialize IncludeManager with configuration
12 13 14 15 |
# File 'lib/vagrant-ssh-config-manager/include_manager.rb', line 12 def initialize(config) @config = config @logger = Log4r::Logger.new("vagrant::plugins::sshconfigmanager::includemanager") end |
Instance Method Details
#add_include_directive ⇒ Object
Add Include directive to main SSH config file
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/vagrant-ssh-config-manager/include_manager.rb', line 32 def add_include_directive return false unless @config.manage_includes return true if include_directive_exists? begin # Create backup before modifying create_backup # Add Include directive at the beginning of file add_include_to_config @logger.info("Added Include directive for #{@config.ssh_config_dir} to ~/.ssh/config") true rescue => e @logger.error("Failed to add Include directive: #{e.message}") restore_backup false end end |
#find_include_location(content) ⇒ Object
Parse SSH config file and find optimal location for Include
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/vagrant-ssh-config-manager/include_manager.rb', line 96 def find_include_location(content) lines = content.lines # Look for existing Include directives to place ours with them include_line_index = lines.find_index { |line| line.strip.start_with?('Include ') } if include_line_index # Place before other Include directives include_line_index else # Place at the beginning of file, after any initial comments comment_end = 0 lines.each_with_index do |line, index| if line.strip.empty? || line.strip.start_with?('#') comment_end = index + 1 else break end end comment_end end end |
#handle_empty_main_config ⇒ Object
Handle edge case: empty main config file
126 127 128 129 130 131 132 133 134 |
# File 'lib/vagrant-ssh-config-manager/include_manager.rb', line 126 def handle_empty_main_config unless File.exist?(ssh_config_file) # Create empty SSH config file with proper permissions FileUtils.mkdir_p(File.dirname(ssh_config_file), mode: 0700) File.write(ssh_config_file, "") File.chmod(0600, ssh_config_file) @logger.info("Created empty SSH config file: #{ssh_config_file}") end end |
#include_directive_exists? ⇒ Boolean
Check if Include directive exists in main SSH config
23 24 25 26 27 28 29 |
# File 'lib/vagrant-ssh-config-manager/include_manager.rb', line 23 def include_directive_exists? return false unless File.exist?(ssh_config_file) content = File.read(ssh_config_file) include_pattern = /^Include\s+#{Regexp.escape(@config.ssh_config_dir)}/ content.match?(include_pattern) end |
#main_config_writable? ⇒ Boolean
Check if main SSH config file is write-protected
120 121 122 123 |
# File 'lib/vagrant-ssh-config-manager/include_manager.rb', line 120 def main_config_writable? return false unless File.exist?(ssh_config_file) File.writable?(ssh_config_file) end |
#manage_include_directive ⇒ Object
Manage Include directive based on current state
85 86 87 88 89 90 91 92 93 |
# File 'lib/vagrant-ssh-config-manager/include_manager.rb', line 85 def manage_include_directive return unless @config.manage_includes if should_remove_include_directive? remove_include_directive elsif Dir.exist?(@config.ssh_config_dir) && !Dir.entries(@config.ssh_config_dir).select { |f| f.end_with?('.conf') }.empty? add_include_directive end end |
#remove_include_directive ⇒ Object
Remove Include directive from main SSH config file
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/vagrant-ssh-config-manager/include_manager.rb', line 53 def remove_include_directive return false unless @config.manage_includes return true unless include_directive_exists? begin # Create backup before modifying create_backup # Remove Include directive remove_include_from_config @logger.info("Removed Include directive for #{@config.ssh_config_dir} from ~/.ssh/config") true rescue => e @logger.error("Failed to remove Include directive: #{e.message}") restore_backup false end end |
#should_remove_include_directive? ⇒ Boolean
Check if Include directive should be removed (no config files exist)
74 75 76 77 78 79 80 81 82 |
# File 'lib/vagrant-ssh-config-manager/include_manager.rb', line 74 def should_remove_include_directive? return false unless @config.cleanup_empty_dir return false unless Dir.exist?(@config.ssh_config_dir) # Check if directory is empty (excluding . and ..) entries = Dir.entries(@config.ssh_config_dir) - %w[. ..] config_files = entries.select { |file| file.end_with?('.conf') } config_files.empty? end |
#ssh_config_file ⇒ Object
Get the SSH config file path (always ~/.ssh/config)
18 19 20 |
# File 'lib/vagrant-ssh-config-manager/include_manager.rb', line 18 def ssh_config_file @ssh_config_file ||= File.("~/.ssh/config") end |
#validate_main_config ⇒ Object
Validate SSH config file format
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/vagrant-ssh-config-manager/include_manager.rb', line 137 def validate_main_config return true unless File.exist?(ssh_config_file) begin content = File.read(ssh_config_file) # Basic validation - check for severely malformed config lines = content.lines lines.each_with_index do |line, index| stripped = line.strip next if stripped.empty? || stripped.start_with?('#') # Check for basic SSH config format issues if stripped.include?("\t") && stripped.start_with?("Host ") @logger.warn("Potential SSH config format issue at line #{index + 1}: tabs in Host directive") end end true rescue => e @logger.error("SSH config validation failed: #{e.message}") false end end |