Sha256: 0cf20ef90aa0b2f2fac9be76a8d93b9557c097c39ca4ac0a53af82ab96ea59c0

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

require 'text-table'
module MKIt
  module ServicesHelper
    def format_response(data, _verbose = false)
      table = Text::Table.new
      table.head = %w[id name addr ports pods status]
      if data.respond_to? 'each'
        data.each do |srv|
          table.rows << build_table_row(srv)
        end
      else
        table.rows << build_table_row(data)
      end
      table.to_s
    end

    def find_by_id_or_name
      srv = Service.find_by_id(params[:id])
      srv ||= Service.find_by_name(params[:id])
      error 404, "Couldn't find Service '#{params[:id]}'\n" unless srv
      srv
    end

    def find_srv_pod_by_id_or_name(srv)
      if params[:pod_id]
        pod = srv.find_pod_by_id_or_name(params[:pod_id])
        error 404, "Service pod not found." unless pod
      else
        pod = srv.pod.first
      end
      pod
    end

    def build_table_row(data)
      ports = data.service_port&.each.map { |p| build_port(p) }.join(',')
      pods = data.pod.each.map { |p| p.name.to_s }.join(' ')
      [data.id, data.name, data.lease&.ip, ports, pods, data.status]
    end

    def build_port(p)
      case p.mode
      when 'http'
        if p.ssl?
          "#{p.mode}s/#{p.external_port}"
        else
          "#{p.mode}/#{p.external_port}"
        end
      when 'tcp'
        if p.ssl?
          "s#{p.mode}/#{p.external_port}"
        else
          "#{p.mode}/#{p.external_port}"
        end
      else
        "#{p.mode}/#{p.external_port}"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mkit-0.9.0 lib/mkit/app/helpers/services_helper.rb
mkit-0.8.0 lib/mkit/app/helpers/services_helper.rb