2015-09-15 23:44:38 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2016-04-11 22:20:49 +00:00
|
|
|
"fmt"
|
2016-01-27 22:56:17 +00:00
|
|
|
"time"
|
|
|
|
|
2016-04-11 22:20:49 +00:00
|
|
|
"github.com/hashicorp/nomad/api"
|
2015-09-15 23:44:38 +00:00
|
|
|
"github.com/ryanuber/columnize"
|
|
|
|
)
|
|
|
|
|
|
|
|
// formatKV takes a set of strings and formats them into properly
|
|
|
|
// aligned k = v pairs using the columnize library.
|
|
|
|
func formatKV(in []string) string {
|
|
|
|
columnConf := columnize.DefaultConfig()
|
2015-09-27 21:04:53 +00:00
|
|
|
columnConf.Empty = "<none>"
|
2015-09-15 23:44:38 +00:00
|
|
|
columnConf.Glue = " = "
|
|
|
|
return columnize.Format(in, columnConf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// formatList takes a set of strings and formats them into properly
|
|
|
|
// aligned output, replacing any blank fields with a placeholder
|
|
|
|
// for awk-ability.
|
|
|
|
func formatList(in []string) string {
|
|
|
|
columnConf := columnize.DefaultConfig()
|
|
|
|
columnConf.Empty = "<none>"
|
|
|
|
return columnize.Format(in, columnConf)
|
|
|
|
}
|
2016-01-27 18:42:10 +00:00
|
|
|
|
2016-03-11 00:20:51 +00:00
|
|
|
// formatListWithSpaces takes a set of strings and formats them into properly
|
|
|
|
// aligned output. It should be used sparingly since it doesn't replace empty
|
|
|
|
// values and hence not awk/sed friendly
|
|
|
|
func formatListWithSpaces(in []string) string {
|
|
|
|
columnConf := columnize.DefaultConfig()
|
|
|
|
return columnize.Format(in, columnConf)
|
|
|
|
}
|
|
|
|
|
2016-01-27 18:42:10 +00:00
|
|
|
// Limits the length of the string.
|
|
|
|
func limit(s string, length int) string {
|
|
|
|
if len(s) < length {
|
2016-03-11 03:02:39 +00:00
|
|
|
return s
|
2016-01-27 18:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s[:length]
|
|
|
|
}
|
2016-01-27 22:56:17 +00:00
|
|
|
|
|
|
|
// formatTime formats the time to string based on RFC822
|
|
|
|
func formatTime(t time.Time) string {
|
2016-02-02 01:41:47 +00:00
|
|
|
return t.Format("02/01/06 15:04:05 MST")
|
2016-01-27 22:56:17 +00:00
|
|
|
}
|
2016-04-11 22:20:49 +00:00
|
|
|
|
|
|
|
// getLocalNodeID returns the node ID of the local Nomad Client and an error if
|
|
|
|
// it couldn't be determined or the Agent is not running in Client mode.
|
|
|
|
func getLocalNodeID(client *api.Client) (string, error) {
|
|
|
|
info, err := client.Agent().Self()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error querying agent info: %s", err)
|
|
|
|
}
|
|
|
|
var stats map[string]interface{}
|
|
|
|
stats, _ = info["stats"]
|
|
|
|
clientStats, ok := stats["client"].(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("Nomad not running in client mode")
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeID, ok := clientStats["node_id"].(string)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("Failed to determine node ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodeID, nil
|
|
|
|
}
|