github.com/hashicorp/packer@v1.14.3/command/version.go (about)

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