-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.go
More file actions
242 lines (212 loc) · 5.98 KB
/
config.go
File metadata and controls
242 lines (212 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package opik
import (
"bufio"
"os"
"path/filepath"
"strings"
)
const (
// DefaultCloudURL is the default URL for Opik Cloud
DefaultCloudURL = "https://www.comet.com/opik/api"
// DefaultLocalURL is the default URL for local Opik server
DefaultLocalURL = "http://localhost:5173/api"
// DefaultProjectName is the default project name
DefaultProjectName = "Default Project"
// DefaultWorkspace is the default workspace name
DefaultWorkspace = "default"
// DefaultConfigFile is the default config file path
DefaultConfigFile = "~/.opik.config"
)
// Environment variable names
const (
EnvURLOverride = "OPIK_URL_OVERRIDE"
EnvAPIKey = "OPIK_API_KEY" //nolint:gosec // G101: This is an environment variable name, not a credential
EnvWorkspace = "OPIK_WORKSPACE"
EnvProjectName = "OPIK_PROJECT_NAME"
EnvTraceDisable = "OPIK_TRACK_DISABLE"
)
// Config holds the configuration for the Opik client.
type Config struct {
// URL is the Opik API endpoint URL.
// Defaults to DefaultCloudURL if APIKey is set, otherwise DefaultLocalURL.
URL string
// APIKey is the API key for authentication with Opik Cloud.
// Not required for local/self-hosted instances.
APIKey string //nolint:gosec // G117: Intentional - SDK config needs API key field
// Workspace is the workspace name for Opik Cloud.
// Required for Opik Cloud, ignored for local instances.
Workspace string
// ProjectName is the default project name for traces.
ProjectName string
// TracingDisabled disables tracing globally when set to true.
TracingDisabled bool
// CheckTLSCertificate enables TLS certificate verification.
// Defaults to true.
CheckTLSCertificate bool
}
// NewConfig creates a new Config with default values.
func NewConfig() *Config {
return &Config{
URL: "",
APIKey: "",
Workspace: DefaultWorkspace,
ProjectName: DefaultProjectName,
TracingDisabled: false,
CheckTLSCertificate: true,
}
}
// LoadConfig loads configuration from environment variables and config file.
// Priority order (highest to lowest):
// 1. Explicitly set values (via options)
// 2. Environment variables
// 3. Config file (~/.opik.config)
// 4. Default values
func LoadConfig() *Config {
cfg := NewConfig()
// Load from config file first (lowest priority of external sources)
cfg.loadFromFile()
// Load from environment variables (overrides config file)
cfg.loadFromEnv()
// Set default URL based on whether API key is present
if cfg.URL == "" {
if cfg.APIKey != "" {
cfg.URL = DefaultCloudURL
} else {
cfg.URL = DefaultLocalURL
}
}
return cfg
}
// loadFromEnv loads configuration from environment variables.
func (c *Config) loadFromEnv() {
if url := os.Getenv(EnvURLOverride); url != "" {
c.URL = url
}
if apiKey := os.Getenv(EnvAPIKey); apiKey != "" {
c.APIKey = apiKey
}
if workspace := os.Getenv(EnvWorkspace); workspace != "" {
c.Workspace = workspace
}
if projectName := os.Getenv(EnvProjectName); projectName != "" {
c.ProjectName = projectName
}
if disable := os.Getenv(EnvTraceDisable); disable != "" {
c.TracingDisabled = strings.ToLower(disable) == "true" || disable == "1"
}
}
// loadFromFile loads configuration from the config file.
func (c *Config) loadFromFile() {
configPath := DefaultConfigFile
if strings.HasPrefix(configPath, "~") {
home, err := os.UserHomeDir()
if err != nil {
return
}
configPath = filepath.Join(home, configPath[1:])
}
file, err := os.Open(configPath)
if err != nil {
return // Config file doesn't exist, which is fine
}
defer file.Close()
// Parse INI-style config file
scanner := bufio.NewScanner(file)
section := ""
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
// Skip empty lines and comments
if line == "" || strings.HasPrefix(line, "#") || strings.HasPrefix(line, ";") {
continue
}
// Section header
if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
section = strings.ToLower(strings.TrimSpace(line[1 : len(line)-1]))
continue
}
// Key-value pair
parts := strings.SplitN(line, "=", 2)
if len(parts) != 2 {
continue
}
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
// Only process [opik] section or no section
if section != "" && section != "opik" {
continue
}
switch strings.ToLower(key) {
case "url_override", "url":
if c.URL == "" {
c.URL = value
}
case "api_key", "apikey":
if c.APIKey == "" {
c.APIKey = value
}
case "workspace":
if c.Workspace == DefaultWorkspace {
c.Workspace = value
}
case "project_name", "projectname":
if c.ProjectName == DefaultProjectName {
c.ProjectName = value
}
}
}
}
// IsCloud returns true if the configuration is for Opik Cloud.
func (c *Config) IsCloud() bool {
return c.APIKey != "" || strings.Contains(c.URL, "comet.com")
}
// Validate checks if the configuration is valid.
func (c *Config) Validate() error {
if c.URL == "" {
return ErrMissingURL
}
if c.IsCloud() && c.APIKey == "" {
return ErrMissingAPIKey
}
return nil
}
// SaveConfig saves the configuration to the config file.
func SaveConfig(cfg *Config) error {
configPath := DefaultConfigFile
if strings.HasPrefix(configPath, "~") {
home, err := os.UserHomeDir()
if err != nil {
return err
}
configPath = filepath.Join(home, configPath[1:])
}
file, err := os.Create(configPath)
if err != nil {
return err
}
defer file.Close()
w := bufio.NewWriter(file)
if _, err := w.WriteString("[opik]\n"); err != nil {
return err
}
if cfg.URL != "" {
if _, err := w.WriteString("url_override = " + cfg.URL + "\n"); err != nil {
return err
}
}
if cfg.APIKey != "" {
if _, err := w.WriteString("api_key = " + cfg.APIKey + "\n"); err != nil {
return err
}
}
if cfg.Workspace != "" {
if _, err := w.WriteString("workspace = " + cfg.Workspace + "\n"); err != nil {
return err
}
}
if cfg.ProjectName != "" {
if _, err := w.WriteString("project_name = " + cfg.ProjectName + "\n"); err != nil {
return err
}
}
return w.Flush()
}