/
pathTransformer.go
65 lines (54 loc) · 1.76 KB
/
pathTransformer.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
package sallust
import (
"net/url"
"os"
)
// PathTransformer is a strategy for altering paths to incorporate
// this package's features.
type PathTransformer struct {
// Rotation is the optional log rotation configuration. If supplied,
// URLs that refer to filesystem paths are altered to be lumberjack URLs.
Rotation *Rotation
// Mapping is an optional expansion function passed to os.Expand. If supplied,
// this function is used to expand $var and ${var} elements in paths.
//
// Any Mapping is always applied to a path first.
Mapping func(string) string
}
// Transform alters a path to allow for log rotation and expanded variables.
// This method may be passed to ApplyTransform.
func (pt PathTransformer) Transform(path string) (string, error) {
if pt.Mapping != nil {
path = os.Expand(path, pt.Mapping)
}
if path == "stdout" || path == "stderr" {
return path, nil
}
if pt.Rotation != nil {
u, err := url.Parse(path)
if err != nil {
return path, err
}
if len(u.Path) > 0 && (u.Scheme == "" || u.Scheme == "file") {
path = pt.Rotation.NewURL(u.Path).String()
}
}
return path, nil
}
// ApplyTransform transforms each of a set of paths using the supplied strategy.
// The transformer parameter can be PathTransformer.Transform, or a custom closure.
// This function always returns a newly allocated slice, even if no transformations are done.
// Any error interrupts the transformation, and the transformed slice's contents are undefined.
func ApplyTransform(transformer func(string) (string, error), paths ...string) (transformed []string, err error) {
if len(paths) == 0 {
return
}
transformed = make([]string, len(paths))
for i, path := range paths {
transformed[i], err = transformer(path)
if err != nil {
break
}
}
return
}