-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
105 lines (87 loc) · 1.92 KB
/
Copy pathutils.go
File metadata and controls
105 lines (87 loc) · 1.92 KB
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
package godexer
import (
"bytes"
"io"
"os"
"sync"
"time"
"unicode"
"github.com/ghodss/yaml"
"github.com/spf13/afero"
)
var TimeSleep = time.Sleep
func ShellEscape(cmd string) string {
result := escapeArgs([]string{cmd})
return result
}
func fileExists(fs afero.Fs, fname string) (bool, error) {
if _, err := fs.Stat(fname); err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return true, nil
}
// toJSON converts a single YAML document into a JSON document
// or returns an error. If the document appears to be JSON the
// YAML decoding path is not used.
func toJSON(data []byte) ([]byte, error) {
if hasJSONPrefix(data) {
return data, nil
}
return yaml.YAMLToJSON(data)
}
var jsonPrefix = []byte("{")
// hasJSONPrefix returns true if the provided buffer appears to start with
// a JSON open brace.
func hasJSONPrefix(buf []byte) bool {
return hasPrefix(buf, jsonPrefix)
}
// Return true if the first non-whitespace bytes in buf is prefix.
func hasPrefix(buf, prefix []byte) bool {
trim := bytes.TrimLeftFunc(buf, unicode.IsSpace)
return bytes.HasPrefix(trim, prefix)
}
type Buffer struct {
buf bytes.Buffer
lock sync.RWMutex
}
func (b *Buffer) Write(p []byte) (n int, err error) {
b.lock.Lock()
defer b.lock.Unlock()
return b.buf.Write(p)
}
func (b *Buffer) Read(p []byte) (n int, err error) {
b.lock.RLock()
defer b.lock.RUnlock()
return b.buf.Read(p)
}
func (b *Buffer) String() string {
b.lock.RLock()
defer b.lock.RUnlock()
return b.buf.String()
}
type CombinedWriter struct {
writers []io.Writer
}
func NewCombinedWriter(writers []io.Writer) *CombinedWriter {
return &CombinedWriter{
writers: writers,
}
}
func (w *CombinedWriter) Write(p []byte) (n int, err error) {
for _, wr := range w.writers {
_, err := wr.Write(p)
if err != nil {
return -1, err
}
}
return len(p), nil
}
func stringDef(s, def string) string {
if s == "" {
return def
}
return s
}