code: func (c *cipdSubcommand) writeJSONOutput(result interface{}, err error) error {
  	// -json-output flag wasn't specified.
  	if c.jsonOutput == "" {
  		return err
  	}
  
  	// Prepare the body of the output file.
  	var body struct {
  		Error  string      `json:"error,omitempty"`
  		Result interface{} `json:"result,omitempty"`
  	}
  	if err != nil {
  		body.Error = err.Error()
  	}
  	body.Result = result
  	out, e := json.MarshalIndent(&body, "", "  ")
  	if e != nil {
  		if err == nil {
  			err = e
  		} else {
  			fmt.Fprintf(os.Stderr, "Failed to serialize JSON output: %s\n", e)
  		}
  		return err
  	}
  
  	e = ioutil.WriteFile(c.jsonOutput, out, 0666)
  	if e != nil {
  		if err == nil {
  			err = e
  		} else {
  			fmt.Fprintf(os.Stderr, "Failed write JSON output to %s: %s\n", c.jsonOutput, e)
  		}
  		return err
  	}
  
  	return err
  }
docs: // writeJSONOutput writes result to JSON output file. It returns original error
  // if it is non-nil.