feat: improve config file handling
- add default config file
- read passed config file or use default if none provided
- ensure cli parameters always supersede config file settings
- add function to check if a file exists
🤖
This commit is contained in:
parent
b06fac60d5
commit
480f1c0cdd
2 changed files with 30 additions and 10 deletions
42
internal/helper/files.go
Normal file
42
internal/helper/files.go
Normal file
|
@ -0,0 +1,42 @@
|
|||
package helper
|
||||
|
||||
import (
|
||||
"github.com/charmbracelet/log"
|
||||
"gopkg.in/yaml.v3"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
|
||||
)
|
||||
|
||||
func expandUnixPath(filename string) string {
|
||||
if strings.HasPrefix(filename, "~/") {
|
||||
dirname, _ := os.UserHomeDir()
|
||||
filename = filepath.Join(dirname, filename[2:])
|
||||
}
|
||||
|
||||
return filename
|
||||
}
|
||||
|
||||
func FileExists(filename string) bool {
|
||||
_, err := os.Stat(expandUnixPath(filename))
|
||||
|
||||
return !os.IsNotExist(err)
|
||||
}
|
||||
|
||||
func ParseConfig(filename string) ChronoTomato.Config {
|
||||
var config ChronoTomato.Config
|
||||
|
||||
yamlFile, err := os.ReadFile(expandUnixPath(filename))
|
||||
if err != nil {
|
||||
log.Fatal("Error opening config file!", "reason", err)
|
||||
}
|
||||
|
||||
err = yaml.Unmarshal(yamlFile, &config)
|
||||
if err != nil {
|
||||
log.Fatalf("Unmarshal: %v", err)
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue