Merge pull request #1052 from iverberk/b-fix-panic-on-fs-job-parameter

Fixes a panic when fs commands with a job parameter return zero allocations
This commit is contained in:
Alex Dadgar 2016-04-08 11:27:20 -07:00
commit 42924b9037
3 changed files with 9 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package command
import (
"fmt"
"math/rand"
"time"
@ -29,6 +30,12 @@ func (f *FSCommand) Run(args []string) int {
func getRandomJobAlloc(client *api.Client, jobID string) (string, error) {
var runningAllocs []*api.AllocationListStub
allocs, _, err := client.Jobs().Allocations(jobID, nil)
// Check that the job actually has allocations
if len(allocs) == 0 {
return "", fmt.Errorf("job %q doesn't exist or it has no allocations", jobID)
}
for _, v := range allocs {
if v.ClientStatus == "running" {
runningAllocs = append(runningAllocs, v)

View File

@ -71,6 +71,7 @@ func (f *FSCatCommand) Run(args []string) int {
allocID, err = getRandomJobAlloc(client, args[0])
if err != nil {
f.Ui.Error(fmt.Sprintf("Error querying API: %v", err))
return 1
}
}

View File

@ -76,6 +76,7 @@ func (f *FSStatCommand) Run(args []string) int {
allocID, err = getRandomJobAlloc(client, args[0])
if err != nil {
f.Ui.Error(fmt.Sprintf("Error querying API: %v", err))
return 1
}
}