<commit_before>require_relative 'services_helper'

module Kontena::Cli::Services
  class ListCommand < Kontena::Command
    include Kontena::Cli::Common
    include Kontena::Cli::GridOptions
    include ServicesHelper

    def execute
      require_api_url
      token = require_token

      grids = client(token).get("grids/#{current_grid}/services")
      services = grids['services'].sort_by{|s| s['updated_at'] }.reverse
      titles = ['NAME', 'INSTANCES', 'STATEFUL', 'STATE', 'EXPOSED PORTS']
      puts "%-60s %-10s %-8s %-10s %-50s" % titles
      services.each do |service|
        stateful = service['stateful'] ? 'yes' : 'no'
        running = service['instances']['running']
        desired = service['container_count']
        instances = "#{running} / #{desired}"
        ports = service['ports'].map{|p|
          "#{p['ip']}:#{p['node_port']}->#{p['container_port']}/#{p['protocol']}"
        }.join(", ")
        health = health_status(service)
        vars = [
          health_status_icon(health),
          "#{service['name']}",
          instances,
          stateful,
          service['state'],
          ports
        ]
        puts "%s %-58s %-10.10s %-8s %-10s %-50s" % vars
      end
    end
  end
end
<commit_msg>Add quiet option to service list command (#1312)<commit_after>require_relative 'services_helper'

module Kontena::Cli::Services
  class ListCommand < Kontena::Command
    include Kontena::Cli::Common
    include Kontena::Cli::GridOptions
    include ServicesHelper

    option ["-q", "--quiet"], :flag, "Show only service names"

    def execute
      require_api_url
      token = require_token

      grids = client(token).get("grids/#{current_grid}/services")
      services = grids['services'].sort_by{|s| s['updated_at'] }.reverse
      if quiet?
        services.each do |service|
          puts service['name']
        end
      else
        titles = ['NAME', 'INSTANCES', 'STATEFUL', 'STATE', 'EXPOSED PORTS']
        puts "%-60s %-10s %-8s %-10s %-50s" % titles
        services.each do |service|
          print_service_row(service)
        end
      end
    end

    def print_service_row(service)
      stateful = service['stateful'] ? 'yes' : 'no'
      running = service['instances']['running']
      desired = service['container_count']
      instances = "#{running} / #{desired}"
      ports = service['ports'].map{|p|
        "#{p['ip']}:#{p['node_port']}->#{p['container_port']}/#{p['protocol']}"
      }.join(", ")
      health = health_status(service)
      vars = [
        health_status_icon(health),
        "#{service['name']}",
        instances,
        stateful,
        service['state'],
        ports
      ]
      puts "%s %-58s %-10.10s %-8s %-10s %-50s" % vars
    end
  end
end

{'fraction_non_alphanumeric': 0.1620050853614239, 'fraction_numerical': 0.015982564475118054, 'mean_word_length': 2.6047120418848166, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5890411', 'n_tokens_mistral': 892, 'n_tokens_neox': 881, 'n_words': 183}