var templateUpdateShortDescription string
var templateUpdateCloudInitFile string

var templateUpdateCmd = &cobra.Command{
	Use:     "update",
	Aliases: []string{"save"},
	Short:   "Save a template",
	Example: "civo templates update --id ubuntu-70.04 --image-id b834b08c-bec6-11e5-b756-5cf9389be614",
	Long:    `Update a template with new parameters for people to use to build images from`,
	Run: func(cmd *cobra.Command, args []string) {
		if templateUpdateID == "" {
			fmt.Println("You need to specify a name with --id in order to update a template")
			os.Exit(-3)
		}

		params := api.TemplateParams{}
		res, err := api.TemplateFind(templateUpdateID)
		if err != nil {
			errorColor := color.New(color.FgRed, color.Bold).SprintFunc()
			fmt.Println(errorColor("An error occured:"), err.Error())
			return
		}
		params.ID = res.Path("id").Data().(string)
		params.Name = res.Path("name").Data().(string)
		params.ImageID = res.Path("image_id").Data().(string)
		params.ShortDescription = res.Path("short_description").Data().(string)
		params.Description = res.Path("description").Data().(string)
		params.CloudConfig = res.Path("cloud_config").Data().(string)

		if templateUpdateName != "" {
			params.Name = templateUpdateName
			fmt.Println("You need to specify a name with --name in order to create a template")
			os.Exit(-3)
		}
		if templateCreateImageId == "" {
			fmt.Println("You need to specify a Glance image ID with --image-id in order to create a template")
			os.Exit(-3)
		}
		if templateCreateShortDescription == "" {
			fmt.Println("You need to specify a one-line description with --short-description in order to create a template")
			os.Exit(-3)
		}

		params := api.TemplateParams{
			ID:               templateCreateID,
			Name:             templateCreateName,
			ImageID:          templateCreateImageId,
			ShortDescription: templateCreateShortDescription,
			Description:      templateCreateDescription,
		}
		if templateCreateCloudInitFile != "" {
			filename := strings.Replace(templateCreateCloudInitFile, "~", "$HOME", -1)
			filename = os.ExpandEnv(filename)
			buf, err := ioutil.ReadFile(filename)
			if err == nil {
				params.CloudConfig = string(buf)
			}
		}

		_, err := api.TemplateCreate(params)
		if err != nil {
			errorColor := color.New(color.FgRed, color.Bold).SprintFunc()