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