github.com/kotovmak/go-admin@v1.1.1/adm/helper.go (about)

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"strings"
     9  	"time"
    10  
    11  	"github.com/kotovmak/go-admin/modules/utils"
    12  	"github.com/mgutz/ansi"
    13  
    14  	"github.com/kotovmak/go-admin/modules/system"
    15  )
    16  
    17  func cliInfo() {
    18  	fmt.Println("GoAdmin CLI " + system.Version() + compareVersion(system.Version()))
    19  	fmt.Println()
    20  }
    21  
    22  func checkError(err error) {
    23  	if err != nil {
    24  		panic(err)
    25  	}
    26  }
    27  
    28  func getLatestVersion() string {
    29  	http.DefaultClient.Timeout = 3 * time.Second
    30  	res, err := http.Get("https://goproxy.cn/github.com/!go!admin!group/go-admin/@v/list")
    31  
    32  	if err != nil || res.Body == nil {
    33  		return ""
    34  	}
    35  
    36  	defer func() {
    37  		_ = res.Body.Close()
    38  	}()
    39  
    40  	if res.StatusCode != http.StatusOK {
    41  		return ""
    42  	}
    43  
    44  	body, err := ioutil.ReadAll(res.Body)
    45  
    46  	if err != nil || body == nil {
    47  		return ""
    48  	}
    49  
    50  	versionsArr := strings.Split(string(body), "\n")
    51  
    52  	return versionsArr[len(versionsArr)-1]
    53  }
    54  
    55  func compareVersion(srcVersion string) string {
    56  	toCompareVersion := getLatestVersion()
    57  	if utils.CompareVersion(srcVersion, toCompareVersion) {
    58  		return ", the latest version is " + toCompareVersion + " now."
    59  	}
    60  	return ""
    61  }
    62  
    63  func printSuccessInfo(msg string) {
    64  	fmt.Println()
    65  	fmt.Println()
    66  	fmt.Println(ansi.Color(getWord(msg), "green"))
    67  	fmt.Println()
    68  	fmt.Println()
    69  }
    70  
    71  func newError(msg string) error {
    72  	return errors.New(getWord(msg))
    73  }