lib/sct/commands/hostfile.rb in sct-0.1.15 vs lib/sct/commands/hostfile.rb in sct-0.1.16
- old
+ new
@@ -1,6 +1,5 @@
-require 'hosts'
require 'sct/command_interface'
module Sct
class HostfileCommand
@@ -23,10 +22,33 @@
return unless Sct::Helpers.ingressAddress
ingressAddress = Sct::Helpers.ingressAddress
+ entries = [
+ {
+ host: "spend.cloud.local",
+ comment: "The spend cloud ingress url"
+ },
+ {
+ host: "mail.spend.cloud.local",
+ comment: "The spend cloud mail url"
+ },
+ {
+ host: "config.spend.cloud.local",
+ comment: "The spend cloud config url"
+ },
+ {
+ host: "spend-cloud.spend.cloud.local",
+ comment: "The spend cloud web app url"
+ },
+ {
+ host: "docs.spend.cloud.local",
+ comment: "The spend cloud documentation url"
+ }
+ ]
+
if options.path
hosts_paths = [options.path]
else
hosts_paths = ["/etc/hosts"]
@@ -34,88 +56,25 @@
hosts_paths << "/mnt/c/Windows/System32/drivers/etc/hosts"
end
end
hosts_paths.each do |hosts_path|
+ line_ending = hosts_path == "/mnt/c/Windows/System32/drivers/etc/hosts" ? "\r\n" : "\n"
- begin
- hosts = Hosts::File.read(hosts_path)
+ lines = File.readlines hosts_path
- if ([ingressAddress].any? { |ip| hosts.to_s =~ /#{ip}/ })
- puts "Skipped patching #{hosts_path} (already up to date)".green
- else
- validate_if_exist(hosts)
- add_entry(ingressAddress, hosts)
- hosts.write
- puts "Patched #{hosts_path} with #{ingressAddress}".green
- end
- rescue ArgumentError
- puts "🔥 Can't read your hostfile".red
- puts " Please update your hostsfile manually at #{hosts_path} with the following entries"
- puts " "
- puts " #{ingressAddress} spend.cloud.local".yellow
- puts " #{ingressAddress} mail.spend.cloud.local".yellow
- puts " #{ingressAddress} config.spend.cloud.local".yellow
- puts " #{ingressAddress} spend-cloud.spend.cloud.local".yellow
- puts " #{ingressAddress} docs.spend.cloud.local".yellow
- puts " "
- end
+ # select the lines that do not include any entry
+ lines = lines.select { |line| ! entries.any? { |entry| line.include? entry[:host] } }
- end
-
- end
-
- def add_entry(url, hosts)
- spend = Hosts::Entry.new(
- url,
- 'spend.cloud.local',
- :comment => 'The spend cloud ingress url'
- )
-
- mail = Hosts::Entry.new(
- url,
- 'mail.spend.cloud.local',
- :comment => 'The spend cloud mail url'
- )
-
- config = Hosts::Entry.new(
- url,
- 'config.spend.cloud.local',
- :comment => 'The spend cloud config url'
- )
-
- spend_cloud = Hosts::Entry.new(
- url,
- 'spend-cloud.spend.cloud.local',
- :comment => 'The spend cloud web app url'
- )
-
- docs = Hosts::Entry.new(
- url,
- 'docs.spend.cloud.local',
- :comment => 'The spend cloud documentation url'
- )
-
- hosts.elements << spend
- hosts.elements << mail
- hosts.elements << config
- hosts.elements << spend_cloud
- hosts.elements << docs
-
- end
-
- def validate_if_exist(array)
- rejectArray = Array.new
-
- array.to_s.each_line.with_index do |line, index|
-
- if (line =~ /(?:spend\.cloud\.local)./)
- rejectArray.push(index)
+ # add entries
+ entries.each do |entry|
+ lines << "#{ingressAddress} #{entry[:host]} # #{entry[:comment]}#{line_ending}"
end
- end
+ File.write hosts_path, lines.join
- array.elements.delete_if.each_with_index { |value, index| rejectArray.include? index }
+ puts "Patched #{hosts_path} with #{ingressAddress}".green
+ end
end
implements CommandInterface