use helper

This commit is contained in:
Alex Dadgar 2017-08-29 10:04:02 -07:00
parent 8c1573642d
commit 52fa3396e7
8 changed files with 18 additions and 35 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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