style: nfctl new version print
This commit is contained in:
		| @@ -1,3 +1,3 @@ | ||||
| package version | ||||
|  | ||||
| const Version = "v24.07.14-r2" | ||||
| const Version = "v24.07.14-r3" | ||||
|   | ||||
| @@ -1,38 +1,39 @@ | ||||
| package version | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"crypto/tls" | ||||
| 	"fmt" | ||||
| 	"github.com/fatih/color" | ||||
| 	"github.com/go-resty/resty/v2" | ||||
| 	"github.com/jedib0t/go-pretty/v6/table" | ||||
| 	"github.com/loveuer/nf/nft/log" | ||||
| 	"github.com/savioxavier/termlink" | ||||
| 	"io" | ||||
| 	"net/http" | ||||
| 	"strings" | ||||
| 	"time" | ||||
| ) | ||||
|  | ||||
| var ( | ||||
| 	client = resty.New().SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) | ||||
| 	uri    = "https://raw.gitcode.com/loveuer/nf/raw/master/nft/nfctl/version/var.go" | ||||
| 	prefix = "const Version = " | ||||
| ) | ||||
|  | ||||
| func UpgradePrint(newVersion string) { | ||||
| 	t := table.NewWriter() | ||||
| 	t.AppendRows([]table.Row{ | ||||
| 		{color.GreenString("New Version Found: %s", newVersion)}, | ||||
| 		{color.CyanString("Upgrade it with: [go install github.com/loveuer/nf/nft/nfctl@master]")}, | ||||
| 		{fmt.Sprint("Or Download by: ")}, | ||||
| 		{color.CyanString(termlink.Link("Releases", "https://github.com/loveuer/nf/releases"))}, | ||||
| 		{color.CyanString(termlink.Link("Releases", "https://gitcode.com/loveuer/nf/releases"))}, | ||||
| 	}) | ||||
|  | ||||
| 	fmt.Println(t.Render()) | ||||
| 	fmt.Printf(`+----------------------------------------------------------------------+ | ||||
| |          🎉 🎉 🎉 %s 🎉 🎉 🎉          | | ||||
| | %s | | ||||
| | Or Download by:                                                      | | ||||
| | %s                    | | ||||
| | %s                   | | ||||
| +----------------------------------------------------------------------+ | ||||
| `, | ||||
| 		color.GreenString("New Version Found: %s", newVersion), | ||||
| 		color.CyanString("Upgrade it with: [go install github.com/loveuer/nf/nft/nfctl@master]"), | ||||
| 		color.CyanString(termlink.Link("Releases", "https://github.com/loveuer/nf/releases")), | ||||
| 		color.CyanString(termlink.Link("Releases", "https://gitcode.com/loveuer/nf/releases")), | ||||
| 	) | ||||
| } | ||||
|  | ||||
| func Check(printUpgradable bool, printNoNeedUpgrade bool, timeout ...int) string { | ||||
| func Check(printUpgradable bool, printNoNeedUpgrade bool, timeouts ...int) string { | ||||
| 	var ( | ||||
| 		v string | ||||
| 	) | ||||
| @@ -51,21 +52,34 @@ func Check(printUpgradable bool, printNoNeedUpgrade bool, timeout ...int) string | ||||
| 		} | ||||
| 	}() | ||||
|  | ||||
| 	ctx, _ := context.WithTimeout(context.Background(), time.Duration(30)*time.Second) | ||||
| 	if len(timeout) > 0 && timeout[0] > 0 { | ||||
| 		ctx, _ = context.WithTimeout(context.Background(), time.Duration(timeout[0])*time.Second) | ||||
| 	timeout := time.Duration(30) * time.Second | ||||
| 	if len(timeouts) > 0 && timeouts[0] > 0 { | ||||
| 		timeout = time.Duration(timeouts[0]) * time.Second | ||||
| 	} | ||||
|  | ||||
| 	resp, err := client.R().SetContext(ctx). | ||||
| 		Get(uri) | ||||
| 	req, _ := http.NewRequest(http.MethodGet, uri, nil) | ||||
| 	resp, err := (&http.Client{ | ||||
| 		Timeout: timeout, | ||||
| 		Transport: &http.Transport{ | ||||
| 			TLSClientConfig: &tls.Config{ | ||||
| 				InsecureSkipVerify: true, | ||||
| 			}, | ||||
| 		}, | ||||
| 	}).Do(req) | ||||
| 	if err != nil { | ||||
| 		log.Debug("[Check] http get[%s] err: %v", uri, err.Error()) | ||||
| 		return "" | ||||
| 	} | ||||
| 	defer resp.Body.Close() | ||||
|  | ||||
| 	log.Debug("[Check] http get[%s] body:\n%s", uri, resp.String()) | ||||
| 	content, err := io.ReadAll(resp.Body) | ||||
| 	if err != nil { | ||||
| 		log.Debug("[Check] http read all body err: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	for _, line := range strings.Split(resp.String(), "\n") { | ||||
| 	log.Debug("[Check] http get[%s] body:\n%s", uri, string(content)) | ||||
|  | ||||
| 	for _, line := range strings.Split(string(content), "\n") { | ||||
| 		log.Debug("[Check] version.go line: %s", line) | ||||
| 		if strings.HasPrefix(line, prefix) { | ||||
| 			may := strings.TrimPrefix(line, prefix) | ||||
|   | ||||
| @@ -6,11 +6,12 @@ import ( | ||||
| ) | ||||
|  | ||||
| func TestUpgradePrint(t *testing.T) { | ||||
| 	log.SetLogLevel(log.LogLevelDebug) | ||||
| 	UpgradePrint("v24.07.14-r5") | ||||
| } | ||||
|  | ||||
| func TestCheck(t *testing.T) { | ||||
| 	log.SetLogLevel(log.LogLevelDebug) | ||||
| 	v := Check(15) | ||||
| 	v := Check(true, true, 1) | ||||
| 	t.Logf("got version: %s", v) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user