lib/sct/commands/hostfile.rb in sct-0.1.11 vs lib/sct/commands/hostfile.rb in sct-0.1.12

- old
+ new

@@ -1,5 +1,6 @@ +require 'hosts' require 'sct/command_interface' module Sct class HostfileCommand @@ -14,38 +15,40 @@ def self.options end def execute(args, options) - return puts "SCT has not been initialized. Run 'sct init' first.".red unless Sct::Config.exists - require 'hosts' + return puts "This command needs to be run with sudo.".red unless Sct::Helpers.isSudo - return unless ingressUrl + return unless Sct::Helpers.ingressAddress - url = ingressUrl - host_path = Sct::Config.get("host-path") + ingressAddress = Sct::Helpers.ingressAddress - if !options.path.nil? - hosts = Hosts::File.read(options.path) - elsif host_path - hosts = Hosts::File.read(host_path) + if options.path + hosts_paths = [options.path] else - puts "No hostpath found in config file. Using default /etc/hosts".yellow - hosts = Hosts::File.read('/etc/hosts') - end + hosts_paths = ["/etc/hosts"] - if ([url].any? { |ip| hosts.to_s =~ /#{ip}/ }) - puts "Already exist and skipped.".green - else - validate_if_exist(hosts) - add_entry(url, hosts) - hosts.write - puts "patched: #{url}".green + if Sct::Helpers.operatingSystem == Sct::Helpers::WINDOWS + hosts_paths << "/mnt/c/Windows/System32/drivers/etc/hosts" + end end + hosts_paths.each do |hosts_path| + hosts = Hosts::File.read(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 + end end def add_entry(url, hosts) spend = Hosts::Entry.new( url, @@ -96,13 +99,9 @@ end array.elements.delete_if.each_with_index { |value, index| rejectArray.include? index } - end - - def ingressUrl - return Sct::Helpers.ingressUrl end implements CommandInterface end