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 --path=/mnt/c/Windows/System32/drivers/etc/hosts" EXAMPLE_DESCRIPTION = "" OPTIONS = [] def initialize end def self.options end def execute(args, options) require 'hosts' return unless ingressUrl url = ingressUrl if !options.path.nil? hosts = Hosts::File.read(options.path) elsif setup && sct_file["hostpath"] hosts = Hosts::File.read("#{sct_file["hostpath"]}") else puts "No hostpath found in Sctfile using default /etc/hosts".yellow hosts = Hosts::File.read('/etc/hosts') end 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 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) end end array.elements.delete_if.each_with_index { |value, index| rejectArray.include? index } end def ingressUrl return Sct::Helpers.ingressUrl end def setup return false unless Sct::SctFolder.setup? return true end def sct_file return unless setup sct_file_string = File.read(Sct::SctFolder.sctfile_path) sct_file_hash = Sct::Helpers.to_hash(sct_file_string) return sct_file_hash end implements CommandInterface end end