Class: Langfuse::CLI::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/langfuse/cli/config.rb

Constant Summary collapse

DEFAULT_HOST =
'https://cloud.langfuse.com'
DEFAULT_OUTPUT_FORMAT =
'table'
DEFAULT_PAGE_LIMIT =
50
CONFIG_DIR =
File.expand_path('~/.langfuse')
CONFIG_FILE =
File.join(CONFIG_DIR, 'config.yml')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config

Returns a new instance of Config.



15
16
17
18
19
# File 'lib/langfuse/cli/config.rb', line 15

def initialize(options = {})
  @profile = options[:profile] || ENV['LANGFUSE_PROFILE'] || 'default'
  load_config
  merge_options(options)
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



7
8
9
# File 'lib/langfuse/cli/config.rb', line 7

def host
  @host
end

#output_formatObject

Returns the value of attribute output_format.



7
8
9
# File 'lib/langfuse/cli/config.rb', line 7

def output_format
  @output_format
end

#page_limitObject

Returns the value of attribute page_limit.



7
8
9
# File 'lib/langfuse/cli/config.rb', line 7

def page_limit
  @page_limit
end

#profileObject

Returns the value of attribute profile.



7
8
9
# File 'lib/langfuse/cli/config.rb', line 7

def profile
  @profile
end

#public_keyObject

Returns the value of attribute public_key.



7
8
9
# File 'lib/langfuse/cli/config.rb', line 7

def public_key
  @public_key
end

#secret_keyObject

Returns the value of attribute secret_key.



7
8
9
# File 'lib/langfuse/cli/config.rb', line 7

def secret_key
  @secret_key
end

Class Method Details

.load(profile = nil) ⇒ Object

Load a specific profile



119
120
121
# File 'lib/langfuse/cli/config.rb', line 119

def self.load(profile = nil)
  new(profile: profile)
end

Instance Method Details

#load_configObject

Load configuration from file and environment variables Priority: passed options > ENV vars > config file > defaults



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/langfuse/cli/config.rb', line 23

def load_config
  # Start with defaults
  @host = DEFAULT_HOST
  @output_format = DEFAULT_OUTPUT_FORMAT
  @page_limit = DEFAULT_PAGE_LIMIT
  @public_key = nil
  @secret_key = nil

  # Load from config file if it exists
  if File.exist?(CONFIG_FILE)
    load_from_file
  end

  # Override with environment variables
  load_from_env
end

#load_from_envObject

Load configuration from environment variables



57
58
59
60
61
# File 'lib/langfuse/cli/config.rb', line 57

def load_from_env
  @public_key = ENV['LANGFUSE_PUBLIC_KEY'] if ENV['LANGFUSE_PUBLIC_KEY']
  @secret_key = ENV['LANGFUSE_SECRET_KEY'] if ENV['LANGFUSE_SECRET_KEY']
  @host = ENV['LANGFUSE_HOST'] if ENV['LANGFUSE_HOST']
end

#load_from_fileObject

Load configuration from YAML file



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/langfuse/cli/config.rb', line 41

def load_from_file
  config_data = YAML.load_file(CONFIG_FILE)

  # Load profile-specific config
  profile_config = config_data.dig('profiles', @profile) || config_data['default'] || {}

  @public_key = profile_config['public_key'] if profile_config['public_key']
  @secret_key = profile_config['secret_key'] if profile_config['secret_key']
  @host = profile_config['host'] if profile_config['host']
  @output_format = profile_config['output_format'] if profile_config['output_format']
  @page_limit = profile_config['page_limit'] if profile_config['page_limit']
rescue => e
  warn "Warning: Error loading config file: #{e.message}"
end

#merge_options(options) ⇒ Object

Merge passed options (highest priority)



64
65
66
67
68
69
70
# File 'lib/langfuse/cli/config.rb', line 64

def merge_options(options)
  @public_key = options[:public_key] if options[:public_key]
  @secret_key = options[:secret_key] if options[:secret_key]
  @host = options[:host] if options[:host]
  @output_format = options[:format] if options[:format]
  @page_limit = options[:limit] if options[:limit]
end

#missing_fieldsObject

Get list of missing required fields



78
79
80
81
82
83
84
# File 'lib/langfuse/cli/config.rb', line 78

def missing_fields
  fields = []
  fields << 'public_key' if @public_key.nil?
  fields << 'secret_key' if @secret_key.nil?
  fields << 'host' if @host.nil?
  fields
end

#save(profile_name = nil) ⇒ Object

Save current configuration to file



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
112
113
114
115
116
# File 'lib/langfuse/cli/config.rb', line 87

def save(profile_name = nil)
  profile_name ||= @profile

  # Ensure config directory exists
  FileUtils.mkdir_p(CONFIG_DIR)

  # Load existing config or create new
  config_data = File.exist?(CONFIG_FILE) ? YAML.load_file(CONFIG_FILE) : {}
  config_data['profiles'] ||= {}

  # Update profile
  config_data['profiles'][profile_name] = {
    'public_key' => @public_key,
    'secret_key' => @secret_key,
    'host' => @host,
    'output_format' => @output_format,
    'page_limit' => @page_limit
  }

  # Write to file
  File.write(CONFIG_FILE, config_data.to_yaml)

  # Set restrictive permissions
  File.chmod(0600, CONFIG_FILE)

  true
rescue => e
  warn "Error saving config: #{e.message}"
  false
end

#to_hObject

Get configuration as a hash



124
125
126
127
128
129
130
131
132
133
# File 'lib/langfuse/cli/config.rb', line 124

def to_h
  {
    public_key: @public_key,
    secret_key: @secret_key,
    host: @host,
    profile: @profile,
    output_format: @output_format,
    page_limit: @page_limit
  }
end

#valid?Boolean

Validate that required configuration is present

Returns:

  • (Boolean)


73
74
75
# File 'lib/langfuse/cli/config.rb', line 73

def valid?
  !@public_key.nil? && !@secret_key.nil? && !@host.nil?
end