github.com/diptanu/nomad@v0.5.7-0.20170516172507-d72e86cbe3d9/command/inspect.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/hashicorp/nomad/api" 8 ) 9 10 type InspectCommand struct { 11 Meta 12 } 13 14 func (c *InspectCommand) Help() string { 15 helpText := ` 16 Usage: nomad inspect [options] <job> 17 18 Inspect is used to see the specification of a submitted job. 19 20 General Options: 21 22 ` + generalOptionsUsage() + ` 23 24 Inspect Options: 25 26 -json 27 Output the evaluation in its JSON format. 28 29 -t 30 Format and display evaluation using a Go template. 31 ` 32 return strings.TrimSpace(helpText) 33 } 34 35 func (c *InspectCommand) Synopsis() string { 36 return "Inspect a submitted job" 37 } 38 39 func (c *InspectCommand) Run(args []string) int { 40 var ojson bool 41 var tmpl string 42 43 flags := c.Meta.FlagSet("inspect", FlagSetClient) 44 flags.Usage = func() { c.Ui.Output(c.Help()) } 45 flags.BoolVar(&ojson, "json", false, "") 46 flags.StringVar(&tmpl, "t", "", "") 47 48 if err := flags.Parse(args); err != nil { 49 return 1 50 } 51 args = flags.Args() 52 53 // Get the HTTP client 54 client, err := c.Meta.Client() 55 if err != nil { 56 c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 57 return 1 58 } 59 60 // If args not specified but output format is specified, format and output the jobs data list 61 if len(args) == 0 { 62 var format string 63 if ojson && len(tmpl) > 0 { 64 c.Ui.Error("Both -json and -t are not allowed") 65 return 1 66 } else if ojson { 67 format = "json" 68 } else if len(tmpl) > 0 { 69 format = "template" 70 } 71 if len(format) > 0 { 72 jobs, _, err := client.Jobs().List(nil) 73 if err != nil { 74 c.Ui.Error(fmt.Sprintf("Error querying jobs: %v", err)) 75 return 1 76 } 77 f, err := DataFormat(format, tmpl) 78 if err != nil { 79 c.Ui.Error(fmt.Sprintf("Error getting formatter: %s", err)) 80 return 1 81 } 82 // Return nothing if no jobs found 83 if len(jobs) == 0 { 84 return 0 85 } 86 87 out, err := f.TransformData(jobs) 88 if err != nil { 89 c.Ui.Error(fmt.Sprintf("Error formatting the data: %s", err)) 90 return 1 91 } 92 c.Ui.Output(out) 93 return 0 94 } 95 } 96 97 // Check that we got exactly one job 98 if len(args) != 1 { 99 c.Ui.Error(c.Help()) 100 return 1 101 } 102 jobID := args[0] 103 104 // Check if the job exists 105 jobs, _, err := client.Jobs().PrefixList(jobID) 106 if err != nil { 107 c.Ui.Error(fmt.Sprintf("Error inspecting job: %s", err)) 108 return 1 109 } 110 if len(jobs) == 0 { 111 c.Ui.Error(fmt.Sprintf("No job(s) with prefix or id %q found", jobID)) 112 return 1 113 } 114 if len(jobs) > 1 && strings.TrimSpace(jobID) != jobs[0].ID { 115 out := make([]string, len(jobs)+1) 116 out[0] = "ID|Type|Priority|Status" 117 for i, job := range jobs { 118 out[i+1] = fmt.Sprintf("%s|%s|%d|%s", 119 job.ID, 120 job.Type, 121 job.Priority, 122 job.Status) 123 } 124 c.Ui.Output(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", formatList(out))) 125 return 0 126 } 127 128 // Prefix lookup matched a single job 129 job, _, err := client.Jobs().Info(jobs[0].ID, nil) 130 if err != nil { 131 c.Ui.Error(fmt.Sprintf("Error inspecting job: %s", err)) 132 return 1 133 } 134 135 // If output format is specified, format and output the data 136 var format string 137 if ojson { 138 format = "json" 139 } else if len(tmpl) > 0 { 140 format = "template" 141 } 142 if len(format) > 0 { 143 f, err := DataFormat(format, tmpl) 144 if err != nil { 145 c.Ui.Error(fmt.Sprintf("Error getting formatter: %s", err)) 146 return 1 147 } 148 149 out, err := f.TransformData(job) 150 if err != nil { 151 c.Ui.Error(fmt.Sprintf("Error formatting the data: %s", err)) 152 return 1 153 } 154 c.Ui.Output(out) 155 return 0 156 } 157 158 // Print the contents of the job 159 req := api.RegisterJobRequest{Job: job} 160 f, err := DataFormat("json", "") 161 if err != nil { 162 c.Ui.Error(fmt.Sprintf("Error getting formatter: %s", err)) 163 return 1 164 } 165 166 out, err := f.TransformData(req) 167 if err != nil { 168 c.Ui.Error(fmt.Sprintf("Error formatting the data: %s", err)) 169 return 1 170 } 171 c.Ui.Output(out) 172 return 0 173 }