github.com/spirius/terraform@v0.10.0-beta2.0.20170714185654-87b2c0cf8fea/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 Terraform.
    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 Terraform.
    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  	var versionString bytes.Buffer
    37  	args, err := c.Meta.process(args, false)
    38  	if err != nil {
    39  		return 1
    40  	}
    41  
    42  	fmt.Fprintf(&versionString, "Terraform v%s", c.Version)
    43  	if c.VersionPrerelease != "" {
    44  		fmt.Fprintf(&versionString, "-%s", c.VersionPrerelease)
    45  
    46  		if c.Revision != "" {
    47  			fmt.Fprintf(&versionString, " (%s)", c.Revision)
    48  		}
    49  	}
    50  
    51  	c.Ui.Output(versionString.String())
    52  
    53  	// If we have a version check function, then let's check for
    54  	// the latest version as well.
    55  	if c.CheckFunc != nil {
    56  		// Separate the prior output with a newline
    57  		c.Ui.Output("")
    58  
    59  		// Check the latest version
    60  		info, err := c.CheckFunc()
    61  		if err != nil {
    62  			c.Ui.Error(fmt.Sprintf(
    63  				"Error checking latest version: %s", err))
    64  		}
    65  		if info.Outdated {
    66  			c.Ui.Output(fmt.Sprintf(
    67  				"Your version of Terraform is out of date! The latest version\n"+
    68  					"is %s. You can update by downloading from www.terraform.io",
    69  				info.Latest))
    70  		}
    71  	}
    72  
    73  	return 0
    74  }
    75  
    76  func (c *VersionCommand) Synopsis() string {
    77  	return "Prints the Terraform version"
    78  }