github.com/vchain-us/vcn@v0.9.11-0.20210921212052-a2484d23c0b3/pkg/cmd/internal/cli/checkversion.go (about)

     1  /*
     2   * Copyright (c) 2018-2020 vChain, Inc. All Rights Reserved.
     3   * This software is released under GPL3.
     4   * The full license information can be found under:
     5   * https://www.gnu.org/licenses/gpl-3.0.en.html
     6   *
     7   */
     8  
     9  package cli
    10  
    11  import (
    12  	"fmt"
    13  	"strings"
    14  	"time"
    15  
    16  	"github.com/fatih/color"
    17  	"github.com/vchain-us/vcn/pkg/meta"
    18  
    19  	"github.com/blang/semver"
    20  	"github.com/vchain-us/vcn/pkg/api"
    21  	"github.com/vchain-us/vcn/pkg/store"
    22  )
    23  
    24  // CheckVersion searches for newer vcn version and, if any, prints a message.
    25  func CheckVersion() {
    26  	if lastCheck := store.VersionCheckTime(); lastCheck != nil && lastCheck.After(time.Now().AddDate(0, 0, -1)) {
    27  		return
    28  	}
    29  	store.SetVersionCheckTime()
    30  
    31  	v, m, err := api.LatestCLIVersion()
    32  	if v == "" || err != nil {
    33  		return
    34  	}
    35  
    36  	curV, err := semver.Parse(strings.TrimPrefix(meta.Version(), "v"))
    37  	if err != nil {
    38  		return
    39  	}
    40  	latestV, err := semver.Parse(strings.TrimPrefix(v, "v"))
    41  	if err != nil {
    42  		return
    43  	}
    44  
    45  	if latestV.GT(curV) {
    46  		fmt.Println()
    47  		color.Set(meta.StyleAffordance())
    48  		fmt.Println("A newer version of vcn is available to download.")
    49  		color.Unset()
    50  		fmt.Printf(`		
    51  Your version: %s
    52  Latest version: %s
    53  %s
    54  `, meta.Version(), v, m)
    55  	}
    56  }