From 2255ebcf11869c936c2c49db7527e12a5cf8003e Mon Sep 17 00:00:00 2001 From: Mechiel Lukkien Date: Sat, 7 Dec 2024 21:14:43 +0100 Subject: [PATCH] quickstart: write all output to a file "quickstart.log" for later reference quite some output is printed. you could remember to tee it all to a file. but that's probably often realized only after having run the quickstart. you can also copy/paste it all from the terminal, but that's sometimes annoying to do. writing to a file is more helpful to users. this has been requested a few times in the past on irc/matrix (i forgot who). --- doc.go | 2 ++ quickstart.go | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/doc.go b/doc.go index cb6d1b5..0761186 100644 --- a/doc.go +++ b/doc.go @@ -146,6 +146,8 @@ Quickstart writes configuration files, prints initial admin and account passwords, DNS records you should create. If you run it on Linux it writes a systemd service file and prints commands to enable and start mox as service. +All output is written to quickstart.log for later reference. + The user or uid is optional, defaults to "mox", and is the user or uid/gid mox will run as after initialization. diff --git a/quickstart.go b/quickstart.go index 4dc7e26..c82aed2 100644 --- a/quickstart.go +++ b/quickstart.go @@ -12,6 +12,7 @@ import ( "encoding/pem" "errors" "fmt" + "io" "log" "net" "net/url" @@ -67,6 +68,8 @@ Quickstart writes configuration files, prints initial admin and account passwords, DNS records you should create. If you run it on Linux it writes a systemd service file and prints commands to enable and start mox as service. +All output is written to quickstart.log for later reference. + The user or uid is optional, defaults to "mox", and is the user or uid/gid mox will run as after initialization. @@ -105,6 +108,35 @@ output of "mox config describe-domains" and see the output of c.Usage() } + // Write all output to quickstart.log. + logfile, err := os.Create("quickstart.log") + xcheckf(err, "creating quickstart.log") + + origStdout := os.Stdout + origStderr := os.Stderr + piper, pipew, err := os.Pipe() + xcheckf(err, "creating pipe for logging to logfile") + pipec := make(chan struct{}) + go func() { + io.Copy(io.MultiWriter(origStdout, logfile), piper) + close(pipec) + }() + // A single pipe, so writes to stdout and stderr don't get interleaved. + os.Stdout = pipew + os.Stderr = pipew + logClose := func() { + pipew.Close() + <-pipec + os.Stdout = origStdout + os.Stderr = origStderr + err := logfile.Close() + xcheckf(err, "closing quickstart.log") + } + defer logClose() + log.SetOutput(os.Stdout) + fmt.Printf("(output is also written to quickstart.log)\n\n") + defer fmt.Printf("\n(output is also written to quickstart.log)\n") + // We take care to cleanup created files when we error out. // We don't want to get a new user into trouble with half of the files // after encountering an error. @@ -121,7 +153,9 @@ output of "mox config describe-domains" and see the output of } } - log.Fatalf(format, args...) + log.Printf(format, args...) + logClose() + os.Exit(1) } xwritefile := func(path string, data []byte, perm os.FileMode) {