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

@ -1,13 +1,5 @@
package gemlog
import (
"fmt"
"os"
"path/filepath"
"gopkg.in/yaml.v3"
)
// CouchDBConfig represents the CouchDB configuration section
type CouchDBConfig struct {
Host string `yaml:"host"`
@ -20,25 +12,3 @@ type CouchDBConfig struct {
type Config struct {
CouchDB CouchDBConfig `yaml:"couchdb"`
}
// LoadConfig reads and parses the YAML configuration file from ~/.config/gemlog-cli
func LoadConfig() (*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 Config
if err := yaml.Unmarshal(data, &config); err != nil {
return nil, fmt.Errorf("failed to parse YAML config: %w", err)
}
return &config, nil
}

View file

@ -1,9 +1,6 @@
package gemlog
import (
"crypto/rand"
"encoding/hex"
"fmt"
"time"
)
@ -16,6 +13,7 @@ type GemlogEntry struct {
Gemtxt string `json:"gemtxt"`
}
// GemlogListEntry is for showing a list of gemlog entries without the main Gemtxt
type GemlogListEntry struct {
Title string `json:"title"`
Slug string `json:"slug"`
@ -23,25 +21,3 @@ type GemlogListEntry struct {
ID string `json:"id"`
Rev string `json:"rev"`
}
// NewUUID generates a new UUID v4 using crypto/rand
func NewUUID() (string, error) {
uuid := make([]byte, 16)
_, err := rand.Read(uuid)
if err != nil {
return "", fmt.Errorf("failed to generate UUID: %w", err)
}
// Set version (4) and variant bits according to RFC 4122
uuid[6] = (uuid[6] & 0x0f) | 0x40 // Version 4
uuid[8] = (uuid[8] & 0x3f) | 0x80 // Variant bits
// Format as standard UUID string: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
return fmt.Sprintf("%s-%s-%s-%s-%s",
hex.EncodeToString(uuid[0:4]),
hex.EncodeToString(uuid[4:6]),
hex.EncodeToString(uuid[6:8]),
hex.EncodeToString(uuid[8:10]),
hex.EncodeToString(uuid[10:16]),
), nil
}

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
}

12
main.go
View file

@ -1,9 +1,19 @@
package main
import (
"fmt"
"os"
ui "git.travisshears.com/travisshears/gemlog-cli/internal/ui"
config "git.travisshears.com/travisshears/gemlog-cli/internal/ui/config"
)
func main() {
ui.Run()
c, err := config.LoadConfig()
if err != nil {
fmt.Printf("Error loading config: %v", err)
os.Exit(1)
}
ui.Run(c)
}

View file

@ -1,12 +0,0 @@
title: todo
date: 2025-12-31
slug: todo
tags: cat, dog
---
Example text
=> https://travisshears.com example link
* Example list item 1
* Example list item 2