ChronoTomato/internal/helper/files.go
Sebastian Mark 8a0ef32c91 feat: use bubbletea framework
- implement TUI in bubbletea
  - split components into separate files
- remove now unused functions
- restructure files
2024-11-02 10:55:15 +01:00

45 lines
961 B
Go

package helper
import (
"github.com/charmbracelet/log"
"gopkg.in/yaml.v3"
"os"
"path/filepath"
"strings"
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
)
// Expands the "~" in a passed filename
func expandUnixPath(filename string) string {
if strings.HasPrefix(filename, "~/") {
dirname, _ := os.UserHomeDir()
filename = filepath.Join(dirname, filename[2:])
}
return filename
}
// Checks if the passed file exists
func FileExists(filename string) bool {
_, err := os.Stat(expandUnixPath(filename))
return !os.IsNotExist(err)
}
// Parses the ChronoTomato config in the passed config file
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
}