Skip to content

Commit 3d4f383

Browse files
committed
extract docker service from runner
1 parent eca1d11 commit 3d4f383

File tree

8 files changed

+299
-134
lines changed

8 files changed

+299
-134
lines changed

helper/command.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package helper
2+
3+
import (
4+
"io"
5+
"log"
6+
"os/exec"
7+
"strings"
8+
)
9+
10+
type Command interface {
11+
Run() (string, error)
12+
Stdin() io.WriteCloser
13+
Kill()
14+
}
15+
16+
type command struct {
17+
name string
18+
args []string
19+
under *exec.Cmd
20+
}
21+
22+
func (c *command) Stdin() io.WriteCloser {
23+
pipe, e := c.under.StdinPipe()
24+
if e != nil {
25+
log.Fatal("error setting up stdin", e)
26+
}
27+
return pipe
28+
}
29+
30+
func (c *command) Kill() {
31+
c.under.Process.Kill()
32+
}
33+
34+
func (c *command) Run() (string, error) {
35+
log.Println("Running", c.name, c.args)
36+
out, err := c.under.CombinedOutput()
37+
return strings.TrimSpace(string(out)), err
38+
}
39+
40+
func NewCommand(name string, args []string) Command {
41+
return &command{
42+
name: name,
43+
args: args,
44+
under: exec.Command(name, args...),
45+
}
46+
}

helper/docker_helper.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package helper
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"time"
7+
)
8+
9+
const (
10+
bootTimeout = 30
11+
)
12+
13+
func NewDockerCommand(args ...string) Command {
14+
return NewCommand("docker", args)
15+
}
16+
17+
func WaitForContainer(container string) error {
18+
filterPs := []string{"ps", "-aq", "--filter", "name=" + container}
19+
id, _ := NewDockerCommand(filterPs...).Run()
20+
for i := 0; ; i++ {
21+
if i > bootTimeout {
22+
return fmt.Errorf("Unable to start container after %d seconds", bootTimeout)
23+
}
24+
log.Println("Waiting for container to be available", container, id)
25+
time.Sleep(1 * time.Second)
26+
if id != "" {
27+
return nil
28+
}
29+
id, _ = NewDockerCommand(filterPs...).Run()
30+
}
31+
return nil
32+
}

runner/file.go renamed to helper/file.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package runner
1+
package helper
22

33
import "os"
44

5-
func exists(path string) bool {
5+
func Exists(path string) bool {
66
_, err := os.Stat(path)
77
if err == nil {
88
return true
@@ -13,7 +13,7 @@ func exists(path string) bool {
1313
return true
1414
}
1515

16-
func isFile(path string) bool {
16+
func IsFile(path string) bool {
1717
info, err := os.Stat(path)
1818
if err != nil {
1919
return false

helper/git.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package helper
2+
3+
import (
4+
"gopkg.in/src-d/go-git.v4"
5+
"io"
6+
"sort"
7+
"time"
8+
)
9+
10+
type timedHash struct {
11+
When time.Time
12+
Hash string
13+
}
14+
15+
type timeSlice []timedHash
16+
17+
func (p timeSlice) Len() int {
18+
return len(p)
19+
}
20+
21+
// Define compare
22+
func (p timeSlice) Less(i, j int) bool {
23+
return p[i].When.Before(p[j].When)
24+
}
25+
26+
// Define swap over an array
27+
func (p timeSlice) Swap(i, j int) {
28+
p[i], p[j] = p[j], p[i]
29+
}
30+
31+
func LatestCommitHash() (string, error) {
32+
r, err := git.PlainOpen(".")
33+
if err != nil {
34+
return "", err
35+
}
36+
37+
iter, e := r.CommitObjects()
38+
if e != nil {
39+
return "", e
40+
}
41+
defer iter.Close()
42+
var hashes timeSlice
43+
for {
44+
commit, err := iter.Next()
45+
if err != nil {
46+
if err == io.EOF {
47+
break
48+
}
49+
return "", err
50+
}
51+
hashes = append(hashes, timedHash{commit.Author.When, commit.Hash.String()})
52+
}
53+
54+
sort.Sort(hashes)
55+
return hashes[len(hashes)-1].Hash, nil
56+
}

0 commit comments

Comments
 (0)