move config file loading out of gemlog package

This commit is contained in:
Travis Shears 2025-10-05 16:42:26 +02:00
parent 6b8ba76018
commit dc635db758
6 changed files with 46 additions and 75 deletions

View file

@ -120,7 +120,7 @@ func (m model) View() string {
var enableLogs bool = true
func Run() {
func Run(config *gemlog.Config) {
if enableLogs {
f, err := tea.LogToFile("debug.log", "debug")
if err != nil {
@ -133,12 +133,7 @@ func Run() {
slog.SetDefault(logger)
}
slog.Info("Starting gemlog cli")
config, err := gemlog.LoadConfig()
if err != nil {
fmt.Printf("Error loading config: %v", err)
os.Exit(1)
}
err = gemlog.CheckDBConnection(config)
err := gemlog.CheckDBConnection(config)
if err != nil {
fmt.Printf("Error checking db connection: %v", err)
os.Exit(1)

View file

@ -0,0 +1,32 @@
package config
import (
"fmt"
"os"
"path/filepath"
gemlog "git.travisshears.com/travisshears/gemlog-cli/gemlog"
"gopkg.in/yaml.v3"
)
// LoadConfig reads and parses the YAML configuration file from ~/.config/gemlog-cli
func LoadConfig() (*gemlog.Config, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("failed to get user home directory: %w", err)
}
configPath := filepath.Join(homeDir, ".config", "gemlog-cli", "config.yml")
data, err := os.ReadFile(configPath)
if err != nil {
return nil, fmt.Errorf("failed to read config file %s: %w", configPath, err)
}
var config gemlog.Config
if err := yaml.Unmarshal(data, &config); err != nil {
return nil, fmt.Errorf("failed to parse YAML config: %w", err)
}
return &config, nil
}