move config file loading out of gemlog package
This commit is contained in:
parent
6b8ba76018
commit
dc635db758
6 changed files with 46 additions and 75 deletions
|
|
@ -1,13 +1,5 @@
|
||||||
package gemlog
|
package gemlog
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
)
|
|
||||||
|
|
||||||
// CouchDBConfig represents the CouchDB configuration section
|
// CouchDBConfig represents the CouchDB configuration section
|
||||||
type CouchDBConfig struct {
|
type CouchDBConfig struct {
|
||||||
Host string `yaml:"host"`
|
Host string `yaml:"host"`
|
||||||
|
|
@ -20,25 +12,3 @@ type CouchDBConfig struct {
|
||||||
type Config struct {
|
type Config struct {
|
||||||
CouchDB CouchDBConfig `yaml:"couchdb"`
|
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
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,6 @@
|
||||||
package gemlog
|
package gemlog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
|
||||||
"encoding/hex"
|
|
||||||
"fmt"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -16,6 +13,7 @@ type GemlogEntry struct {
|
||||||
Gemtxt string `json:"gemtxt"`
|
Gemtxt string `json:"gemtxt"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GemlogListEntry is for showing a list of gemlog entries without the main Gemtxt
|
||||||
type GemlogListEntry struct {
|
type GemlogListEntry struct {
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Slug string `json:"slug"`
|
Slug string `json:"slug"`
|
||||||
|
|
@ -23,25 +21,3 @@ type GemlogListEntry struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Rev string `json:"rev"`
|
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
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -120,7 +120,7 @@ func (m model) View() string {
|
||||||
|
|
||||||
var enableLogs bool = true
|
var enableLogs bool = true
|
||||||
|
|
||||||
func Run() {
|
func Run(config *gemlog.Config) {
|
||||||
if enableLogs {
|
if enableLogs {
|
||||||
f, err := tea.LogToFile("debug.log", "debug")
|
f, err := tea.LogToFile("debug.log", "debug")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -133,12 +133,7 @@ func Run() {
|
||||||
slog.SetDefault(logger)
|
slog.SetDefault(logger)
|
||||||
}
|
}
|
||||||
slog.Info("Starting gemlog cli")
|
slog.Info("Starting gemlog cli")
|
||||||
config, err := gemlog.LoadConfig()
|
err := gemlog.CheckDBConnection(config)
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Error loading config: %v", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
err = gemlog.CheckDBConnection(config)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error checking db connection: %v", err)
|
fmt.Printf("Error checking db connection: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
|
||||||
32
internal/ui/config/config.go
Normal file
32
internal/ui/config/config.go
Normal 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
12
main.go
|
|
@ -1,9 +1,19 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
ui "git.travisshears.com/travisshears/gemlog-cli/internal/ui"
|
ui "git.travisshears.com/travisshears/gemlog-cli/internal/ui"
|
||||||
|
config "git.travisshears.com/travisshears/gemlog-cli/internal/ui/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ui.Run()
|
c, err := config.LoadConfig()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error loading config: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
ui.Run(c)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue