From cec8bc88a96b2318d4c3cf1e7164fe389ca5649d Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 2 Jun 2021 17:26:50 -0400 Subject: [PATCH] cmd: remove unnecessary GatedUi The intent of this struct was to prevent non-json output to stdout. With the previous cleanup, this can now be done by simply changing the stdout stream to io.Discard. This is just one example of why passing around io.Writers for the streams is better than the UI interface. --- command/agent/agent.go | 51 ++++++++++++++---------------------------- 1 file changed, 17 insertions(+), 34 deletions(-) diff --git a/command/agent/agent.go b/command/agent/agent.go index ef65a025e7..a0a3601c64 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -4,6 +4,7 @@ import ( "context" "flag" "fmt" + "io" "os" "os/signal" "path/filepath" @@ -178,34 +179,33 @@ func (c *cmd) run(args []string) int { } config := bd.RuntimeConfig - - // Setup gate to check if we should output CLI information - cli := GatedUi{ - JSONoutput: config.Logging.LogJSON, - ui: ui, + if config.Logging.LogJSON { + // Hide all non-error output when JSON logging is enabled. + ui.Ui = &cli.BasicUI{ + BasicUi: mcli.BasicUi{ErrorWriter: c.ui.Stderr(), Writer: io.Discard}, + } } - // Create the agent - cli.output("Starting Consul agent...") + ui.Output("Starting Consul agent...") segment := config.SegmentName if config.ServerMode { segment = "" } - cli.info(fmt.Sprintf(" Version: '%s'", c.versionHuman)) - cli.info(fmt.Sprintf(" Node ID: '%s'", config.NodeID)) - cli.info(fmt.Sprintf(" Node name: '%s'", config.NodeName)) - cli.info(fmt.Sprintf(" Datacenter: '%s' (Segment: '%s')", config.Datacenter, segment)) - cli.info(fmt.Sprintf(" Server: %v (Bootstrap: %v)", config.ServerMode, config.Bootstrap)) - cli.info(fmt.Sprintf(" Client Addr: %v (HTTP: %d, HTTPS: %d, gRPC: %d, DNS: %d)", config.ClientAddrs, + ui.Info(fmt.Sprintf(" Version: '%s'", c.versionHuman)) + ui.Info(fmt.Sprintf(" Node ID: '%s'", config.NodeID)) + ui.Info(fmt.Sprintf(" Node name: '%s'", config.NodeName)) + ui.Info(fmt.Sprintf(" Datacenter: '%s' (Segment: '%s')", config.Datacenter, segment)) + ui.Info(fmt.Sprintf(" Server: %v (Bootstrap: %v)", config.ServerMode, config.Bootstrap)) + ui.Info(fmt.Sprintf(" Client Addr: %v (HTTP: %d, HTTPS: %d, gRPC: %d, DNS: %d)", config.ClientAddrs, config.HTTPPort, config.HTTPSPort, config.GRPCPort, config.DNSPort)) - cli.info(fmt.Sprintf(" Cluster Addr: %v (LAN: %d, WAN: %d)", config.AdvertiseAddrLAN, + ui.Info(fmt.Sprintf(" Cluster Addr: %v (LAN: %d, WAN: %d)", config.AdvertiseAddrLAN, config.SerfPortLAN, config.SerfPortWAN)) - cli.info(fmt.Sprintf(" Encrypt: Gossip: %v, TLS-Outgoing: %v, TLS-Incoming: %v, Auto-Encrypt-TLS: %t", + ui.Info(fmt.Sprintf(" Encrypt: Gossip: %v, TLS-Outgoing: %v, TLS-Incoming: %v, Auto-Encrypt-TLS: %t", config.EncryptKey != "", config.VerifyOutgoing, config.VerifyIncoming, config.AutoEncryptTLS || config.AutoEncryptAllowTLS)) // Enable log streaming - cli.output("") - cli.output("Log data will now stream in as it occurs:\n") + ui.Output("") + ui.Output("Log data will now stream in as it occurs:\n") logGate.Flush() // wait for signal @@ -341,23 +341,6 @@ func (c *cmd) run(args []string) int { } } -type GatedUi struct { - JSONoutput bool - ui mcli.Ui -} - -func (g *GatedUi) output(s string) { - if !g.JSONoutput { - g.ui.Output(s) - } -} - -func (g *GatedUi) info(s string) { - if !g.JSONoutput { - g.ui.Info(s) - } -} - func (c *cmd) Synopsis() string { return synopsis }