Skip to content

Commit cb10930

Browse files
author
mirkobrombin
committed
chore: Refactor config package and add custom log directory support
1 parent 4ef8bb0 commit cb10930

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

internal/config/config.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package config
33
import (
44
"encoding/json"
55
"fmt"
6-
"io/ioutil"
76
"os"
87
"path/filepath"
98
"runtime"
109
)
1110

11+
// customLogDir is used to override the default log directory, e.g. for testing.
12+
var customLogDir string
13+
1214
// SSLConfig represents the SSL configuration for a site.
1315
type SSLConfig struct {
1416
Enabled bool `json:"enabled"`
@@ -42,6 +44,10 @@ func GetConfigDir() string {
4244

4345
// GetLogDir returns the directory where log files are stored.
4446
func GetLogDir() string {
47+
if customLogDir != "" {
48+
return customLogDir
49+
}
50+
4551
var logDir string
4652
if xdgDataHome := os.Getenv("XDG_DATA_HOME"); xdgDataHome != "" {
4753
logDir = filepath.Join(xdgDataHome, "goup", "logs")
@@ -56,7 +62,7 @@ func GetLogDir() string {
5662
// LoadConfig loads a configuration from a file.
5763
func LoadConfig(filePath string) (SiteConfig, error) {
5864
var conf SiteConfig
59-
data, err := ioutil.ReadFile(filePath)
65+
data, err := os.ReadFile(filePath)
6066
if err != nil {
6167
return conf, err
6268
}
@@ -69,7 +75,7 @@ func LoadConfig(filePath string) (SiteConfig, error) {
6975
// LoadAllConfigs loads all configurations from the configuration directory.
7076
func LoadAllConfigs() ([]SiteConfig, error) {
7177
configDir := GetConfigDir()
72-
files, err := ioutil.ReadDir(configDir)
78+
files, err := os.ReadDir(configDir)
7379
if err != nil {
7480
return nil, err
7581
}
@@ -94,5 +100,10 @@ func (conf *SiteConfig) Save(filePath string) error {
94100
if err != nil {
95101
return err
96102
}
97-
return ioutil.WriteFile(filePath, data, 0644)
103+
return os.WriteFile(filePath, data, 0644)
104+
}
105+
106+
// SetCustomLogDir allows setting a custom log directory for testing.
107+
func SetCustomLogDir(dir string) {
108+
customLogDir = dir
98109
}

0 commit comments

Comments
 (0)