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