1
0
mirror of https://github.com/docker/cli.git synced 2026-01-18 08:21:31 +03:00

Moved more utilities to docker/future

Upstream-commit: 401dd3d8e02f720e5e3b2a0f337a57af41fa29c4
Component: engine
This commit is contained in:
Solomon Hykes
2013-01-25 11:23:18 -08:00
parent 9dc7d71f11
commit 5ddc825ec6
3 changed files with 34 additions and 34 deletions

View File

@@ -286,7 +286,7 @@ func (docker *Docker) addContainer(name string, source string, size uint) *Conta
size = fake.RandomContainerSize()
}
c := &Container{
Id: fake.RandomId(),
Id: future.RandomId(),
Name: name,
Created: time.Now(),
Source: source,
@@ -389,7 +389,7 @@ func startCommand(cmd *exec.Cmd, interactive bool) (io.WriteCloser, io.ReadClose
func main() {
fake.Seed()
future.Seed()
flag.Parse()
docker := New()
go func() {
@@ -450,14 +450,6 @@ func (docker *Docker) CmdWeb(stdin io.ReadCloser, stdout io.Writer, args ...stri
}
func Go(f func() error) chan error {
ch := make(chan error)
go func() {
ch <- f()
}()
return ch
}
type Docker struct {
containers map[string]*Container
containersByName map[string]*ByDate
@@ -488,26 +480,26 @@ func (c *Container) Run(command string, args []string, stdin io.ReadCloser, stdo
// Reset logs
c.stdoutLog.Reset()
c.stdinLog.Reset()
c.Running = true
cmd := exec.Command(c.Cmd, c.Args...)
cmd_stdin, cmd_stdout, err := startCommand(cmd, true)
// ADD FAKE RANDOM CHANGES
c.FilesChanged = fake.RandomFilesChanged()
c.BytesChanged = fake.RandomBytesChanged()
cmd_stdin, cmd_stdout, err := startCommand(cmd, tty)
if err != nil {
return err
}
copy_out := Go(func() error {
c.Running = true
// ADD FAKE RANDOM CHANGES
c.FilesChanged = fake.RandomFilesChanged()
c.BytesChanged = fake.RandomBytesChanged()
copy_out := future.Go(func() error {
_, err := io.Copy(io.MultiWriter(stdout, c.stdoutLog), cmd_stdout)
return err
})
Go(func() error {
future.Go(func() error {
_, err := io.Copy(io.MultiWriter(cmd_stdin, c.stdinLog), stdin)
cmd_stdin.Close()
stdin.Close()
return err
})
wait := Go(func() error {
wait := future.Go(func() error {
err := cmd.Wait()
c.Running = false
return err

View File

@@ -1,22 +1,12 @@
package fake
import (
"github.com/dotcloud/docker/future"
"bytes"
"math/rand"
"time"
"io"
"archive/tar"
"fmt"
)
func Seed() {
rand.Seed(time.Now().UTC().UnixNano())
}
func randomBytes() io.Reader {
return bytes.NewBuffer([]byte(fmt.Sprintf("%x", rand.Int())))
}
func FakeTar() (io.Reader, error) {
content := []byte("Hello world!\n")
@@ -46,12 +36,6 @@ func WriteFakeTar(dst io.Writer) error {
}
func RandomId() string {
id, _ := future.ComputeId(randomBytes()) // can't fail
return id
}
func RandomBytesChanged() uint {
return uint(rand.Int31n(24 * 1024 * 1024))
}

View File

@@ -5,8 +5,14 @@ import (
"io"
"fmt"
"time"
"bytes"
"math/rand"
)
func Seed() {
rand.Seed(time.Now().UTC().UnixNano())
}
func ComputeId(content io.Reader) (string, error) {
h := sha256.New()
if _, err := io.Copy(h, content); err != nil {
@@ -37,3 +43,21 @@ func HumanDuration(d time.Duration) string {
}
return fmt.Sprintf("%d years", d.Hours() / 24 / 365)
}
func randomBytes() io.Reader {
return bytes.NewBuffer([]byte(fmt.Sprintf("%x", rand.Int())))
}
func RandomId() string {
id, _ := ComputeId(randomBytes()) // can't fail
return id
}
func Go(f func() error) chan error {
ch := make(chan error)
go func() {
ch <- f()
}()
return ch
}