doc: fix running Kubernetes test with CUE_UPDATE=1

This is a temporary sticking plaster that allows go test to be run on
the Kubernetes tutorial once again when CUE_UPDATE=1 is set. We allow
the running of go get under the same conditions as cue get.

The main work to fix up this tutorial is still captured in #824.

Change-Id: Ia05dc40fae96891145ff2da0ba2c0db6a908fd1e
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9182
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/doc/tutorial/kubernetes/tut_test.go b/doc/tutorial/kubernetes/tut_test.go
index b32fdcd..6ab9ec4 100644
--- a/doc/tutorial/kubernetes/tut_test.go
+++ b/doc/tutorial/kubernetes/tut_test.go
@@ -81,15 +81,8 @@
 		// The test environment won't work in all environments. We create
 		// a fake go.mod so that Go will find the module root. By default
 		// we won't set it.
-		if err := os.Chdir(dir); err != nil {
-			t.Fatal(err)
-		}
-		cmd := exec.Command("go", "mod", "init", "cuelang.org/dummy")
-		b, err := cmd.CombinedOutput()
-		logf(t, string(b))
-		if err != nil {
-			t.Fatal(err)
-		}
+		out := execute(t, dir, "go", "mod", "init", "cuelang.org/dummy")
+		logf(t, "%s", out)
 	} else {
 		// We only fetch new kubernetes files with when updating.
 		err := copy.Dir(load.GenPath("quick"), load.GenPath(dir))
@@ -198,6 +191,14 @@
 				if err != nil {
 					t.Fatal(err)
 				}
+			case strings.HasPrefix(cmd, "go "):
+				if !cuetest.UpdateGoldenFiles && strings.HasPrefix(cmd, "go get") {
+					// Don't fetch stuff in normal mode.
+					break
+				}
+
+				out := execute(t, wd, splitArgs(t, cmd)...)
+				logf(t, "%s", out)
 			}
 		}
 	}
@@ -312,6 +313,27 @@
 	Golden string
 }
 
+// execute executes the given command in the given directory
+func execute(t *testing.T, dir string, args ...string) string {
+	old, err := os.Getwd()
+	if err != nil {
+		t.Fatal(err)
+	}
+	if err = os.Chdir(dir); err != nil {
+		t.Fatal(err)
+	}
+	defer func() { os.Chdir(old) }()
+
+	logf(t, "Executing command: %s", strings.Join(args, " "))
+
+	cmd := exec.Command(args[0], args[1:]...)
+	out, err := cmd.CombinedOutput()
+	if err != nil {
+		t.Fatalf("failed to run [%v] in %s: %v\n%s", cmd, dir, err, out)
+	}
+	return string(out)
+}
+
 // run executes the given command in the given directory and reports any
 // errors comparing it to the gold standard.
 func run(t *testing.T, dir, command string, cfg *config) {