-
Notifications
You must be signed in to change notification settings - Fork 20
/
data.go
224 lines (176 loc) · 5.07 KB
/
data.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* ZEUS - An Electrifying Build System
* Copyright (c) 2017 Philipp Mieden <dreadl0ck [at] protonmail [dot] ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"errors"
"io/ioutil"
"os"
"strings"
"sync"
yaml "gopkg.in/yaml.v2"
"time"
"github.com/fsnotify/fsnotify"
)
var (
// path for the project data YAML
projectDataPath string
// ErrEmptyZeusData occurs when the zeus_data file is empty
ErrEmptyZeusData = errors.New("zeus data file is empty")
)
// zeus project data written to disk
type data struct {
fields *dataFields
sync.RWMutex
}
type dataFields struct {
BuildNumber int `yaml:"buildNumber"`
// project deadline
Deadline string `yaml:"deadline"`
// project milestones
Milestones []*milestone `yaml:"milestones"`
// alias names mapped to commands
Aliases map[string]string `yaml:"aliases"`
// mapping from watched path to the corresponding event
Events map[string]*Event `yaml:"events"`
Author string `yaml:"author"`
// keys mapped to commands
KeyBindings map[string]string `yaml:"keyBindings"`
}
func newData() *data {
return &data{
fields: &dataFields{
BuildNumber: 0,
Deadline: "",
Milestones: make([]*milestone, 0),
Aliases: make(map[string]string, 0),
Events: make(map[string]*Event, 0),
Author: "",
KeyBindings: make(map[string]string, 0),
},
}
}
// update project data on disk
func (d *data) update() {
d.Lock()
defer d.Unlock()
// marshal data
b, err := yaml.Marshal(d.fields)
if err != nil {
Log.WithError(err).Error("failed to marshal zeus data")
return
}
// get file handle
f, err := os.OpenFile(projectDataPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0700)
if err != nil {
Log.WithError(err).Error("failed to open zeus data")
return
}
// write to file
_, err = f.Write(append([]byte(asciiArtYAML), b...))
if err != nil {
Log.WithError(err).Error("failed to write zeus data")
return
}
}
// parse the project data YAML
func parseProjectData() (*data, error) {
projectDataPath = zeusDir + "/data.yml"
// init default data
var d = newData()
_, err := os.Stat(projectDataPath)
if err != nil {
return nil, err
}
contents, err := ioutil.ReadFile(projectDataPath)
if err != nil {
return nil, err
}
if len(contents) == 0 {
return nil, ErrEmptyZeusData
}
err = yaml.Unmarshal(contents, d.fields)
if err != nil {
printFileContents(contents)
Log.WithError(err).Fatal("failed to unmarshal zeus data - invalid YAML:")
return nil, err
}
return d, nil
}
// load user events from projectData and create the watchers
func loadEvents() {
projectData.Lock()
for _, e := range projectData.fields.Events {
// reload internal watchers from project data
if e.Command == "internal" {
// remove from projectData
delete(projectData.fields.Events, e.ID)
reloadEvent(e)
continue
}
fields := strings.Fields(e.Command)
Log.Info("loading event: ", e.Command, " path: ", e.Path)
// addEvent will create a new eventID so we need to clean up the entry for the previous one
delete(projectData.fields.Events, e.ID)
// copy values from struct
var (
path = e.Path
op = e.Op
command = e.Command
name = e.Name
fileExtension = e.FileExtension
)
go func() {
err := addEvent(newEvent(path, op, name, fileExtension, "", command, func(event fsnotify.Event) {
Log.Debug("event fired, name: ", event.Name, " path: ", path)
// validate commandChain
if cmdChain, ok := validCommandChain(fields, true); ok {
cmdChain.exec(fields)
} else {
Log.Debug("passing command to shell: ", fields)
// its a shell command
// ignoring the error here, because stdin, stdout and stderr will be wired up when the command gets executed
// so the user will become aware of any errors that occur.
if len(fields) > 1 {
_ = passCommandToShell(fields[0], fields[1:])
} else {
_ = passCommandToShell(fields[0], []string{})
}
}
}))
if err != nil {
Log.Error("failed to watch path: ", path)
}
}()
}
projectData.Unlock()
// event creation is async
// wait a little bit to avoid duplicate internal events
time.Sleep(50 * time.Millisecond)
}
// print the current project data as YAML to stdout
func printProjectData() {
projectData.Lock()
defer projectData.Unlock()
l.Println()
// make it pretty
b, err := yaml.Marshal(projectData.fields)
if err != nil {
Log.WithError(err).Fatal("failed to marshal zeus project data to YAML")
}
l.Println(string(b))
}