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