maintner: simplify TailNetworkMutationSource API

Document the property that MutationStreamEvent.Mutation is non-nil if
MutationStreamEvent.Err is nil. This is consistent with documentation
inside the MutationStreamEvent struct, and simplifies API for callers.
They can use the Mutation field directly after checking the Err field.

Use a generic temporary directory name in TailNetworkMutationSource,
since it can be used by anything, not just the maintwatch command.

Use the new godata.Server constant in maintwatch.

Change-Id: I39dd6918b5f21173a8f47a7359f489cab511295e
Reviewed-on: https://go-review.googlesource.com/c/build/+/221637
Reviewed-by: Alexander Rakoczy <alex@golang.org>
diff --git a/maintner/maintner.go b/maintner/maintner.go
index 07f66ee..26a8bf6 100644
--- a/maintner/maintner.go
+++ b/maintner/maintner.go
@@ -208,9 +208,9 @@
 }
 
 // MutationStreamEvent represents one of three possible events while
-// reading mutations from disk. An event is either a mutation, an
-// error, or reaching the current end of the log. Only one of the
-// fields will be non-zero.
+// reading mutations from disk or another source.
+// An event is either a mutation, an error, or reaching the current
+// end of the log. Exactly one of the three fields will be non-zero.
 type MutationStreamEvent struct {
 	Mutation *maintpb.Mutation
 
diff --git a/maintner/maintwatch/maintwatch.go b/maintner/maintwatch/maintwatch.go
index bae729c..ce5dd1b 100644
--- a/maintner/maintwatch/maintwatch.go
+++ b/maintner/maintwatch/maintwatch.go
@@ -15,12 +15,15 @@
 
 	"github.com/golang/protobuf/proto"
 	"golang.org/x/build/maintner"
+	"golang.org/x/build/maintner/godata"
 )
 
-var server = flag.String("server", "https://maintner.golang.org/logs", "maintner server's /logs URL")
+var server = flag.String("server", godata.Server, "maintner server's /logs URL")
 
 func main() {
 	flag.Parse()
+
+	tm := proto.TextMarshaler{Compact: false}
 	for {
 		err := maintner.TailNetworkMutationSource(context.Background(), *server, func(e maintner.MutationStreamEvent) error {
 			if e.Err != nil {
@@ -28,14 +31,11 @@
 				time.Sleep(5 * time.Second)
 				return nil
 			}
-			if e.Mutation != nil {
-				fmt.Println()
-				tm := proto.TextMarshaler{Compact: false}
-				tm.Marshal(os.Stdout, e.Mutation)
-			}
+			fmt.Println()
+			tm.Marshal(os.Stdout, e.Mutation)
 			return nil
 		})
-		log.Printf("tail error: %v; restarting", err)
+		log.Printf("tail error: %v; restarting\n", err)
 		time.Sleep(time.Second)
 	}
 }
diff --git a/maintner/netsource.go b/maintner/netsource.go
index e95cf32..6c86032 100644
--- a/maintner/netsource.go
+++ b/maintner/netsource.go
@@ -43,13 +43,15 @@
 }
 
 // TailNetworkMutationSource calls fn for all new mutations added to the log on server.
+// Events with the End field set to true are not sent, so all events will
+// have exactly one of Mutation or Err fields set to a non-zero value.
 // It ignores prior events.
 // If the server is restarted and its history diverges,
 // TailNetworkMutationSource may return duplicate events. This therefore does not
 // return a MutationSource, so it can't be accidentally misused for important things.
 // TailNetworkMutationSource returns if fn returns an error, or if ctx expires.
 func TailNetworkMutationSource(ctx context.Context, server string, fn func(MutationStreamEvent) error) error {
-	td, err := ioutil.TempDir("", "maintnerwatch")
+	td, err := ioutil.TempDir("", "maintnertail")
 	if err != nil {
 		return err
 	}