Sha256: 61e7c442d2bbe739c4633e871f28cc34b38810dc1f1e8e538e49897ab7d8f670

Contents?: true

Size: 1.89 KB

Versions: 7

Compression:

Stored size: 1.89 KB

Contents

module Sct
  class Helpers

    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

7 entries across 7 versions & 1 rubygems

Version Path
sct-0.1.18 lib/sct/setup/helpers.rb
sct-0.1.17 lib/sct/setup/helpers.rb
sct-0.1.16 lib/sct/setup/helpers.rb
sct-0.1.15 lib/sct/setup/helpers.rb
sct-0.1.14 lib/sct/setup/helpers.rb
sct-0.1.13 lib/sct/setup/helpers.rb
sct-0.1.12 lib/sct/setup/helpers.rb