2024-10-23 15:36:30 +00:00
|
|
|
package helper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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-27 20:34:53 +00:00
|
|
|
|
|
|
|
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
|
2024-10-23 15:36:30 +00:00
|
|
|
)
|
|
|
|
|
2024-10-30 08:32:01 +00:00
|
|
|
func expandUnixPath(filename string) string {
|
2024-10-23 18:22:43 +00:00
|
|
|
if strings.HasPrefix(filename, "~/") {
|
|
|
|
dirname, _ := os.UserHomeDir()
|
|
|
|
filename = filepath.Join(dirname, filename[2:])
|
|
|
|
}
|
2024-10-27 16:03:28 +00:00
|
|
|
|
2024-10-30 08:32:01 +00:00
|
|
|
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))
|
2024-10-23 15:36:30 +00:00
|
|
|
if err != nil {
|
2024-10-27 15:36:14 +00:00
|
|
|
log.Fatal("Error opening config file!", "reason", err)
|
2024-10-23 15:36:30 +00:00
|
|
|
}
|
2024-10-27 16:03:28 +00:00
|
|
|
|
2024-10-23 15:36:30 +00:00
|
|
|
err = yaml.Unmarshal(yamlFile, &config)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Unmarshal: %v", err)
|
|
|
|
}
|
2024-10-27 16:03:28 +00:00
|
|
|
|
2024-10-23 15:36:30 +00:00
|
|
|
return config
|
|
|
|
}
|