-
Notifications
You must be signed in to change notification settings - Fork 99
/
cmd_run_result_buf.go
46 lines (36 loc) · 1001 Bytes
/
cmd_run_result_buf.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
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package deploy
import (
"sync"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/exec"
)
type WriterFunc func([]byte) (int, error)
func (f WriterFunc) Write(data []byte) (int, error) {
return f(data)
}
type CmdRunResultBuffer struct {
result *exec.CmdRunResult
resultLock *sync.RWMutex
}
func NewCmdRunResultBuffer() *CmdRunResultBuffer {
return &CmdRunResultBuffer{result: &exec.CmdRunResult{}, resultLock: &sync.RWMutex{}}
}
func (w *CmdRunResultBuffer) WriteStdout(data []byte) (int, error) {
w.resultLock.Lock()
w.result.Stdout += string(data)
w.resultLock.Unlock()
return len(data), nil
}
func (w *CmdRunResultBuffer) WriteStderr(data []byte) (int, error) {
w.resultLock.Lock()
w.result.Stderr += string(data)
w.resultLock.Unlock()
return len(data), nil
}
func (w *CmdRunResultBuffer) Copy() exec.CmdRunResult {
w.resultLock.RLock()
result := *w.result
w.resultLock.RUnlock()
return result
}