github.com/kimor79/packer@v0.8.7-0.20151221212622-d507b18eb4cf/command/version.go (about)

     1  package command
     2  
     3  //go:generate go run ../scripts/generate-plugins.go
     4  
     5  import (
     6  	"bytes"
     7  	"fmt"
     8  )
     9  
    10  // VersionCommand is a Command implementation prints the version.
    11  type VersionCommand struct {
    12  	Meta
    13  
    14  	Revision          string
    15  	Version           string
    16  	VersionPrerelease string
    17  	CheckFunc         VersionCheckFunc
    18  }
    19  
    20  // VersionCheckFunc is the callback called by the Version command to
    21  // check if there is a new version of Packer.
    22  type VersionCheckFunc func() (VersionCheckInfo, error)
    23  
    24  // VersionCheckInfo is the return value for the VersionCheckFunc callback
    25  // and tells the Version command information about the latest version
    26  // of Packer.
    27  type VersionCheckInfo struct {
    28  	Outdated bool
    29  	Latest   string
    30  	Alerts   []string
    31  }
    32  
    33  func (c *VersionCommand) Help() string {
    34  	return ""
    35  }
    36  
    37  func (c *VersionCommand) Run(args []string) int {
    38  	c.Ui.Machine("version", c.Version)
    39  	c.Ui.Machine("version-prelease", c.VersionPrerelease)
    40  	c.Ui.Machine("version-commit", c.Revision)
    41  
    42  	var versionString bytes.Buffer
    43  	fmt.Fprintf(&versionString, "Packer v%s", c.Version)
    44  	if c.VersionPrerelease != "" {
    45  		fmt.Fprintf(&versionString, ".%s", c.VersionPrerelease)
    46  
    47  		if c.Revision != "" {
    48  			fmt.Fprintf(&versionString, " (%s)", c.Revision)
    49  		}
    50  	}
    51  
    52  	c.Ui.Say(versionString.String())
    53  
    54  	// If we have a version check function, then let's check for
    55  	// the latest version as well.
    56  	if c.CheckFunc != nil {
    57  		// Separate the prior output with a newline
    58  		c.Ui.Say("")
    59  
    60  		// Check the latest version
    61  		info, err := c.CheckFunc()
    62  		if err != nil {
    63  			c.Ui.Error(fmt.Sprintf(
    64  				"Error checking latest version: %s", err))
    65  		}
    66  		if info.Outdated {
    67  			c.Ui.Say(fmt.Sprintf(
    68  				"Your version of Packer is out of date! The latest version\n"+
    69  					"is %s. You can update by downloading from www.packer.io",
    70  				info.Latest))
    71  		}
    72  	}
    73  
    74  	return 0
    75  }
    76  
    77  func (c *VersionCommand) Synopsis() string {
    78  	return "Prints the Packer version"
    79  }