2024-10-23 15:36:30 +00:00
|
|
|
package helper
|
|
|
|
|
|
|
|
import (
|
2024-10-27 09:41:31 +00:00
|
|
|
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
|
2024-10-23 15:36:30 +00:00
|
|
|
"github.com/charmbracelet/log"
|
2024-10-25 02:00:59 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2024-10-23 15:36:30 +00:00
|
|
|
"os"
|
2024-10-23 18:22:43 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2024-10-23 15:36:30 +00:00
|
|
|
)
|
|
|
|
|
2024-10-27 09:41:31 +00:00
|
|
|
func ParseConfig(filename string) ChronoTomato.Config {
|
|
|
|
var config ChronoTomato.Config
|
2024-10-23 18:22:43 +00:00
|
|
|
if strings.HasPrefix(filename, "~/") {
|
|
|
|
dirname, _ := os.UserHomeDir()
|
|
|
|
filename = filepath.Join(dirname, filename[2:])
|
|
|
|
}
|
2024-10-23 15:36:30 +00:00
|
|
|
yamlFile, err := os.ReadFile(filename)
|
|
|
|
if err != nil {
|
2024-10-24 05:31:34 +00:00
|
|
|
log.Warn("Error opening config file!", "reason", err)
|
|
|
|
log.Warn("Using defaults")
|
2024-10-27 09:41:31 +00:00
|
|
|
return ChronoTomato.Config{
|
2024-10-23 15:36:30 +00:00
|
|
|
URL: "ws://localhost:8080/ws",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = yaml.Unmarshal(yamlFile, &config)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Unmarshal: %v", err)
|
|
|
|
}
|
|
|
|
return config
|
|
|
|
|
|
|
|
}
|