-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
copymulti.go
72 lines (59 loc) · 2 KB
/
copymulti.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
package rsync
import (
"bytes"
"fmt"
"io"
"strings"
"github.com/golang/glog"
"k8s.io/apimachinery/pkg/util/errors"
)
// copyStrategies is an ordered list of copyStrategy objects that behaves as a single
// strategy. If a strategy fails with a setup error, it continues on to the next strategy.
type copyStrategies []copyStrategy
// ensure copyStrategies implements the copyStrategy interface
var _ copyStrategy = copyStrategies{}
// Copy will call copy for strategies in list order. If a strategySetupError results from a copy,
// the next strategy will be attempted. Otherwise the error is returned.
func (ss copyStrategies) Copy(source, destination *pathSpec, out, errOut io.Writer) error {
var err error
foundStrategy := false
for _, s := range ss {
errBuf := &bytes.Buffer{}
err = s.Copy(source, destination, out, errBuf)
if _, isSetupError := err.(strategySetupError); isSetupError {
glog.V(4).Infof("Error output:\n%s", errBuf.String())
fmt.Fprintf(errOut, "WARNING: cannot use %s: %v\n", s.String(), err.Error())
continue
}
io.Copy(errOut, errBuf)
foundStrategy = true
break
}
if !foundStrategy {
err = strategySetupError("No available strategies to copy.")
}
return err
}
// Validate will call Validate on all strategies and return an aggregate of their errors
func (ss copyStrategies) Validate() error {
var errs []error
for _, s := range ss {
err := s.Validate()
if err != nil {
errs = append(errs, fmt.Errorf("invalid %v strategy: %v", s, err))
}
}
return errors.NewAggregate(errs)
}
// String will return a comma-separated list of strategy names
func (ss copyStrategies) String() string {
names := []string{}
for _, s := range ss {
names = append(names, s.String())
}
return strings.Join(names, ",")
}
// strategySetupError is an error that occurred while setting up a strategy
// (as opposed to actually executing a copy and getting an error from normal copy execution)
type strategySetupError string
func (e strategySetupError) Error() string { return string(e) }