cmd/rmplaysnippet: account for local datastore host env var

If DATASTORE_EMULATOR_HOST is set, datastore.NewClient will use
it to connect to a locally-running datastore instance. Clear it
before creating the client and set a 30-second timeout on the
context so that the process doesn't appear to hang indefinitely
when the env var is set.

Change-Id: I605ee699a55f4537c174a5667c92044936f10d74
Reviewed-on: https://go-review.googlesource.com/c/build/+/185142
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
diff --git a/cmd/rmplaysnippet/main.go b/cmd/rmplaysnippet/main.go
index ae21f58..775af77 100644
--- a/cmd/rmplaysnippet/main.go
+++ b/cmd/rmplaysnippet/main.go
@@ -3,7 +3,8 @@
 // license that can be found in the LICENSE file.
 
 // The rmplaysnippet binary removes a code snippet from play.golang.org given its URL
-// or ID.
+// or ID. It will always connect to the production datastore instance, ignoring any
+// local value of DATASTORE_EMULATOR_HOST.
 package main
 
 import (
@@ -11,6 +12,7 @@
 	"fmt"
 	"os"
 	"strings"
+	"time"
 
 	"cloud.google.com/go/datastore"
 	"golang.org/x/build/buildenv"
@@ -41,13 +43,21 @@
 	}
 
 	buildenv.CheckUserCredentials()
-	ctx := context.Background()
+
+	// Don't attempt to connect to a locally-running datastore instance.
+	if err := os.Setenv("DATASTORE_EMULATOR_HOST", ""); err != nil {
+		fmt.Fprintf(os.Stderr, "Failed to clear env var DATASTORE_EMULATOR_HOST: %v\n", err)
+		os.Exit(1)
+	}
+	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
+	defer cancel()
 	client, err := datastore.NewClient(ctx, "golang-org")
 	if err != nil {
 		fmt.Fprintf(os.Stderr, "Failed to create Datastore client: %v\n", err)
 		os.Exit(1)
 	}
 	k := datastore.NameKey("Snippet", snippetID, nil)
+	fmt.Printf("Deleting snippet %q ...\n", snippetID)
 	if err := client.Delete(ctx, k); err != nil {
 		fmt.Fprintf(os.Stderr, "Unable to delete Snippet with ID %q: %v\n", snippetID, err)
 		fmt.Fprintf(os.Stderr, "rmplaysnippet requires Application Default Credentials.\n")