diff --git a/command/alloc_status.go b/command/alloc_status.go index 86ef62d4c..c75e0a9f9 100644 --- a/command/alloc_status.go +++ b/command/alloc_status.go @@ -142,12 +142,8 @@ func (c *AllocStatusCommand) Run(args []string) int { c.Ui.Error(fmt.Sprintf("Identifier must contain at least two characters.")) return 1 } - if hyphens := strings.Count(allocID, "-"); (len(allocID)-hyphens)%2 == 1 { - // Identifiers must be of even length, so we strip off the last byte - // to provide a consistent user experience. - allocID = allocID[:len(allocID)-1] - } + allocID = sanatizeUUIDPrefix(allocID) allocs, _, err := client.Allocations().PrefixList(allocID) if err != nil { c.Ui.Error(fmt.Sprintf("Error querying allocation: %v", err)) diff --git a/command/eval_status.go b/command/eval_status.go index 1a687a0a2..a370a90ed 100644 --- a/command/eval_status.go +++ b/command/eval_status.go @@ -130,12 +130,8 @@ func (c *EvalStatusCommand) Run(args []string) int { c.Ui.Error(fmt.Sprintf("Identifier must contain at least two characters.")) return 1 } - if hyphens := strings.Count(evalID, "-"); (len(evalID)-hyphens)%2 == 1 { - // Identifiers must be of even length, so we strip off the last byte - // to provide a consistent user experience. - evalID = evalID[:len(evalID)-1] - } + evalID = sanatizeUUIDPrefix(evalID) evals, _, err := client.Evaluations().PrefixList(evalID) if err != nil { c.Ui.Error(fmt.Sprintf("Error querying evaluation: %v", err)) diff --git a/command/fs.go b/command/fs.go index 2b8111d9b..5fc3424ca 100644 --- a/command/fs.go +++ b/command/fs.go @@ -169,12 +169,8 @@ func (f *FSCommand) Run(args []string) int { f.Ui.Error(fmt.Sprintf("Alloc ID must contain at least two characters.")) return 1 } - if hyphens := strings.Count(allocID, "-"); (len(allocID)-hyphens)%2 == 1 { - // Identifiers must be of even length, so we strip off the last byte - // to provide a consistent user experience. - allocID = allocID[:len(allocID)-1] - } + allocID = sanatizeUUIDPrefix(allocID) allocs, _, err := client.Allocations().PrefixList(allocID) if err != nil { f.Ui.Error(fmt.Sprintf("Error querying allocation: %v", err)) diff --git a/command/helpers.go b/command/helpers.go index 3bdf99322..13f74ba9d 100644 --- a/command/helpers.go +++ b/command/helpers.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "os" "strconv" + "strings" "time" gg "github.com/hashicorp/go-getter" @@ -330,3 +331,13 @@ func mergeAutocompleteFlags(flags ...complete.Flags) complete.Flags { } return merged } + +// sanatizeUUIDPrefix is used to sanatize a UUID prefix. The returned result +// will be a truncated version of the prefix if the prefix would not be +// queriable. +func sanatizeUUIDPrefix(prefix string) string { + hyphens := strings.Count(prefix, "-") + length := len(prefix) - hyphens + remainder := length % 2 + return prefix[:len(prefix)-remainder] +} diff --git a/command/logs.go b/command/logs.go index d2797864d..e9839cb13 100644 --- a/command/logs.go +++ b/command/logs.go @@ -144,12 +144,8 @@ func (l *LogsCommand) Run(args []string) int { l.Ui.Error(fmt.Sprintf("Alloc ID must contain at least two characters.")) return 1 } - if hyphens := strings.Count(allocID, "-"); (len(allocID)-hyphens)%2 == 1 { - // Identifiers must be of even length, so we strip off the last byte - // to provide a consistent user experience. - allocID = allocID[:len(allocID)-1] - } + allocID = sanatizeUUIDPrefix(allocID) allocs, _, err := client.Allocations().PrefixList(allocID) if err != nil { l.Ui.Error(fmt.Sprintf("Error querying allocation: %v", err)) diff --git a/command/monitor.go b/command/monitor.go index 92642d867..1a4937a1c 100644 --- a/command/monitor.go +++ b/command/monitor.go @@ -194,12 +194,8 @@ func (m *monitor) monitor(evalID string, allowPrefix bool) int { m.ui.Error(fmt.Sprintf("Identifier must contain at least two characters.")) return 1 } - if hyphens := strings.Count(evalID, "-"); (len(evalID)-hyphens)%2 == 1 { - // Identifiers must be of even length, so we strip off the last byte - // to provide a consistent user experience. - evalID = evalID[:len(evalID)-1] - } + evalID = sanatizeUUIDPrefix(evalID) evals, _, err := m.client.Evaluations().PrefixList(evalID) if err != nil { m.ui.Error(fmt.Sprintf("Error reading evaluation: %s", err)) diff --git a/command/node_drain.go b/command/node_drain.go index 61ddd5c36..a9a87bfe1 100644 --- a/command/node_drain.go +++ b/command/node_drain.go @@ -117,12 +117,8 @@ func (c *NodeDrainCommand) Run(args []string) int { c.Ui.Error(fmt.Sprintf("Identifier must contain at least two characters.")) return 1 } - if hyphens := strings.Count(nodeID, "-"); (len(nodeID)-hyphens)%2 == 1 { - // Identifiers must be of even length, so we strip off the last byte - // to provide a consistent user experience. - nodeID = nodeID[:len(nodeID)-1] - } + nodeID = sanatizeUUIDPrefix(nodeID) nodes, _, err := client.Nodes().PrefixList(nodeID) if err != nil { c.Ui.Error(fmt.Sprintf("Error toggling drain mode: %s", err)) diff --git a/command/node_status.go b/command/node_status.go index 22cbcef19..a57a6bd9f 100644 --- a/command/node_status.go +++ b/command/node_status.go @@ -231,12 +231,8 @@ func (c *NodeStatusCommand) Run(args []string) int { c.Ui.Error(fmt.Sprintf("Identifier must contain at least two characters.")) return 1 } - if hyphens := strings.Count(nodeID, "-"); (len(nodeID)-hyphens)%2 == 1 { - // Identifiers must be of even length, so we strip off the last byte - // to provide a consistent user experience. - nodeID = nodeID[:len(nodeID)-1] - } + nodeID = sanatizeUUIDPrefix(nodeID) nodes, _, err := client.Nodes().PrefixList(nodeID) if err != nil { c.Ui.Error(fmt.Sprintf("Error querying node info: %s", err))