github.phpd.cn/hashicorp/packer@v1.3.2/command/version.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/packer/version"
     7  )
     8  
     9  // VersionCommand is a Command implementation prints the version.
    10  type VersionCommand struct {
    11  	Meta
    12  
    13  	CheckFunc VersionCheckFunc
    14  }
    15  
    16  // VersionCheckFunc is the callback called by the Version command to
    17  // check if there is a new version of Packer.
    18  type VersionCheckFunc func() (VersionCheckInfo, error)
    19  
    20  // VersionCheckInfo is the return value for the VersionCheckFunc callback
    21  // and tells the Version command information about the latest version
    22  // of Packer.
    23  type VersionCheckInfo struct {
    24  	Outdated bool
    25  	Latest   string
    26  	Alerts   []string
    27  }
    28  
    29  func (c *VersionCommand) Help() string {
    30  	return "Prints the Packer version, and checks for new release."
    31  }
    32  
    33  func (c *VersionCommand) Run(args []string) int {
    34  	c.Ui.Machine("version", version.Version)
    35  	c.Ui.Machine("version-prelease", version.VersionPrerelease)
    36  	c.Ui.Machine("version-commit", version.GitCommit)
    37  
    38  	c.Ui.Say(fmt.Sprintf("Packer v%s", version.FormattedVersion()))
    39  
    40  	// If we have a version check function, then let's check for
    41  	// the latest version as well.
    42  	if c.CheckFunc != nil {
    43  
    44  		// Check the latest version
    45  		info, err := c.CheckFunc()
    46  		if err != nil {
    47  			c.Ui.Error(fmt.Sprintf(
    48  				"\nError checking latest version: %s", err))
    49  		}
    50  		if info.Outdated {
    51  			c.Ui.Say(fmt.Sprintf(
    52  				"\nYour version of Packer is out of date! The latest version\n"+
    53  					"is %s. You can update by downloading from www.packer.io/downloads.html",
    54  				info.Latest))
    55  		}
    56  	}
    57  
    58  	return 0
    59  }
    60  
    61  func (c *VersionCommand) Synopsis() string {
    62  	return "Prints the Packer version"
    63  }