From 7ff790bc7dea4d04ea5781a8be7b67a718e8edd5 Mon Sep 17 00:00:00 2001 From: Michael Schurter Date: Fri, 30 Mar 2018 11:42:11 -0700 Subject: [PATCH] cli: differentiate normal output vs info --- api/nodes.go | 13 +++++++------ command/agent/command.go | 8 ++++---- command/commands.go | 4 ++-- command/node_drain.go | 2 ++ command/operator_keyring.go | 8 ++++---- main.go | 10 +++++++++- 6 files changed, 28 insertions(+), 17 deletions(-) diff --git a/api/nodes.go b/api/nodes.go index d3f8def54..802f9bc16 100644 --- a/api/nodes.go +++ b/api/nodes.go @@ -89,9 +89,10 @@ func (n *Nodes) UpdateDrain(nodeID string, spec *DrainSpec, markEligible bool, q type MonitorMsgLevel int const ( - MonitorMsgLevelInfo MonitorMsgLevel = 0 - MonitorMsgLevelWarn MonitorMsgLevel = 1 - MonitorMsgLevelError MonitorMsgLevel = 2 + MonitorMsgLevelNormal MonitorMsgLevel = 0 + MonitorMsgLevelInfo MonitorMsgLevel = 1 + MonitorMsgLevelWarn MonitorMsgLevel = 2 + MonitorMsgLevelError MonitorMsgLevel = 3 ) // MonitorMessage contains a message and log level. @@ -208,7 +209,7 @@ func (n *Nodes) monitorDrainNode(ctx context.Context, nodeID string, index uint6 } if node.DrainStrategy == nil { - msg := Messagef(MonitorMsgLevelWarn, "Node %q drain complete", nodeID) + msg := Messagef(MonitorMsgLevelInfo, "Node %q drain complete", nodeID) select { case nodeCh <- msg: case <-ctx.Done(): @@ -288,7 +289,7 @@ func (n *Nodes) monitorDrainAllocs(ctx context.Context, nodeID string, ignoreSys if msg != "" { select { - case allocCh <- Messagef(MonitorMsgLevelInfo, "Alloc %q %s", a.ID, msg): + case allocCh <- Messagef(MonitorMsgLevelNormal, "Alloc %q %s", a.ID, msg): case <-ctx.Done(): return } @@ -312,7 +313,7 @@ func (n *Nodes) monitorDrainAllocs(ctx context.Context, nodeID string, ignoreSys // Exit if all allocs are terminal if runningAllocs == 0 { - msg := Messagef(MonitorMsgLevelWarn, "All allocations on node %q have stopped.", nodeID) + msg := Messagef(MonitorMsgLevelInfo, "All allocations on node %q have stopped.", nodeID) select { case allocCh <- msg: case <-ctx.Done(): diff --git a/command/agent/command.go b/command/agent/command.go index fb5c489d7..59926777c 100644 --- a/command/agent/command.go +++ b/command/agent/command.go @@ -478,9 +478,9 @@ func (c *Command) Run(args []string) int { // Log config files if len(config.Files) > 0 { - c.Ui.Info(fmt.Sprintf("Loaded configuration from %s", strings.Join(config.Files, ", "))) + c.Ui.Output(fmt.Sprintf("Loaded configuration from %s", strings.Join(config.Files, ", "))) } else { - c.Ui.Info("No configuration files loaded") + c.Ui.Output("No configuration files loaded") } // Initialize the telemetry @@ -529,7 +529,7 @@ func (c *Command) Run(args []string) int { padding := 18 c.Ui.Output("Nomad agent configuration:\n") for _, k := range infoKeys { - c.Ui.Info(fmt.Sprintf( + c.Ui.Output(fmt.Sprintf( "%s%s: %s", strings.Repeat(" ", padding-len(k)), strings.Title(k), @@ -831,7 +831,7 @@ func (c *Command) startupJoin(config *Config) error { return err } - c.Ui.Info(fmt.Sprintf("Join completed. Synced with %d initial agents", n)) + c.Ui.Output(fmt.Sprintf("Join completed. Synced with %d initial agents", n)) return nil } diff --git a/command/commands.go b/command/commands.go index 63a050ad3..6eeba401a 100644 --- a/command/commands.go +++ b/command/commands.go @@ -48,7 +48,7 @@ func (c *DeprecatedCommand) warn() { // Commands returns the mapping of CLI commands for Nomad. The meta // parameter lets you set meta options for all commands. -func Commands(metaPtr *Meta) map[string]cli.CommandFactory { +func Commands(metaPtr *Meta, agentUi cli.Ui) map[string]cli.CommandFactory { if metaPtr == nil { metaPtr = new(Meta) } @@ -156,7 +156,7 @@ func Commands(metaPtr *Meta) map[string]cli.CommandFactory { "agent": func() (cli.Command, error) { return &agent.Command{ Version: version.GetVersion(), - Ui: meta.Ui, + Ui: agentUi, ShutdownCh: make(chan struct{}), }, nil }, diff --git a/command/node_drain.go b/command/node_drain.go index b362870df..9a85dce85 100644 --- a/command/node_drain.go +++ b/command/node_drain.go @@ -286,6 +286,8 @@ func (c *NodeDrainCommand) Run(args []string) int { outCh := client.Nodes().MonitorDrain(context.Background(), node.ID, meta.LastIndex, ignoreSystem) for msg := range outCh { switch msg.Level { + case api.MonitorMsgLevelInfo: + c.Ui.Info(fmt.Sprintf("%s: %s", formatTime(time.Now()), msg)) case api.MonitorMsgLevelWarn: c.Ui.Warn(fmt.Sprintf("%s: %s", formatTime(time.Now()), msg)) case api.MonitorMsgLevelError: diff --git a/command/operator_keyring.go b/command/operator_keyring.go index a74285870..5ef828178 100644 --- a/command/operator_keyring.go +++ b/command/operator_keyring.go @@ -114,7 +114,7 @@ func (c *OperatorKeyringCommand) Run(args []string) int { } if listKeys { - c.Ui.Info("Gathering installed encryption keys...") + c.Ui.Output("Gathering installed encryption keys...") r, err := client.Agent().ListKeys() if err != nil { c.Ui.Error(fmt.Sprintf("error: %s", err)) @@ -125,7 +125,7 @@ func (c *OperatorKeyringCommand) Run(args []string) int { } if installKey != "" { - c.Ui.Info("Installing new gossip encryption key...") + c.Ui.Output("Installing new gossip encryption key...") _, err := client.Agent().InstallKey(installKey) if err != nil { c.Ui.Error(fmt.Sprintf("error: %s", err)) @@ -135,7 +135,7 @@ func (c *OperatorKeyringCommand) Run(args []string) int { } if useKey != "" { - c.Ui.Info("Changing primary gossip encryption key...") + c.Ui.Output("Changing primary gossip encryption key...") _, err := client.Agent().UseKey(useKey) if err != nil { c.Ui.Error(fmt.Sprintf("error: %s", err)) @@ -145,7 +145,7 @@ func (c *OperatorKeyringCommand) Run(args []string) int { } if removeKey != "" { - c.Ui.Info("Removing gossip encryption key...") + c.Ui.Output("Removing gossip encryption key...") _, err := client.Agent().RemoveKey(removeKey) if err != nil { c.Ui.Error(fmt.Sprintf("error: %s", err)) diff --git a/main.go b/main.go index b563a7f02..d74bef167 100644 --- a/main.go +++ b/main.go @@ -92,16 +92,24 @@ func RunCustom(args []string) int { ErrorWriter: os.Stderr, } + // The Nomad agent never outputs color + agentUi := &cli.BasicUi{ + Reader: os.Stdin, + Writer: os.Stdout, + ErrorWriter: os.Stderr, + } + // Only use colored UI if stdout is a tty, and not disabled if isTerminal && color { metaPtr.Ui = &cli.ColoredUi{ ErrorColor: cli.UiColorRed, WarnColor: cli.UiColorYellow, + InfoColor: cli.UiColorGreen, Ui: metaPtr.Ui, } } - commands := command.Commands(metaPtr) + commands := command.Commands(metaPtr, agentUi) cli := &cli.CLI{ Name: "nomad", Version: version.GetVersion().FullVersionNumber(true),