Class: Deploio::Commands::Services

Inherits:
Thor
  • Object
show all
Includes:
SharedOptions
Defined in:
lib/deploio/commands/services.rb

Instance Method Summary collapse

Methods included from SharedOptions

included

Instance Method Details

#listObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/deploio/commands/services.rb', line 25

def list
  setup_options

  validate_project_required_options!

  project = merged_options[:project] ? resolve_project(merged_options[:project]) : nil
  all_services = @nctl.get_all_services(project: project)

  if options[:json]
    puts JSON.pretty_generate(all_services)
    return
  end

  if all_services.empty?
    msg = project ? "No services found in project #{merged_options[:project]}" : "No services found"
    Output.warning(msg) unless merged_options[:dry_run]
    return
  end

  show_url = merged_options[:url]
  show_connected_apps = merged_options[:connected_apps]
  show_price = merged_options[:chf]
  current_org = @nctl.current_org

  # Pre-fetch apps and their env vars if we need to show connected apps
  apps_by_project = {}
  if show_connected_apps
    all_services.map { |s| s.dig("metadata", "namespace") }.uniq.each do |ns|
      apps_by_project[ns] = @nctl.get_apps_by_project(ns)
    end
  end

  # Initialize price fetcher if needed
  price_fetcher = PriceFetcher.new if show_price

  # Build rows and group by project
  grouped_rows = {}
  all_services.each do |service|
     = service["metadata"] || {}
    status = service["status"] || {}
    spec = service["spec"] || {}
    conditions = status["conditions"] || []
    ready_condition = conditions.find { |c| c["type"] == "Ready" }

    namespace = ["namespace"] || ""
    name = ["name"] || ""
    type = service["_type"] || "-"
    project_name = project_from_namespace(namespace, current_org)

    row = [
      short_service_name(namespace, name, current_org),
      project_name,
      type,
      presence(ready_condition&.dig("status"))
    ]

    if show_price
      price = price_fetcher.price_for_service(type, spec)
      row << price_fetcher.format_price(price)
    end

    # Get URL if needed (for display or for connected apps search)
    url = nil
    if show_url || show_connected_apps
      url = @nctl.get_service_connection_string(type, name, project: namespace)
    end

    row << presence(url) if show_url

    if show_connected_apps
      connected = find_connected_apps(apps_by_project[namespace] || [], url)
      row << (connected.empty? ? "-" : connected.join(", "))
    end

    grouped_rows[project_name] ||= []
    grouped_rows[project_name] << row
  end

  # Sort groups by project name
  sorted_groups = grouped_rows.sort.to_h

  headers = %w[SERVICE PROJECT TYPE READY]
  headers << "PRICE" if show_price
  headers << "URL" if show_url
  headers << "CONNECTED APPS" if show_connected_apps
  Output.grouped_table(sorted_groups, headers: headers)
end