Class: LanguageOperator::CLI::Commands::Use

Inherits:
BaseCommand
  • Object
show all
Includes:
Helpers::UxHelper
Defined in:
lib/language_operator/cli/commands/use.rb

Overview

Switch cluster context command

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::UxHelper

#box, #highlight_ruby_code, #logo, #pastel, #prompt, #spinner, #table

Class Method Details

.exit_on_failure?Boolean

Returns:



17
18
19
# File 'lib/language_operator/cli/commands/use.rb', line 17

def self.exit_on_failure?
  true
end

Instance Method Details

#switch(cluster_name) ⇒ Object



21
22
23
24
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
# File 'lib/language_operator/cli/commands/use.rb', line 21

def switch(cluster_name)
  handle_command_error('switch cluster') do
    # First check if cluster exists in local config
    if Config::ClusterConfig.cluster_exists?(cluster_name)
      Config::ClusterConfig.set_current_cluster(cluster_name)
      cluster = Config::ClusterConfig.get_cluster(cluster_name)
    else
      # If not in local config, check if it exists in Kubernetes
      k8s_cluster = find_cluster_in_kubernetes(cluster_name)
      
      if k8s_cluster
        # Auto-import the cluster to local config with current kubectl context
        import_cluster_to_config(k8s_cluster)
        Config::ClusterConfig.set_current_cluster(cluster_name)
        cluster = Config::ClusterConfig.get_cluster(cluster_name)
      else
        show_cluster_not_found_error(cluster_name)
        exit 1
      end
    end

    Formatters::ProgressFormatter.success("Switched to cluster '#{cluster_name}'")

    # Get cluster details from Kubernetes for complete information
    begin
      k8s = Helpers::ClusterValidator.kubernetes_client(cluster_name)
      cluster_resource = k8s.get_resource('LanguageCluster', cluster[:name], cluster[:namespace])
      status = cluster_resource.dig('status', 'phase') || 'Unknown'
      domain = cluster_resource.dig('spec', 'domain')

      puts
      format_cluster_details(
        name: cluster[:name],
        namespace: cluster[:namespace],
        context: cluster[:context] || 'default',
        domain: domain,
        status: status,
        created: cluster[:created]
      )
    rescue StandardError
      # Fallback to basic display if K8s access fails
      puts
      format_cluster_details(
        name: cluster[:name],
        namespace: cluster[:namespace],
        context: cluster[:context] || 'default',
        status: 'Connection Error'
      )
    end
  end
end