Browse code

daemon/command: NewDaemonRunner: set both stdout and stderr

Make sure Cobra is configured with the streams we use, and use
Cobra's utilities to print the validation messsage.

While updating, also add a short comment outlining why we're using
STDERR, not STDOUT for this message.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2025/11/06 08:58:49
Showing 1 changed files
... ...
@@ -15,7 +15,7 @@ import (
15 15
 
16 16
 var honorXDG bool
17 17
 
18
-func newDaemonCommand(stderr io.Writer) (*cobra.Command, error) {
18
+func newDaemonCommand() (*cobra.Command, error) {
19 19
 	// FIXME(thaJeztah): config.New also looks up default binary-path, but this code is also executed when running "--version".
20 20
 	cfg, err := config.New()
21 21
 	if err != nil {
... ...
@@ -38,7 +38,10 @@ func newDaemonCommand(stderr io.Writer) (*cobra.Command, error) {
38 38
 			}
39 39
 			if opts.Validate {
40 40
 				// If config wasn't OK we wouldn't have made it this far.
41
-				_, _ = fmt.Fprintln(stderr, "configuration OK")
41
+				//
42
+				// We print this message on STDERR, not STDOUT, to align
43
+				// with other tools, such as "nginx -t" or "sshd -t".
44
+				cmd.PrintErrln("configuration OK")
42 45
 				return nil
43 46
 			}
44 47
 
... ...
@@ -104,11 +107,12 @@ func NewDaemonRunner(stdout, stderr io.Writer) (Runner, error) {
104 104
 
105 105
 	initLogging(stdout, stderr)
106 106
 
107
-	cmd, err := newDaemonCommand(stderr)
107
+	cmd, err := newDaemonCommand()
108 108
 	if err != nil {
109 109
 		return nil, err
110 110
 	}
111 111
 	cmd.SetOut(stdout)
112
+	cmd.SetErr(stderr)
112 113
 
113 114
 	return daemonRunner{cmd}, nil
114 115
 }