Sha256: d91f20c248aa4eebd2245241d7962e70b288f129af06d6682fb8a00509d478ec

Contents?: true

Size: 1.83 KB

Versions: 5

Compression:

Stored size: 1.83 KB

Contents

module Sct
  class HostfileCommand

    def execute(args, options)
      return UI.error("SCT has not been initialized. Run 'sct init' first.") unless SctCore::Config.exists

      return UI.error("This command needs to be run with sudo.") unless SctCore::Helper.isSudo

      return unless SctCore::Helper.ingressAddress
      
      ingressAddress = SctCore::Helper.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 SctCore::Helper.operatingSystem == SctCore::Helper::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

        UI.success("Patched #{hosts_path} with #{ingressAddress}")
      end

    end

  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sct-0.1.23 sct/lib/sct/commands/hostfile.rb
sct-0.1.22 sct/lib/sct/commands/hostfile.rb
sct-0.1.21 sct/lib/sct/commands/hostfile.rb
sct-0.1.20 sct/lib/sct/commands/hostfile.rb
sct-0.1.19 sct/lib/sct/commands/hostfile.rb