lib/sct/setup/helpers.rb in sct-0.1.11 vs lib/sct/setup/helpers.rb in sct-0.1.12

- old
+ new

@@ -1,22 +1,90 @@ module Sct class Helpers - def self.ingressUrl - ip = `kubectl get ingress` - - if ip.nil? || ip.empty? - puts "Can't fetch ip from kubectl".yellow + WINDOWS = "Windows" + MAC_OS = "MacOS" + UBUNTU = "Ubuntu" + + def self.ingressAddress + if self.operatingSystem == WINDOWS + kubeconfig_file = "minikube-config" + else + kubeconfig_file = "config" + end + + kubeconfig_path= "#{self.homePath}/.kube/#{kubeconfig_file}" + + ip = `KUBECONFIG="#{kubeconfig_path}" kubectl get ingress` + + if ip.nil? || ip.empty? + puts "Can't fetch IP from kubectl".yellow return nil end - - match = ip.scan(/((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3})/); - return match[0].first + + match = ip.scan(/((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3})/) + + return match.first.first end def self.to_hash(str) Hash[ str.split("\n").map{|i|i.split('=')} ] end + + def self.operatingSystem + proc = `uname -a` + + case proc + when /Microsoft/ + os = WINDOWS + when /Darwin/ + os = MAC_OS + else + os = UBUNTU + end + + return os + end + + def self.homePath + user = ENV["SUDO_USER"] || ENV["USER"] + + if self.operatingSystem == MAC_OS + home = "/Users" + else + home = "/home" + end + + return "#{home}/#{user}" + end + + def self.windowsHomePath + if self.operatingSystem == WINDOWS + return self.convertWindowsToWSLPath(`cmd.exe /c echo %userprofile%`) + end + + return nil + end + + def self.convertWindowsToWSLPath(path) + if self.operatingSystem == WINDOWS + return path.gsub(/C:\\/, '/mnt/c/').gsub(/\\\\/, "/").gsub(/\\/, '/').gsub(/\r\n?/, '') + end + + return path + end + + def self.convertWSLToWindowsPath(path) + if self.operatingSystem == WINDOWS + return path.gsub(/\/mnt\/c/, 'C:/').gsub(/\/\//, '/').gsub(/\\\\/, "/").gsub(/\r\n?/, '') + end + + return path + end + + def self.isSudo + return !ENV["SUDO_USER"].nil? && !ENV["SUDO_USER"].empty? + end end -end \ No newline at end of file +end