Class: LanguageOperator::Config::ClusterConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/language_operator/config/cluster_config.rb

Overview

Manages cluster configuration in ~/.langop/config.yaml

Constant Summary collapse

CONFIG_DIR =
LanguageOperator::Utils::SecurePath.expand_home_path('.langop')
CONFIG_PATH =
File.join(CONFIG_DIR, 'config.yaml')

Class Method Summary collapse

Class Method Details

.add_cluster(name, namespace, kubeconfig, context) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/language_operator/config/cluster_config.rb', line 42

def add_cluster(name, namespace, kubeconfig, context)
  config = load
  config['clusters'] ||= []

  # Remove existing cluster with same name
  config['clusters'].reject! { |c| c['name'] == name }

  # Add new cluster
  config['clusters'] << {
    'name' => name,
    'namespace' => namespace,
    'kubeconfig' => kubeconfig,
    'context' => context,
    'created' => Time.now.utc.iso8601
  }

  save(config)
end

.cluster_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/language_operator/config/cluster_config.rb', line 85

def cluster_exists?(name)
  !get_cluster(name).nil?
end

.current_clusterObject



29
30
31
32
# File 'lib/language_operator/config/cluster_config.rb', line 29

def current_cluster
  config = load
  config['current-cluster']
end

.get_cluster(name) ⇒ Object



71
72
73
74
75
76
# File 'lib/language_operator/config/cluster_config.rb', line 71

def get_cluster(name)
  config = load
  cluster = config['clusters']&.find { |c| c['name'] == name }
  # Convert string keys to symbol keys for easier access
  cluster&.transform_keys(&:to_sym)
end

.list_clustersObject



78
79
80
81
82
83
# File 'lib/language_operator/config/cluster_config.rb', line 78

def list_clusters
  config = load
  clusters = config['clusters'] || []
  # Convert string keys to symbol keys for easier access
  clusters.map { |c| c.transform_keys(&:to_sym) }
end

.loadObject



15
16
17
18
19
20
21
22
# File 'lib/language_operator/config/cluster_config.rb', line 15

def load
  return default_config unless File.exist?(CONFIG_PATH)

  YAML.safe_load_file(CONFIG_PATH, permitted_classes: [Symbol], aliases: true) || default_config
rescue StandardError => e
  warn "Warning: Failed to load config from #{CONFIG_PATH}: #{e.message}"
  default_config
end

.remove_cluster(name) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/language_operator/config/cluster_config.rb', line 61

def remove_cluster(name)
  config = load
  config['clusters']&.reject! { |c| c['name'] == name }

  # Clear current-cluster if it was the removed one
  config['current-cluster'] = nil if config['current-cluster'] == name

  save(config)
end

.save(config) ⇒ Object



24
25
26
27
# File 'lib/language_operator/config/cluster_config.rb', line 24

def save(config)
  FileUtils.mkdir_p(CONFIG_DIR)
  File.write(CONFIG_PATH, YAML.dump(config))
end

.set_current_cluster(name) ⇒ Object

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
# File 'lib/language_operator/config/cluster_config.rb', line 34

def set_current_cluster(name)
  config = load
  raise ArgumentError, "Cluster '#{name}' does not exist" unless cluster_exists?(name)

  config['current-cluster'] = name
  save(config)
end