|
| 1 | +package model |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + |
| 8 | + "github.com/pelletier/go-toml/v2" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + codexConfigDir = ".codex" |
| 13 | + codexConfigFile = "config.toml" |
| 14 | +) |
| 15 | + |
| 16 | +// CodexOtelConfigService handles Codex OTEL configuration |
| 17 | +type CodexOtelConfigService interface { |
| 18 | + Install() error |
| 19 | + Uninstall() error |
| 20 | + Check() (bool, error) |
| 21 | +} |
| 22 | + |
| 23 | +type codexOtelConfigService struct { |
| 24 | + configPath string |
| 25 | +} |
| 26 | + |
| 27 | +// NewCodexOtelConfigService creates a new Codex OTEL config service |
| 28 | +func NewCodexOtelConfigService() CodexOtelConfigService { |
| 29 | + homeDir, _ := os.UserHomeDir() |
| 30 | + configPath := filepath.Join(homeDir, codexConfigDir, codexConfigFile) |
| 31 | + return &codexOtelConfigService{ |
| 32 | + configPath: configPath, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// Install adds OTEL configuration to ~/.codex/config.toml |
| 37 | +func (s *codexOtelConfigService) Install() error { |
| 38 | + // Ensure directory exists |
| 39 | + dir := filepath.Dir(s.configPath) |
| 40 | + if err := os.MkdirAll(dir, 0755); err != nil { |
| 41 | + return fmt.Errorf("failed to create config directory: %w", err) |
| 42 | + } |
| 43 | + |
| 44 | + // Read existing config or create empty map |
| 45 | + config := make(map[string]interface{}) |
| 46 | + if data, err := os.ReadFile(s.configPath); err == nil && len(data) > 0 { |
| 47 | + if err := toml.Unmarshal(data, &config); err != nil { |
| 48 | + return fmt.Errorf("failed to parse existing config: %w", err) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // Add OTEL configuration |
| 53 | + // Format: exporter = { otlp-grpc = {endpoint = "..."} } |
| 54 | + config["otel"] = map[string]interface{}{ |
| 55 | + "log_user_prompt": true, |
| 56 | + "exporter": map[string]interface{}{ |
| 57 | + "otlp-grpc": map[string]interface{}{ |
| 58 | + "endpoint": aiCodeOtelEndpoint, |
| 59 | + }, |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + // Write config back |
| 64 | + data, err := toml.Marshal(config) |
| 65 | + if err != nil { |
| 66 | + return fmt.Errorf("failed to marshal config: %w", err) |
| 67 | + } |
| 68 | + |
| 69 | + if err := os.WriteFile(s.configPath, data, 0644); err != nil { |
| 70 | + return fmt.Errorf("failed to write config file: %w", err) |
| 71 | + } |
| 72 | + |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +// Uninstall removes OTEL configuration from ~/.codex/config.toml |
| 77 | +func (s *codexOtelConfigService) Uninstall() error { |
| 78 | + // Check if config file exists |
| 79 | + if _, err := os.Stat(s.configPath); os.IsNotExist(err) { |
| 80 | + return nil // Nothing to uninstall |
| 81 | + } |
| 82 | + |
| 83 | + // Read existing config |
| 84 | + data, err := os.ReadFile(s.configPath) |
| 85 | + if err != nil { |
| 86 | + return fmt.Errorf("failed to read config file: %w", err) |
| 87 | + } |
| 88 | + |
| 89 | + config := make(map[string]interface{}) |
| 90 | + if len(data) > 0 { |
| 91 | + if err := toml.Unmarshal(data, &config); err != nil { |
| 92 | + return fmt.Errorf("failed to parse config: %w", err) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // Remove OTEL configuration |
| 97 | + delete(config, "otel") |
| 98 | + |
| 99 | + // Write config back |
| 100 | + newData, err := toml.Marshal(config) |
| 101 | + if err != nil { |
| 102 | + return fmt.Errorf("failed to marshal config: %w", err) |
| 103 | + } |
| 104 | + |
| 105 | + if err := os.WriteFile(s.configPath, newData, 0644); err != nil { |
| 106 | + return fmt.Errorf("failed to write config file: %w", err) |
| 107 | + } |
| 108 | + |
| 109 | + return nil |
| 110 | +} |
| 111 | + |
| 112 | +// Check returns true if OTEL is configured in ~/.codex/config.toml |
| 113 | +func (s *codexOtelConfigService) Check() (bool, error) { |
| 114 | + if _, err := os.Stat(s.configPath); os.IsNotExist(err) { |
| 115 | + return false, nil |
| 116 | + } |
| 117 | + |
| 118 | + data, err := os.ReadFile(s.configPath) |
| 119 | + if err != nil { |
| 120 | + return false, fmt.Errorf("failed to read config file: %w", err) |
| 121 | + } |
| 122 | + |
| 123 | + config := make(map[string]interface{}) |
| 124 | + if len(data) > 0 { |
| 125 | + if err := toml.Unmarshal(data, &config); err != nil { |
| 126 | + return false, fmt.Errorf("failed to parse config: %w", err) |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + _, exists := config["otel"] |
| 131 | + return exists, nil |
| 132 | +} |
0 commit comments