-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.go
69 lines (58 loc) · 1.21 KB
/
utils.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
package dockercommand
import (
"bufio"
"bytes"
"io"
"fmt"
)
func (dock *Docker) pullImageIfNotExist(image string) error {
_, err := dock.client.InspectImage(image)
if err != nil && err.Error() == "no such image" {
err = dock.Pull(&PullOptions{image})
}
return err
}
func convertEnvMapToSlice(envMap map[string]string) []string {
envSlice := []string{}
for key, value := range envMap {
envSlice = append(envSlice, fmt.Sprintf("%s=%s", key, value))
}
return envSlice
}
type Scanner struct {
reader *bufio.Reader
line []byte
err error
dead bool
}
func NewScanner(reader io.Reader) *Scanner {
return &Scanner{reader: bufio.NewReader(reader)}
}
func (s *Scanner) Scan() bool {
if s.dead {
return false
}
line, err := s.reader.ReadBytes('\n')
line = bytes.TrimSuffix(line, []byte{'\n'})
// ReadBytes can return full or partial output even when it failed.
// e.g. it can return a full entry and EOF.
if err == nil || len(line) > 0 {
s.line = line
s.err = nil
return true
}
if err != nil {
s.line = nil
s.dead = true
if err != io.EOF {
s.err = err
}
}
return false
}
func (s *Scanner) Text() string {
return string(s.line)
}
func (s *Scanner) Err() error {
return s.err
}