<commit_before>From e4dd16aa68cdf1719abc42d0394ecf2b9f8f7643 Mon Sep 17 00:00:00 2001
From: <NAME> <<EMAIL>>
Date: Sat, 30 Jan 2021 02:55:00 -0800
Subject: [PATCH] io: fix nil ref exception against nil stdout

Signed-off-by: <NAME> <<EMAIL>>
---
 setup/setup_image.go       |  3 ++-
 shell/shell.go             | 13 ++++++++-----
 util/execcmd/hijackedio.go | 10 ++++++----
 util/execcmd/instream.go   |  2 +-
 util/execcmd/outstream.go  |  2 +-
 5 files changed, 18 insertions(+), 12 deletions(-)

diff --git a/setup/setup_image.go b/setup/setup_image.go
index 3c387fb..d796a02 100644
--- a/setup/setup_image.go
+++ b/setup/setup_image.go
@@ -137,7 +137,8 @@ func (i *ImageSetup) Execute() (exError error) {
 	if i.config.Pull != nil {
 		if i.config.Pull.Policy == config.ConfigPullPolicy_IfBuildFails {
 			postBuildPull = true
-		} else if !exists || i.config.Pull.Policy == config.ConfigPullPolicy_Always {
+		} else if (!exists && i.config.Pull.Policy == config.ConfigPullPolicy_IfNotPresent) ||
+			i.config.Pull.Policy == config.ConfigPullPolicy_Always {
 			err := i.pull(dockerClient)
 			if err == nil {
 				return nil
diff --git a/shell/shell.go b/shell/shell.go
index 30a71e1..53bbac2 100644
--- a/shell/shell.go
+++ b/shell/shell.go
@@ -96,7 +96,10 @@ func (s *Shell) Execute(
 	in := execcmd.NewInStream(os.Stdin, true)
 	out := execcmd.NewOutStream(os.Stdout)
 	errOut := execcmd.NewOutStream(os.Stderr)
-	useTty := in.IsTty()
+	inStrm, _ := in.(*execcmd.InStream)
+	useTty := inStrm != nil && inStrm.IsTty()
+	outStrm, _ := out.(*execcmd.OutStream)
+	// errStrm, _ := errOut.(*execcmd.OutStream)
 
 	configPath := path.Join(s.homeDir, config.UserConfigFile)
 	logPath := path.Join(s.homeDir, config.UserLogFile)
@@ -218,15 +221,15 @@ func (s *Shell) Execute(
 			Tty:          useTty,
 		}
 		if useTty {
-			in.SetRawMode()
-			defer in.RestoreTerminal()
+			inStrm.SetRawMode()
+			defer inStrm.RestoreTerminal()
 		}
 
 		errCh <- streamer.Stream(ctx)
 	}()
 
-	if useTty && in.IsTerminal() {
-		if err := MonitorTtySize(ctx, dockerClient, out, execCreate.ID, true); err != nil {
+	if useTty && inStrm != nil && inStrm.IsTerminal() && outStrm != nil {
+		if err := MonitorTtySize(ctx, dockerClient, outStrm, execCreate.ID, true); err != nil {
 			log.WithError(err).Error("Error monitoring TTY size")
 		}
 	}
diff --git a/util/execcmd/hijackedio.go b/util/execcmd/hijackedio.go
index b8c09be..94a0fec 100644
--- a/util/execcmd/hijackedio.go
+++ b/util/execcmd/hijackedio.go
@@ -65,10 +65,12 @@ func (h *HijackedIOStreamer) beginOutputStream() <-chan error {
 		var err error
 
 		// When TTY is ON, use regular copy
-		if h.OutputStream != nil && h.Tty {
-			_, err = io.Copy(h.OutputStream, h.Resp.Reader)
-		} else {
-			_, err = stdcopy.StdCopy(h.OutputStream, h.ErrorStream, h.Resp.Reader)
+		if h.OutputStream != nil {
+			if h.Tty {
+				_, err = io.Copy(h.OutputStream, h.Resp.Reader)
+			} else {
+				_, err = stdcopy.StdCopy(h.OutputStream, h.ErrorStream, h.Resp.Reader)
+			}
 		}
 
 		if err != nil {
diff --git a/util/execcmd/instream.go b/util/execcmd/instream.go
index 18d4a2c..4a88bb9 100644
--- a/util/execcmd/instream.go
+++ b/util/execcmd/instream.go
@@ -36,7 +36,7 @@ func (i *InStream) IsTty() bool {
 }
 
 // NewInStream returns a new InStream object from a ReadCloser<commit_msg>skiff/core: fix mistake in execstream refactoring<commit_after>From c3379dd7bd3e45a8d584321faa1a9a7283d86f0a Mon Sep 17 00:00:00 2001
From: <NAME> <<EMAIL>>
Date: Sat, 30 Jan 2021 02:55:00 -0800
Subject: [PATCH] io: fix nil ref exception against nil stdout

Signed-off-by: <NAME> <<EMAIL>>
---
 setup/setup_image.go       |  3 ++-
 shell/shell.go             | 13 ++++++++-----
 util/execcmd/container.go  |  3 ++-
 util/execcmd/hijackedio.go | 10 ++++++----
 util/execcmd/instream.go   |  2 +-
 util/execcmd/outstream.go  |  2 +-
 6 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/setup/setup_image.go b/setup/setup_image.go
index 3c387fb..d796a02 100644
--- a/setup/setup_image.go
+++ b/setup/setup_image.go
@@ -137,7 +137,8 @@ func (i *ImageSetup) Execute() (exError error) {
 	if i.config.Pull != nil {
 		if i.config.Pull.Policy == config.ConfigPullPolicy_IfBuildFails {
 			postBuildPull = true
-		} else if !exists || i.config.Pull.Policy == config.ConfigPullPolicy_Always {
+		} else if (!exists && i.config.Pull.Policy == config.ConfigPullPolicy_IfNotPresent) ||
+			i.config.Pull.Policy == config.ConfigPullPolicy_Always {
 			err := i.pull(dockerClient)
 			if err == nil {
 				return nil
diff --git a/shell/shell.go b/shell/shell.go
index 30a71e1..53bbac2 100644
--- a/shell/shell.go
+++ b/shell/shell.go
@@ -96,7 +96,10 @@ func (s *Shell) Execute(
 	in := execcmd.NewInStream(os.Stdin, true)
 	out := execcmd.NewOutStream(os.Stdout)
 	errOut := execcmd.NewOutStream(os.Stderr)
-	useTty := in.IsTty()
+	inStrm, _ := in.(*execcmd.InStream)
+	useTty := inStrm != nil && inStrm.IsTty()
+	outStrm, _ := out.(*execcmd.OutStream)
+	// errStrm, _ := errOut.(*execcmd.OutStream)
 
 	configPath := path.Join(s.homeDir, config.UserConfigFile)
 	logPath := path.Join(s.homeDir, config.UserLogFile)
@@ -218,15 +221,15 @@ func (s *Shell) Execute(
 			Tty:          useTty,
 		}
 		if useTty {
-			in.SetRawMode()
-			defer in.RestoreTerminal()
+			inStrm.SetRawMode()
+			defer inStrm.RestoreTerminal()
 		}
 
 		errCh <- streamer.Stream(ctx)
 	}()
 
-	if useTty && in.IsTerminal() {
-		if err := MonitorTtySize(ctx, dockerClient, out, execCreate.ID, true); err != nil {
+	if useTty && inStrm != nil && inStrm.IsTerminal() && outStrm != nil {
+		if err := MonitorTtySize(ctx, dockerClient, outStrm, execCreate.ID, true); err != nil {
 			log.WithError(err).Error("Error monitoring TTY size")
 		}
 	}
diff --git a/util/execcmd/container.go b/util/execcmd/container.go
index 42503de..04e2c69 100644
--- a/util/execcmd/container.go
+++ b/util/execcmd/container.go
@@ -19,7 +19,8 @@ func ExecCmdContainer(
 	in := NewInStream(stdIn, false)
 	out := NewOutStream(stdOut)
 	errOut := NewOutStream(stdErr)
-	useTty := in != nil && in.IsTty()
+	inStrm, _ := in.(*InStream)
+	useTty := inStrm != nil && inStrm.IsTty()
 
 	cmds := append([]string{cmd}, args...)
 	execCreate, err := dockerClient.ContainerExecCreate(ctx, containerID, types.ExecConfig{
diff --git a/util/execcmd/hijackedio.go b/util/execcmd/hijackedio.go
index b8c09be..94a0fec 100644
--- a/util/execcmd/hijackedio.go
+++ b/util/execcmd/hijackedio.go
@@ -65,10 +65,12 @@ func (h *HijackedIOStreamer) beginOutputStream() <-chan error {
 		var err error
 
 		// When TTY is ON, use regular copy
-		if h.OutputStream != nil && h.Tty {
-			_, err = io.Copy(h.OutputStream, h.Resp.Reader)
-		} else {
-			_, err = stdcopy.StdCopy(h.OutputStream, h.ErrorStream, h.Resp.Reader)
+		if h.OutputStream != nil {
+			if h.Tty {
+				_, err = io.Copy(h.OutputStream, h.Resp.Reader)
+			} else {
+				_, err = stdcopy.StdCopy(h.OutputStream, h.ErrorStream, h.Resp.Reader)
+			}
 		}
 
 		if err != nil {
diff --git a/util/execcmd/instream.go b/util/execcmd/instream.go
index 18d4a2c..4a88bb9 100644
--- a/util/execcmd/instream.go
+++ b/util/execcmd/instream.go
@@ -36,7 +36,7 @@ func (i *InStream) IsTty() bool {
 }
 
 // NewInStream returns a new InStream object from a ReadCloser
{'fraction_non_alphanumeric': 0.16825003442103814, 'fraction_numerical': 0.046399559410711826, 'mean_word_length': 3.383826191913096, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3529232', 'n_tokens_mistral': 3334, 'n_tokens_neox': 2919, 'n_words': 566}