-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
299 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package helper | ||
|
||
import ( | ||
"io" | ||
"log" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
type Command interface { | ||
Run() (string, error) | ||
Stdin() io.WriteCloser | ||
Kill() | ||
} | ||
|
||
type command struct { | ||
name string | ||
args []string | ||
under *exec.Cmd | ||
} | ||
|
||
func (c *command) Stdin() io.WriteCloser { | ||
pipe, e := c.under.StdinPipe() | ||
if e != nil { | ||
log.Fatal("error setting up stdin", e) | ||
} | ||
return pipe | ||
} | ||
|
||
func (c *command) Kill() { | ||
c.under.Process.Kill() | ||
} | ||
|
||
func (c *command) Run() (string, error) { | ||
log.Println("Running", c.name, c.args) | ||
out, err := c.under.CombinedOutput() | ||
return strings.TrimSpace(string(out)), err | ||
} | ||
|
||
func NewCommand(name string, args []string) Command { | ||
return &command{ | ||
name: name, | ||
args: args, | ||
under: exec.Command(name, args...), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package helper | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
) | ||
|
||
const ( | ||
bootTimeout = 30 | ||
) | ||
|
||
func NewDockerCommand(args ...string) Command { | ||
return NewCommand("docker", args) | ||
} | ||
|
||
func WaitForContainer(container string) error { | ||
filterPs := []string{"ps", "-aq", "--filter", "name=" + container} | ||
id, _ := NewDockerCommand(filterPs...).Run() | ||
for i := 0; ; i++ { | ||
if i > bootTimeout { | ||
return fmt.Errorf("Unable to start container after %d seconds", bootTimeout) | ||
} | ||
log.Println("Waiting for container to be available", container, id) | ||
time.Sleep(1 * time.Second) | ||
if id != "" { | ||
return nil | ||
} | ||
id, _ = NewDockerCommand(filterPs...).Run() | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package helper | ||
|
||
import ( | ||
"gopkg.in/src-d/go-git.v4" | ||
"io" | ||
"sort" | ||
"time" | ||
) | ||
|
||
type timedHash struct { | ||
When time.Time | ||
Hash string | ||
} | ||
|
||
type timeSlice []timedHash | ||
|
||
func (p timeSlice) Len() int { | ||
return len(p) | ||
} | ||
|
||
// Define compare | ||
func (p timeSlice) Less(i, j int) bool { | ||
return p[i].When.Before(p[j].When) | ||
} | ||
|
||
// Define swap over an array | ||
func (p timeSlice) Swap(i, j int) { | ||
p[i], p[j] = p[j], p[i] | ||
} | ||
|
||
func LatestCommitHash() (string, error) { | ||
r, err := git.PlainOpen(".") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
iter, e := r.CommitObjects() | ||
if e != nil { | ||
return "", e | ||
} | ||
defer iter.Close() | ||
var hashes timeSlice | ||
for { | ||
commit, err := iter.Next() | ||
if err != nil { | ||
if err == io.EOF { | ||
break | ||
} | ||
return "", err | ||
} | ||
hashes = append(hashes, timedHash{commit.Author.When, commit.Hash.String()}) | ||
} | ||
|
||
sort.Sort(hashes) | ||
return hashes[len(hashes)-1].Hash, nil | ||
} |
Oops, something went wrong.