sct/lib/sct/commands/hostfile.rb in sct-0.1.28 vs sct/lib/sct/commands/hostfile.rb in sct-0.1.29

- old
+ new

@@ -1,17 +1,11 @@ module Sct class HostfileCommand def execute(args, options) - return UI.error("SCT has not been initialized. Run 'sct init' first.") unless SctCore::Config.exists + SctCore::Helper.ensure_permissions - return UI.error("This command needs to be run with sudo.") unless SctCore::Helper.isSudo - - return unless SctCore::Helper.ingressAddress - - ingressAddress = SctCore::Helper.ingressAddress - entries = [ { host: "spend.cloud.local", comment: "The spend cloud ingress url" }, @@ -35,35 +29,45 @@ host: "henk.spend.cloud.local", comment: "The spend cloud Henk web-interface" } ] + is_windows = SctCore::Helper::is_windows? + + if is_windows + ingress_address = "127.0.0.1" + else + ingress_address = `sudo -u $SUDO_USER minikube ip`.chomp + end + + windows_hosts_path = "/mnt/c/Windows/System32/drivers/etc/hosts" + if options.path hosts_paths = [options.path] else hosts_paths = ["/etc/hosts"] - if SctCore::Helper.operatingSystem == SctCore::Helper::WINDOWS - hosts_paths << "/mnt/c/Windows/System32/drivers/etc/hosts" + if is_windows + hosts_paths << windows_hosts_path end end hosts_paths.each do |hosts_path| - line_ending = hosts_path == "/mnt/c/Windows/System32/drivers/etc/hosts" ? "\r\n" : "\n" + line_ending = hosts_path == windows_hosts_path ? "\r\n" : "\n" lines = File.readlines hosts_path # select the lines that do not include any entry - lines = lines.select { |line| ! entries.any? { |entry| line.include? entry[:host] } } + lines = lines.select { |line| ! entries.any? { |entry| line.include? " #{entry[:host]}" } } # add entries entries.each do |entry| - lines << "#{ingressAddress} #{entry[:host]} # #{entry[:comment]}#{line_ending}" + lines << "#{ingress_address} #{entry[:host]} # #{entry[:comment]}#{line_ending}" end File.write hosts_path, lines.join - UI.success("Patched #{hosts_path} with #{ingressAddress}") + UI.success("Patched #{hosts_path} with #{ingress_address}") end end end