cli: differentiate normal output vs info

This commit is contained in:
Michael Schurter
2018-03-30 11:42:11 -07:00
parent 55b98ee299
commit 7ff790bc7d
6 changed files with 28 additions and 17 deletions

View File

@@ -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():

View File

@@ -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
}

View File

@@ -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
},

View File

@@ -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:

View File

@@ -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))

10
main.go
View File

@@ -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),