docker save: Do not save to a terminal.
| ... | ... |
@@ -5,6 +5,7 @@ import ( |
| 5 | 5 |
"bytes" |
| 6 | 6 |
"encoding/base64" |
| 7 | 7 |
"encoding/json" |
| 8 |
+ "errors" |
|
| 8 | 9 |
"fmt" |
| 9 | 10 |
"io" |
| 10 | 11 |
"io/ioutil" |
| ... | ... |
@@ -2411,7 +2412,10 @@ func (cli *DockerCli) CmdSave(args ...string) error {
|
| 2411 | 2411 |
if err != nil {
|
| 2412 | 2412 |
return err |
| 2413 | 2413 |
} |
| 2414 |
+ } else if cli.isTerminalOut {
|
|
| 2415 |
+ return errors.New("Cowardly refusing to save to a terminal. Use the -o flag or redirect.")
|
|
| 2414 | 2416 |
} |
| 2417 |
+ |
|
| 2415 | 2418 |
if len(cmd.Args()) == 1 {
|
| 2416 | 2419 |
image := cmd.Arg(0) |
| 2417 | 2420 |
if err := cli.stream("GET", "/images/"+image+"/get", nil, output, nil); err != nil {
|
| ... | ... |
@@ -1,6 +1,7 @@ |
| 1 | 1 |
package main |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 |
+ "bytes" |
|
| 4 | 5 |
"fmt" |
| 5 | 6 |
"io/ioutil" |
| 6 | 7 |
"os" |
| ... | ... |
@@ -8,6 +9,8 @@ import ( |
| 8 | 8 |
"path/filepath" |
| 9 | 9 |
"reflect" |
| 10 | 10 |
"testing" |
| 11 |
+ |
|
| 12 |
+ "github.com/docker/docker/vendor/src/github.com/kr/pty" |
|
| 11 | 13 |
) |
| 12 | 14 |
|
| 13 | 15 |
// save a repo and try to load it using stdout |
| ... | ... |
@@ -70,6 +73,34 @@ func TestSaveAndLoadRepoStdout(t *testing.T) {
|
| 70 | 70 |
|
| 71 | 71 |
logDone("save - save a repo using stdout")
|
| 72 | 72 |
logDone("load - load a repo using stdout")
|
| 73 |
+ |
|
| 74 |
+ pty, tty, err := pty.Open() |
|
| 75 |
+ if err != nil {
|
|
| 76 |
+ t.Fatalf("Could not open pty: %v", err)
|
|
| 77 |
+ } |
|
| 78 |
+ cmd := exec.Command(dockerBinary, "save", repoName) |
|
| 79 |
+ cmd.Stdin = tty |
|
| 80 |
+ cmd.Stdout = tty |
|
| 81 |
+ cmd.Stderr = tty |
|
| 82 |
+ if err := cmd.Start(); err != nil {
|
|
| 83 |
+ t.Fatalf("start err: %v", err)
|
|
| 84 |
+ } |
|
| 85 |
+ if err := cmd.Wait(); err == nil {
|
|
| 86 |
+ t.Fatal("did not break writing to a TTY")
|
|
| 87 |
+ } |
|
| 88 |
+ |
|
| 89 |
+ buf := make([]byte, 1024) |
|
| 90 |
+ |
|
| 91 |
+ n, err := pty.Read(buf) |
|
| 92 |
+ if err != nil {
|
|
| 93 |
+ t.Fatal("could not read tty output")
|
|
| 94 |
+ } |
|
| 95 |
+ |
|
| 96 |
+ if !bytes.Contains(buf[:n], []byte("Cowardly refusing")) {
|
|
| 97 |
+ t.Fatal("help output is not being yielded", out)
|
|
| 98 |
+ } |
|
| 99 |
+ |
|
| 100 |
+ logDone("save - do not save to a tty")
|
|
| 73 | 101 |
} |
| 74 | 102 |
|
| 75 | 103 |
func TestSaveSingleTag(t *testing.T) {
|