github.com/devcamcar/cli@v0.0.0-20181107134215-706a05759d18/config/version.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"path"
     7  	"strings"
     8  	"time"
     9  )
    10  
    11  // Version of Fn CLI
    12  var Version = "0.5.29"
    13  
    14  func GetVersion(versionType string) string {
    15  	base := "https://github.com/fnproject/cli/releases"
    16  	url := ""
    17  	c := http.Client{}
    18  	c.Timeout = time.Second * 3
    19  	c.CheckRedirect = func(req *http.Request, via []*http.Request) error {
    20  		url = req.URL.String()
    21  		return nil
    22  	}
    23  	r, err := c.Get(fmt.Sprintf("%s/latest", base))
    24  	if err != nil {
    25  		return ""
    26  	}
    27  	defer r.Body.Close()
    28  	if !strings.Contains(url, base) {
    29  		return ""
    30  	}
    31  	if versionType == "current" {
    32  		return GetCurrentVersion(url)
    33  	}
    34  
    35  	return GetLatestVersion(url)
    36  }
    37  
    38  func GetLatestVersion(url string) string {
    39  	if path.Base(url) != Version {
    40  		return fmt.Sprintf("Client version: %s is not latest: %s", Version, path.Base(url))
    41  	}
    42  	return "Client version is latest version: " + path.Base(url)
    43  }
    44  
    45  func GetCurrentVersion(url string) string {
    46  	return path.Base(url)
    47  }