-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.go
56 lines (48 loc) · 1.16 KB
/
container.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
package dea
import (
"bytes"
"context"
"sync"
"time"
)
type Container struct {
ID string `json:"id"`
// Cond is a condition variable to signal new terminal output presence in commands
Cond *sync.Cond `json:"-"`
// StdinCond is a condition variable to signal that command is finished
StdinCond *sync.Cond `json:"-"`
StoppedAt *time.Time `json:"stopped_at"`
Error error `json:"error"`
commands []*Command
buffers map[LineKind]*bytes.Buffer
Ctx context.Context `json:"-"`
}
func NewContainer() *Container {
cnt := Container{
Cond: sync.NewCond(&sync.Mutex{}),
StdinCond: sync.NewCond(&sync.Mutex{}),
commands: []*Command{},
buffers: map[LineKind]*bytes.Buffer{
StdOut: bytes.NewBuffer([]byte{}),
StdErr: bytes.NewBuffer([]byte{}),
},
}
cnt.StartCommand("startup")
return &cnt
}
func (c *Container) StartCommand(command string) {
t := time.Now()
c.Cond.L.Lock()
if len(c.commands) > 0 {
cmd := c.commands[len(c.commands)-1]
cmd.EndedAt = &t
}
c.commands = append(c.commands, &Command{
Command: command,
StartedAt: &t,
})
c.Cond.L.Unlock()
}
func (c *Container) GetCommands() []*Command {
return c.commands
}