require 'sct/command_interface' module Sct class HostfileCommand IS_PUBLIC_COMMAND = true SYNTAX = 'sct hostfile' SUMMARY = 'adds the ingress url to the users hostfile' DESCRIPTION = "" EXAMPLE = "sct hostfile" EXAMPLE_DESCRIPTION = "" OPTIONS = [] def self.options end def execute(args, options) return puts "SCT has not been initialized. Run 'sct init' first.".red unless Sct::Config.exists return puts "This command needs to be run with sudo.".red unless Sct::Helpers.isSudo 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"] if Sct::Helpers.operatingSystem == Sct::Helpers::WINDOWS 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" 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 << "#{ingressAddress} #{entry[:host]} # #{entry[:comment]}#{line_ending}" end File.write hosts_path, lines.join puts "Patched #{hosts_path} with #{ingressAddress}".green end end implements CommandInterface end end