-
Notifications
You must be signed in to change notification settings - Fork 61
/
init.go
66 lines (53 loc) · 1.4 KB
/
init.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
package cli
import (
"bufio"
"bytes"
"io"
"strings"
"github.com/k8up-io/k8up/v2/restic/logging"
)
// Init initialises a repository, checks if the repository exists and will
// initialise it if not. It's safe to call this every time.
func (r *Restic) Init() error {
if r.clientCert != (clientCert{}) {
if err := generatePemFile(r.clientCert.cert, r.clientCert.key, r.clientCert.pem); err != nil {
return err
}
}
initLogger := r.logger.WithName("RepoInit")
resticLogger := initLogger.WithName("restic")
initErrorCatcher := &initStdErrWrapper{
exists: false,
Writer: logging.NewErrorWriter(resticLogger),
}
opts := CommandOptions{
Path: r.resticPath,
Args: r.globalFlags.ApplyToCommand("init"),
StdOut: logging.NewInfoWriter(resticLogger),
StdErr: initErrorCatcher,
}
cmd := NewCommand(r.ctx, initLogger, opts)
cmd.Run()
if !initErrorCatcher.exists {
return cmd.FatalError
}
return nil
}
type initStdErrWrapper struct {
exists bool
io.Writer
}
func (i *initStdErrWrapper) Write(p []byte) (n int, err error) {
scanner := bufio.NewScanner(bytes.NewReader(p))
// array of acceptable errors to attempt to continue
okErrorArray := []string{"already initialized", "already exists"}
for scanner.Scan() {
for _, errorString := range okErrorArray {
if strings.Contains(scanner.Text(), errorString) {
i.exists = true
return len(p), nil
}
}
}
return i.Writer.Write(p)
}