github.com/dkerwin/nomad@v0.3.3-0.20160525181927-74554135514b/command/inspect.go (about) 1 package command 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "strings" 7 8 "github.com/hashicorp/nomad/api" 9 ) 10 11 type InspectCommand struct { 12 Meta 13 } 14 15 func (c *InspectCommand) Help() string { 16 helpText := ` 17 Usage: nomad inspect [options] <job> 18 19 Inspect is used to see the specification of a submitted job. 20 21 General Options: 22 23 ` + generalOptionsUsage() 24 25 return strings.TrimSpace(helpText) 26 } 27 28 func (c *InspectCommand) Synopsis() string { 29 return "Inspect a submitted job" 30 } 31 32 func (c *InspectCommand) Run(args []string) int { 33 flags := c.Meta.FlagSet("inspect", FlagSetClient) 34 flags.Usage = func() { c.Ui.Output(c.Help()) } 35 36 if err := flags.Parse(args); err != nil { 37 return 1 38 } 39 40 // Check that we got exactly one job 41 args = flags.Args() 42 if len(args) != 1 { 43 c.Ui.Error(c.Help()) 44 return 1 45 } 46 jobID := args[0] 47 48 // Get the HTTP client 49 client, err := c.Meta.Client() 50 if err != nil { 51 c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 52 return 1 53 } 54 55 // Check if the job exists 56 jobs, _, err := client.Jobs().PrefixList(jobID) 57 if err != nil { 58 c.Ui.Error(fmt.Sprintf("Error inspecting job: %s", err)) 59 return 1 60 } 61 if len(jobs) == 0 { 62 c.Ui.Error(fmt.Sprintf("No job(s) with prefix or id %q found", jobID)) 63 return 1 64 } 65 if len(jobs) > 1 && strings.TrimSpace(jobID) != jobs[0].ID { 66 out := make([]string, len(jobs)+1) 67 out[0] = "ID|Type|Priority|Status" 68 for i, job := range jobs { 69 out[i+1] = fmt.Sprintf("%s|%s|%d|%s", 70 job.ID, 71 job.Type, 72 job.Priority, 73 job.Status) 74 } 75 c.Ui.Output(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", formatList(out))) 76 return 0 77 } 78 79 // Prefix lookup matched a single job 80 job, _, err := client.Jobs().Info(jobs[0].ID, nil) 81 if err != nil { 82 c.Ui.Error(fmt.Sprintf("Error inspecting job: %s", err)) 83 return 1 84 } 85 86 // Print the contents of the job 87 req := api.RegisterJobRequest{job} 88 buf, err := json.MarshalIndent(req, "", " ") 89 if err != nil { 90 c.Ui.Error(fmt.Sprintf("Error converting job: %s", err)) 91 return 1 92 } 93 94 c.Ui.Output(string(buf)) 95 return 0 96 }