2013-03-22 04:47:23 +04:00
|
|
|
package docker
|
2013-03-11 16:42:36 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os/exec"
|
|
|
|
)
|
|
|
|
|
2013-03-18 11:15:35 +04:00
|
|
|
type Archive io.Reader
|
|
|
|
|
2013-03-11 16:42:36 +04:00
|
|
|
type Compression uint32
|
|
|
|
|
|
|
|
const (
|
|
|
|
Uncompressed Compression = iota
|
|
|
|
Bzip2
|
|
|
|
Gzip
|
|
|
|
)
|
|
|
|
|
|
|
|
func (compression *Compression) Flag() string {
|
|
|
|
switch *compression {
|
|
|
|
case Bzip2:
|
|
|
|
return "j"
|
|
|
|
case Gzip:
|
|
|
|
return "z"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func Tar(path string, compression Compression) (io.Reader, error) {
|
|
|
|
cmd := exec.Command("bsdtar", "-f", "-", "-C", path, "-c"+compression.Flag(), ".")
|
|
|
|
return CmdStream(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Untar(archive io.Reader, path string) error {
|
|
|
|
cmd := exec.Command("bsdtar", "-f", "-", "-C", path, "-x")
|
|
|
|
cmd.Stdin = archive
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(err.Error() + ": " + string(output))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-03-30 00:18:59 +04:00
|
|
|
// CmdStream executes a command, and returns its stdout as a stream.
|
|
|
|
// If the command fails to run or doesn't complete successfully, an error
|
|
|
|
// will be returned, including anything written on stderr.
|
2013-03-11 16:42:36 +04:00
|
|
|
func CmdStream(cmd *exec.Cmd) (io.Reader, error) {
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
stderr, err := cmd.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pipeR, pipeW := io.Pipe()
|
2013-03-29 15:42:17 +04:00
|
|
|
errChan := make(chan []byte)
|
2013-03-30 00:26:02 +04:00
|
|
|
// Collect stderr, we will use it in case of an error
|
2013-03-11 16:42:36 +04:00
|
|
|
go func() {
|
|
|
|
errText, e := ioutil.ReadAll(stderr)
|
|
|
|
if e != nil {
|
|
|
|
errText = []byte("(...couldn't fetch stderr: " + e.Error() + ")")
|
|
|
|
}
|
2013-03-29 15:42:17 +04:00
|
|
|
errChan <- errText
|
|
|
|
}()
|
2013-03-30 00:26:02 +04:00
|
|
|
// Copy stdout to the returned pipe
|
2013-03-29 15:42:17 +04:00
|
|
|
go func() {
|
|
|
|
_, err := io.Copy(pipeW, stdout)
|
|
|
|
if err != nil {
|
|
|
|
pipeW.CloseWithError(err)
|
|
|
|
}
|
|
|
|
errText := <-errChan
|
2013-03-11 16:42:36 +04:00
|
|
|
if err := cmd.Wait(); err != nil {
|
|
|
|
pipeW.CloseWithError(errors.New(err.Error() + ": " + string(errText)))
|
|
|
|
} else {
|
|
|
|
pipeW.Close()
|
|
|
|
}
|
|
|
|
}()
|
2013-03-30 00:26:02 +04:00
|
|
|
// Run the command and return the pipe
|
2013-03-11 16:42:36 +04:00
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return pipeR, nil
|
|
|
|
}
|