Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevents duplicate host file records in Windows #264

Merged
merged 1 commit into from
May 18, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Move newlines out of quoted header and footer. Match carriage return …
…along with newline in hosts file pattern.
  • Loading branch information
jalandis committed May 17, 2018
commit 7e434ecbb4c136cbb0ba3a1e57c3c4e087873b1f
8 changes: 4 additions & 4 deletions lib/vagrant-hostmanager/hosts_file/updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ def update_file(file, resolving_machine = nil, include_id = true, line_endings)

def update_content(file_content, resolving_machine, include_id, line_endings)
id = include_id ? " id: #{read_or_create_id}" : ""
header = "## vagrant-hostmanager-start#{id}\n"
footer = "## vagrant-hostmanager-end\n"
header = "## vagrant-hostmanager-start#{id}"
footer = "## vagrant-hostmanager-end"
body = get_machines
.map { |machine| get_hosts_file_entry(machine, resolving_machine) }
.join
Expand Down Expand Up @@ -146,12 +146,12 @@ def get_new_content(header, footer, body, old_content, line_endings)
if body.empty?
block = "\n"
else
block = "\n\n" + header + body + footer + "\n"
block = "\n\n" + header + "\n" + body + footer + "\n\n"
end
# Pattern for finding existing block
header_pattern = Regexp.quote(header)
footer_pattern = Regexp.quote(footer)
pattern = Regexp.new("\n*#{header_pattern}.*?#{footer_pattern}\n*", Regexp::MULTILINE)
pattern = Regexp.new("[\r\n]*#{header_pattern}.*?#{footer_pattern}[\r\n]*", Regexp::MULTILINE)
# Replace existing block or append
content = old_content.match(pattern) ? old_content.sub(pattern, block) : old_content.rstrip + block
if line_endings == "crlf"
Expand Down