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).
This commit is contained in:
Mechiel Lukkien 2024-12-07 21:14:43 +01:00
parent 35af7e30a6
commit 2255ebcf11
No known key found for this signature in database
2 changed files with 37 additions and 1 deletions

2
doc.go
View file

@ -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 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. 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 The user or uid is optional, defaults to "mox", and is the user or uid/gid mox
will run as after initialization. will run as after initialization.

View file

@ -12,6 +12,7 @@ import (
"encoding/pem" "encoding/pem"
"errors" "errors"
"fmt" "fmt"
"io"
"log" "log"
"net" "net"
"net/url" "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 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. 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 The user or uid is optional, defaults to "mox", and is the user or uid/gid mox
will run as after initialization. will run as after initialization.
@ -105,6 +108,35 @@ output of "mox config describe-domains" and see the output of
c.Usage() 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 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 // We don't want to get a new user into trouble with half of the files
// after encountering an error. // 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) { xwritefile := func(path string, data []byte, perm os.FileMode) {