module Sct class HostfileCommand def execute(args, options) SctCore::Helper.ensure_permissions 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" }, { 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 is_windows hosts_paths << windows_hosts_path end end hosts_paths.each do |hosts_path| 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]}" } } # add entries entries.each do |entry| lines << "#{ingress_address} #{entry[:host]} # #{entry[:comment]}#{line_ending}" end File.write hosts_path, lines.join UI.success("Patched #{hosts_path} with #{ingress_address}") end end end end