forked from segmentio/analytics.js-integrations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
139 lines (113 loc) · 3.05 KB
/
utils.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
package operations
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"text/template"
"github.com/blang/semver"
)
// Verbose prints some extra info
var Verbose = false
// Output prints the message to stdout
func Output(format string, args ...interface{}) {
fmt.Fprintf(os.Stdout, format+"\n", args...)
}
// Log prints the message to stderr
func Log(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, format+"\n", args...)
}
// LogError prints the error and a message to stderr
func LogError(err error, format string, args ...interface{}) {
a := append(args, err.Error())
fmt.Fprintf(os.Stderr, format+": %s\n", a...)
}
// Debug prints a message if the Verbose flag is set
func Debug(format string, args ...interface{}) {
if Verbose {
Log(format, args...)
}
}
// copyFiles copies all the files/directories into
// the destination folder.
func copyFiles(src, dst string, ignorePaths map[string]bool) error {
Debug("Copying %s into %s", src, dst)
if err := makeDir(dst); err != nil {
LogError(err, "Error creating destination folder")
return err
}
files, err := ioutil.ReadDir(src)
if err != nil {
LogError(err, "Error reading files of %s", src)
return err
}
for _, entry := range files {
name := entry.Name()
if ignorePaths[name] {
continue
}
if err := copy(path.Join(src, name), path.Join(dst, name)); err != nil {
LogError(err, "Error copying %s", name)
return err
}
}
return nil
}
// copy copies the file/directory to a different directory
// Golang, why don't you have this in the standard library?
func copy(src, dst string) error {
Debug("Copying %s into %s", src, dst)
cmd := exec.Command("cp", "-r", src, dst)
if Verbose {
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
}
return cmd.Run()
}
// makeDir creates the full path for the integration
func makeDir(dir string) error {
Debug("Creating %s", dir)
return exec.Command("mkdir", "-p", dir).Run()
}
func executeTemplate(tmpl *template.Template, data interface{}) string {
buffer := new(bytes.Buffer)
if err := tmpl.Execute(buffer, data); err != nil {
panic(err)
}
return buffer.String()
}
func fileExists(file string) (bool, error) {
if _, err := os.Stat(file); err != nil {
if os.IsNotExist(err) {
return false, nil
}
LogError(err, "Error reading file")
return false, err
}
return true, nil
}
func writeFileWithTemplate(filename string, tmpl *template.Template, data interface{}) error {
if err := ioutil.WriteFile(filename, []byte(executeTemplate(tmpl, data)), 0644); err != nil {
LogError(err, "Error writing file")
return err
}
return nil
}
// CompareSemanticVersion returns -1, 0 or 1 if the version is older, the
// same or newer than the other.
// See https://godoc.org/github.com/blang/semver#Version.Compare
func CompareSemanticVersion(version, other string) (int, error) {
v, err := semver.Parse(version)
if err != nil {
LogError(err, "Error parsing %s", version)
return 0, err
}
o, err := semver.Parse(other)
if err != nil {
LogError(err, "Error parsing %s", other)
return 0, err
}
return v.Compare(o), nil
}