/
container.go
157 lines (127 loc) · 2.87 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package container
import (
"encoding/json"
"errors"
"time"
)
const timeFormat = time.RFC3339
type Container struct {
impl
}
type impl struct {
ID_ ID `json:"id"`
Name_ string `json:"name"`
Status_ Status `json:"status"`
ExitCode_ int32 `json:"exitCode"`
CreatedAt_ string `json:"createdAt"`
StartedAt_ string `json:"startedAt,omitempty"`
FinishedAt_ string `json:"finishedAt,omitempty"`
Command_ []string `json:"command"`
Args_ []string `json:"args,omitempty"`
Rootfs_ string `json:"rootfs"`
LogPath_ string `json:"logPath,omitempty"`
}
func New(
id ID,
name string,
logPath string,
) (*Container, error) {
if !isValidName(name) {
return nil, errors.New("Invalid container name")
}
return &Container{
impl{
ID_: id,
Name_: name,
LogPath_: logPath,
},
}, nil
}
func (c *Container) ID() ID {
return c.ID_
}
func (c *Container) Name() string {
return c.Name_
}
func (c *Container) CreatedAt() string {
return c.CreatedAt_
}
func (c *Container) CreatedAtNano() int64 {
return unixNanoTime(c.CreatedAt())
}
func (c *Container) SetCreatedAt(t time.Time) error {
if c.CreatedAt_ != "" {
return errors.New("CreatedAt has been already set")
}
c.CreatedAt_ = t.Format(timeFormat)
return nil
}
func (c *Container) StartedAt() string {
return c.StartedAt_
}
func (c *Container) StartedAtNano() int64 {
if c.StartedAt_ == "" {
return 0
}
return unixNanoTime(c.StartedAt())
}
func (c *Container) SetStartedAt(t time.Time) error {
if c.StartedAt_ != "" {
return errors.New("StartedAt has been already set")
}
c.StartedAt_ = t.Format(timeFormat)
return nil
}
func (c *Container) FinishedAt() string {
return c.FinishedAt_
}
func (c *Container) FinishedAtNano() int64 {
if c.FinishedAt_ == "" {
return 0
}
return unixNanoTime(c.FinishedAt())
}
func (c *Container) SetFinishedAt(t time.Time) error {
finishedAt := t.Format(timeFormat)
if c.FinishedAt_ == "" || c.FinishedAt_ == finishedAt {
c.FinishedAt_ = finishedAt
return nil
}
return errors.New("FinishedAt has been already set")
}
func (c *Container) Status() Status {
return c.Status_
}
func (c *Container) SetStatus(s Status) {
c.Status_ = s
}
func (c *Container) ExitCode() int32 {
return c.ExitCode_
}
func (c *Container) SetExitCode(code int32) {
c.ExitCode_ = code
}
func (c *Container) LogPath() string {
return c.LogPath_
}
func (c *Container) MarshalJSON() ([]byte, error) {
return json.Marshal(c.impl)
}
func (c *Container) UnmarshalJSON(bytes []byte) error {
return json.Unmarshal(bytes, &c.impl)
}
func isValidName(name string) bool {
for _, c := range name {
if (c < 'a' || c > 'z') && (c < 'A' || c > 'Z') && (c < '0' || c > '9') && (c != '_') {
return false
}
}
return len(name) > 0 && len(name) <= 32
}
func unixNanoTime(s string) int64 {
t, err := time.Parse(timeFormat, s)
if err != nil {
panic(err)
}
return t.UnixNano()
}