Sha256: a75348522cebdbb9aa0d250a5a768aebc10ca4d8436dc3a060c1c3f7a22fcf27

Contents?: true

Size: 1.9 KB

Versions: 4

Compression:

Stored size: 1.9 KB

Contents

module SctCore
  class Helper

    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.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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sct-0.1.22 sct_core/lib/sct_core/helper.rb
sct-0.1.21 sct_core/lib/sct_core/helper.rb
sct-0.1.20 sct_core/lib/sct_core/helper.rb
sct-0.1.19 sct_core/lib/sct_core/helper.rb