github.com/polydawn/docket@v0.5.4-0.20140630233848-90b70fb433da/dex/graphLoadRequest.go (about) 1 package dex; 2 3 import ( 4 "archive/tar" 5 "io" 6 "polydawn.net/hroot/crocker" 7 "polydawn.net/guitar/stream" 8 "sync" 9 ) 10 11 type GraphLoadRequest interface { 12 receive(path string) 13 } 14 15 type GraphLoadRequest_Tar struct { 16 Tarstream *tar.Writer 17 } 18 19 func (gr *GraphLoadRequest_Tar) receive(path string) { 20 // Use guitar to read the graph contents a tarstream 21 err := stream.ImportFromFilesystem(gr.Tarstream, path) 22 if err != nil { panic(err); } 23 } 24 25 type GraphLoadRequest_Image struct { 26 Dock *crocker.Dock 27 ImageName string 28 } 29 30 func (gr *GraphLoadRequest_Image) receive(path string) { 31 //Pipe for I/O, and a waitgroup to make async action blocking 32 importReader, importWriter := io.Pipe() 33 var wait sync.WaitGroup 34 wait.Add(1) 35 36 //Closure to run the docker import 37 go func() { 38 gr.Dock.Import(importReader, gr.ImageName, "latest") 39 wait.Done() 40 }() 41 42 //Run the guitar import 43 wat := GraphLoadRequest_Tar{ 44 Tarstream: tar.NewWriter(importWriter), 45 } // golang, you're bad. why can't i one-line this. 46 wat.receive(path); 47 importWriter.Close() 48 49 // wait for docker importing on the tar byte stream to return 50 wait.Wait() 51 } 52 53