diff --git a/command/fs_list.go b/command/fs_list.go index e0187482c..1b3880816 100644 --- a/command/fs_list.go +++ b/command/fs_list.go @@ -49,10 +49,51 @@ func (f *FSListCommand) Run(args []string) int { return 1 } + // Query the allocation info alloc, _, err := client.Allocations().Info(allocID, nil) if err != nil { - f.Ui.Error(fmt.Sprintf("Error getting alloc: %v", err)) - return 1 + if len(allocID) == 1 { + f.Ui.Error(fmt.Sprintf("Identifier must contain at least two characters.")) + return 1 + } + if len(allocID)%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] + } + + allocs, _, err := client.Allocations().PrefixList(allocID) + if err != nil { + f.Ui.Error(fmt.Sprintf("Error querying allocation: %v", err)) + return 1 + } + if len(allocs) == 0 { + f.Ui.Error(fmt.Sprintf("No allocation(s) with prefix or id %q found", allocID)) + return 1 + } + if len(allocs) > 1 { + // Format the allocs + out := make([]string, len(allocs)+1) + out[0] = "ID|Eval ID|Job ID|Task Group|Desired Status|Client Status" + for i, alloc := range allocs { + out[i+1] = fmt.Sprintf("%s|%s|%s|%s|%s|%s", + alloc.ID, + alloc.EvalID, + alloc.JobID, + alloc.TaskGroup, + alloc.DesiredStatus, + alloc.ClientStatus, + ) + } + f.Ui.Output(fmt.Sprintf("Prefix matched multiple allocations\n\n%s", formatList(out))) + return 0 + } + // Prefix lookup matched a single allocation + alloc, _, err = client.Allocations().Info(allocs[0].ID, nil) + if err != nil { + f.Ui.Error(fmt.Sprintf("Error querying allocation: %s", err)) + return 1 + } } files, _, err := client.AllocFS().List(alloc, path, nil) diff --git a/command/fs_readat.go b/command/fs_readat.go index 29b3e21dc..d5990505b 100644 --- a/command/fs_readat.go +++ b/command/fs_readat.go @@ -50,10 +50,51 @@ func (f *FSCatCommand) Run(args []string) int { return 1 } + // Query the allocation info alloc, _, err := client.Allocations().Info(allocID, nil) if err != nil { - f.Ui.Error(fmt.Sprintf("Error getting alloc: %v", err)) - return 1 + if len(allocID) == 1 { + f.Ui.Error(fmt.Sprintf("Identifier must contain at least two characters.")) + return 1 + } + if len(allocID)%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] + } + + allocs, _, err := client.Allocations().PrefixList(allocID) + if err != nil { + f.Ui.Error(fmt.Sprintf("Error querying allocation: %v", err)) + return 1 + } + if len(allocs) == 0 { + f.Ui.Error(fmt.Sprintf("No allocation(s) with prefix or id %q found", allocID)) + return 1 + } + if len(allocs) > 1 { + // Format the allocs + out := make([]string, len(allocs)+1) + out[0] = "ID|Eval ID|Job ID|Task Group|Desired Status|Client Status" + for i, alloc := range allocs { + out[i+1] = fmt.Sprintf("%s|%s|%s|%s|%s|%s", + alloc.ID, + alloc.EvalID, + alloc.JobID, + alloc.TaskGroup, + alloc.DesiredStatus, + alloc.ClientStatus, + ) + } + f.Ui.Output(fmt.Sprintf("Prefix matched multiple allocations\n\n%s", formatList(out))) + return 0 + } + // Prefix lookup matched a single allocation + alloc, _, err = client.Allocations().Info(allocs[0].ID, nil) + if err != nil { + f.Ui.Error(fmt.Sprintf("Error querying allocation: %s", err)) + return 1 + } } file, _, err := client.AllocFS().Stat(alloc, path, nil) diff --git a/command/fs_stat.go b/command/fs_stat.go index 2fc22b37e..5029f8e19 100644 --- a/command/fs_stat.go +++ b/command/fs_stat.go @@ -49,10 +49,51 @@ func (f *FSStatCommand) Run(args []string) int { return 1 } + // Query the allocation info alloc, _, err := client.Allocations().Info(allocID, nil) if err != nil { - f.Ui.Error(fmt.Sprintf("Error getting alloc: %v", err)) - return 1 + if len(allocID) == 1 { + f.Ui.Error(fmt.Sprintf("Identifier must contain at least two characters.")) + return 1 + } + if len(allocID)%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] + } + + allocs, _, err := client.Allocations().PrefixList(allocID) + if err != nil { + f.Ui.Error(fmt.Sprintf("Error querying allocation: %v", err)) + return 1 + } + if len(allocs) == 0 { + f.Ui.Error(fmt.Sprintf("No allocation(s) with prefix or id %q found", allocID)) + return 1 + } + if len(allocs) > 1 { + // Format the allocs + out := make([]string, len(allocs)+1) + out[0] = "ID|Eval ID|Job ID|Task Group|Desired Status|Client Status" + for i, alloc := range allocs { + out[i+1] = fmt.Sprintf("%s|%s|%s|%s|%s|%s", + alloc.ID, + alloc.EvalID, + alloc.JobID, + alloc.TaskGroup, + alloc.DesiredStatus, + alloc.ClientStatus, + ) + } + f.Ui.Output(fmt.Sprintf("Prefix matched multiple allocations\n\n%s", formatList(out))) + return 0 + } + // Prefix lookup matched a single allocation + alloc, _, err = client.Allocations().Info(allocs[0].ID, nil) + if err != nil { + f.Ui.Error(fmt.Sprintf("Error querying allocation: %s", err)) + return 1 + } } file, _, err := client.AllocFS().Stat(alloc, path, nil)