-
Notifications
You must be signed in to change notification settings - Fork 8
/
run.go
67 lines (57 loc) · 1.45 KB
/
run.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
package goflow
import (
"fmt"
"os"
"path"
"strings"
"time"
)
// WriteFile writes content into a file called filename. It updates the file only
// if there is a difference between the current content of the file and what is
// passed in content.
//
// It also logs the time taken since start, because why not.
func WriteFile(content, filename string, start time.Time) error {
before, err := os.ReadFile(filename)
if err != nil && !os.IsNotExist(err) {
return err
}
if string(before) == content {
// Do not write file if there is nothing new to write...
return nil
}
created := os.IsNotExist(err)
wf, err := os.OpenFile(filename, os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0o600)
if err != nil {
return err
}
defer wf.Close()
if _, err := wf.WriteString(content); err != nil {
return err
}
action := "updated"
if created {
action = "created"
}
fmt.Println(filename, action, "in", time.Since(start))
return nil
}
func FindGraphFileNames(dir string) ([]string, error) {
files, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
graphNames := make([]string, 0)
for _, file := range files {
if file.IsDir() && file.Name() != "vendor" {
subs, err := FindGraphFileNames(path.Join(dir, file.Name()))
if err != nil {
return nil, err
}
graphNames = append(graphNames, subs...)
} else if strings.HasSuffix(file.Name(), ".yml") {
graphNames = append(graphNames, path.Join(dir, file.Name()))
}
}
return graphNames, nil
}