Load reads the configuration file and parses it.
(path string)
| 147 | |
| 148 | // Load reads the configuration file and parses it. |
| 149 | func Load(path string) (*Config, error) { |
| 150 | cfg := Config{ |
| 151 | Keybinds: defaultKeybinds(), |
| 152 | } |
| 153 | if err := toml.Unmarshal(defaultCfg, &cfg); err != nil { |
| 154 | return nil, fmt.Errorf("failed to unmarshal default config: %w", err) |
| 155 | } |
| 156 | |
| 157 | file, err := os.Open(path) |
| 158 | switch { |
| 159 | case os.IsNotExist(err): |
| 160 | slog.Info("config file does not exist, falling back to the default config", "path", path, "err", err) |
| 161 | case err != nil: |
| 162 | return nil, fmt.Errorf("failed to open config file: %w", err) |
| 163 | default: |
| 164 | defer file.Close() |
| 165 | if _, err := toml.NewDecoder(file).Decode(&cfg); err != nil { |
| 166 | return nil, fmt.Errorf("failed to decode config: %w", err) |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | applyDefaults(&cfg) |
| 171 | return &cfg, nil |
| 172 | } |
| 173 | |
| 174 | func applyDefaults(cfg *Config) { |
| 175 | if cfg.Editor == "default" { |