-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaml.go
49 lines (40 loc) · 1.02 KB
/
yaml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"fmt"
"os"
"path/filepath"
"gopkg.in/yaml.v3"
)
// LoadYaml function to handle the YAML loading logic
func LoadYaml(initial interface{}) error {
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("error getting home directory: %v", err)
}
// File paths to check
paths := []string{
filepath.Join(homeDir, ".video.yaml"),
filepath.Join(".", ".video.yaml"),
}
for _, path := range paths {
if _, err := os.Stat(path); err == nil { // File exists
if err := parseYAMLFile(path, initial); err != nil {
return fmt.Errorf("error parsing YAML file (%s): %v", path, err)
}
break // Stop after the first successful load
}
}
return nil
}
// Helper function to parse YAML file
func parseYAMLFile(path string, initial interface{}) error {
data, err := os.ReadFile(path)
if err != nil {
return err
}
err = yaml.Unmarshal(data, initial)
if err != nil {
return fmt.Errorf("error unmarshalling YAML: %v", err)
}
return nil
}