-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
149 lines (119 loc) · 3.63 KB
/
parser.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"log"
"os"
"path"
"strings"
"gopkg.in/yaml.v2"
)
type Dotfile struct {
TemplateText string // raw text of the template file
SlotText string // raw text contained in the slotfile
TemplateFilepath string // path to the template file. Absolute path.
SlotFilepath string // path to the slot file. Absolute path.
CompiledFilepath string // path to the compiled file. Absolute path.
}
type DotcopyYaml struct {
Template string `yaml:"template"`
Slotfile string `yaml:"slotfile"`
Location string `yaml:"location"`
}
type LocalConfig struct {
RootFilepath string `yaml:"root_filepath"`
MachineDirectory string `yaml:"machine_directory"`
}
type GlobalVar struct {
Name string `yaml:"name"`
Value string `yaml:"value"`
}
func ParseDotfiles(fs Filesystem, localConfig LocalConfig) ([]Dotfile, error) {
yamlData, err := unmarshalYaml(fs, path.Join(localConfig.RootFilepath, "dotcopy.yaml"))
if err != nil {
return nil, err
}
dotfiles := []Dotfile{}
for _, yamlObj := range yamlData {
dotfile := Dotfile{
TemplateFilepath: path.Join(localConfig.RootFilepath, yamlObj.Template),
SlotFilepath: path.Join(localConfig.RootFilepath, localConfig.MachineDirectory, yamlObj.Slotfile),
CompiledFilepath: yamlObj.Location,
TemplateText: "",
SlotText: "",
}
if strings.HasPrefix(dotfile.CompiledFilepath, "~/") {
dotfile.CompiledFilepath = path.Join(fs.Homedir(), dotfile.CompiledFilepath[1:])
}
templateText, err := fs.ReadFile(dotfile.TemplateFilepath)
if err == nil {
dotfile.TemplateText = templateText
}
// this is kinda a mess
// if the slotfile is empty, we just don't want to bother
// if there is no slotfile, we just copy the dotfile as is
if yamlObj.Slotfile != "" {
// first we see if the slotfile exists
exists, err := fs.FileExists(dotfile.SlotFilepath)
if exists && err == nil {
// if it does, we read it
slotText, err := fs.ReadFile(dotfile.SlotFilepath)
if err != nil {
return nil, err
}
dotfile.SlotText = slotText
} else {
// if that slotfile doesn't exist, we check to see if _default as the machine directory exists
dotfile.SlotFilepath = path.Join(localConfig.RootFilepath, "_default", yamlObj.Slotfile)
text, err := fs.ReadFile(dotfile.SlotFilepath)
if err != nil {
return nil, err
}
dotfile.SlotText = text
}
}
dotfiles = append(dotfiles, dotfile)
}
return dotfiles, nil
}
func unmarshalYaml(fs Filesystem, filepath string) ([]DotcopyYaml, error) {
rootFileText, err := fs.ReadFile(filepath)
if err != nil {
return nil, err
}
yamlData := []DotcopyYaml{}
err = yaml.Unmarshal([]byte(rootFileText), &yamlData)
if err != nil {
return nil, err
}
return yamlData, nil
}
func ParseLocalConfig(fs Filesystem) (LocalConfig, error) {
dirname := fs.Homedir()
hostname, err := os.Hostname()
if err != nil {
log.Fatal(err)
}
filepath := path.Join(dirname, ".config/dotcopy/localconfig.yaml")
localConfig := LocalConfig{
RootFilepath: path.Join(dirname, "dotfiles"),
MachineDirectory: hostname,
}
localConfigText, err := fs.ReadFile(filepath)
if err != nil {
return localConfig, err
}
err = yaml.Unmarshal([]byte(localConfigText), &localConfig)
if err != nil {
return localConfig, err
}
return localConfig, nil
}
func ParseGlobalVars(fs Filesystem, localconfig LocalConfig) ([]GlobalVar, error) {
filepath := path.Join(localconfig.RootFilepath, "vars.yaml")
globalVars := []GlobalVar{}
globalVarText, err := fs.ReadFile(filepath)
if err != nil {
return globalVars, err
}
err = yaml.Unmarshal([]byte(globalVarText), &globalVars)
return globalVars, nil
}