This repository has been archived by the owner on Apr 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execution.go
64 lines (53 loc) · 1.67 KB
/
execution.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
package ci
import (
"bytes"
"encoding/hex"
"fmt"
"time"
"github.com/ericaro/sbr/format"
)
//execution is a tool to run any execution (refresh or build), and keep: information about it.
type execution struct {
version [20]byte // sha1 of all sha1 when the build has started, or ended (if the execution should change it.)
start, end time.Time // keep track of when
errcode int // execution error code
result *bytes.Buffer // console output
}
// whenever an execution starts it updates its start time
func (x *execution) IsRunning() bool { return x.start.After(x.end) }
//Marshal converts execution state into a "format" message.
func (x *execution) Marshal() *format.Execution { return x.Status(true) }
//Status return a format.Execution status,
// withResult true will also serialize the buffer's content.
func (x *execution) Status(withResult bool) *format.Execution {
version := fmt.Sprintf("%x", x.version)
start, end := x.start.Unix(), x.end.Unix()
code := int32(x.errcode)
f := &format.Execution{
Version: &version,
Start: &start,
End: &end,
Errcode: &code,
}
if withResult {
result := x.result.String()
f.Result = &result
}
return f
}
//Unmarshal restore an execution instance from the format.Exception message.
func (x *execution) Unmarshal(f *format.Execution) error {
b, err := hex.DecodeString(f.GetVersion())
if err != nil {
return err
}
if len(b) != 20 {
return fmt.Errorf("invalid sha1 length %d instead of 20", len(b))
}
copy(x.version[:], b)
x.start = time.Unix(f.GetStart(), 0)
x.end = time.Unix(f.GetEnd(), 0)
x.errcode = int(f.GetErrcode())
x.result = bytes.NewBufferString(f.GetResult())
return nil
}