chore: nfctl version, check cmd
This commit is contained in:
		
							
								
								
									
										16
									
								
								nft/nfctl/cmd/check.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								nft/nfctl/cmd/check.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"github.com/loveuer/nf/nft/nfctl/version" | ||||
| 	"github.com/spf13/cobra" | ||||
| ) | ||||
|  | ||||
| var ( | ||||
| 	checkCmd = &cobra.Command{ | ||||
| 		Use:   "check", | ||||
| 		Short: "nfctl new version check", | ||||
| 		Run: func(cmd *cobra.Command, args []string) { | ||||
| 			version.Check(true, true, 30) | ||||
| 		}, | ||||
| 	} | ||||
| ) | ||||
| @@ -1,19 +1,30 @@ | ||||
| package cmd | ||||
|  | ||||
| import "github.com/spf13/cobra" | ||||
| import ( | ||||
| 	"github.com/loveuer/nf/nft/log" | ||||
| 	"github.com/loveuer/nf/nft/nfctl/opt" | ||||
| 	"github.com/spf13/cobra" | ||||
| ) | ||||
|  | ||||
| var ( | ||||
| 	Root = &cobra.Command{ | ||||
| 		Use:   "nfctl", | ||||
| 		Short: "nfctl: easy start your nf backend work", | ||||
| 		PersistentPreRun: func(cmd *cobra.Command, args []string) { | ||||
| 			if opt.Debug == true { | ||||
| 				log.SetLogLevel(log.LogLevelDebug) | ||||
| 			} | ||||
| 		}, | ||||
| 	} | ||||
| ) | ||||
|  | ||||
| func init() { | ||||
| 	initNew() | ||||
| 	Root.PersistentFlags().BoolVar(&opt.Debug, "debug", false, "debug mode") | ||||
|  | ||||
| 	Root.AddCommand( | ||||
| 		versionCmd, | ||||
| 		checkCmd, | ||||
| 		cmdNew, | ||||
| 	) | ||||
| } | ||||
|   | ||||
| @@ -7,6 +7,7 @@ import ( | ||||
| 	"github.com/loveuer/nf/nft/nfctl/clone" | ||||
| 	"github.com/loveuer/nf/nft/nfctl/opt" | ||||
| 	"github.com/loveuer/nf/nft/nfctl/tp" | ||||
| 	"github.com/loveuer/nf/nft/nfctl/version" | ||||
| 	"github.com/spf13/cobra" | ||||
| 	"net/url" | ||||
| 	"os" | ||||
| @@ -33,14 +34,11 @@ nfctl new {project} --template http://username:token@my.gitlab.com/my-zone/my-re | ||||
| ) | ||||
|  | ||||
| func initNew() { | ||||
| 	cmdNew.Flags().BoolVar(&opt.Debug, "debug", false, "debug mode") | ||||
| 	cmdNew.Flags().StringVarP(&template, "template", "t", "", "template name/url[example:ultone, https://github.com/xxx/yyy.git]") | ||||
| 	cmdNew.Flags().BoolVar(&disableInit, "without-init", false, "don't run template init script") | ||||
|  | ||||
| 	cmdNew.RunE = func(cmd *cobra.Command, args []string) error { | ||||
| 		if opt.Debug { | ||||
| 			log.SetLogLevel(log.LogLevelDebug) | ||||
| 		} | ||||
| 		version.Check(true, false, 5) | ||||
|  | ||||
| 		var ( | ||||
| 			err        error | ||||
|   | ||||
| @@ -1,7 +1,7 @@ | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"github.com/loveuer/nf/nft/log" | ||||
| 	"github.com/fatih/color" | ||||
| 	"github.com/loveuer/nf/nft/nfctl/version" | ||||
| 	"github.com/spf13/cobra" | ||||
| ) | ||||
| @@ -11,7 +11,8 @@ var ( | ||||
| 		Use:   "version", | ||||
| 		Short: "print nfctl version and exit", | ||||
| 		Run: func(cmd *cobra.Command, args []string) { | ||||
| 			log.Info("version: %s", version.Version) | ||||
| 			color.Cyan("nfctl - version: %s", version.Version) | ||||
| 			version.Check(true, false, 5) | ||||
| 		}, | ||||
| 	} | ||||
| ) | ||||
|   | ||||
| @@ -3,24 +3,17 @@ package main | ||||
| import ( | ||||
| 	"context" | ||||
| 	"github.com/loveuer/nf/nft/nfctl/cmd" | ||||
| 	"github.com/loveuer/nf/nft/nfctl/version" | ||||
| 	"os/signal" | ||||
| 	"syscall" | ||||
| 	"time" | ||||
| ) | ||||
|  | ||||
| func main() { | ||||
| 	ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) | ||||
| 	defer cancel() | ||||
|  | ||||
| 	version.Check() | ||||
| 	defer version.Fn() | ||||
| 	//if !(len(os.Args) >= 2 && os.Args[1] == "version") { | ||||
| 	//	version.Check(5) | ||||
| 	//} | ||||
|  | ||||
| 	_ = cmd.Root.ExecuteContext(ctx) | ||||
|  | ||||
| 	select { | ||||
| 	case <-time.After(3 * time.Second): | ||||
| 	case <-ctx.Done(): | ||||
| 	case <-version.OkCh: | ||||
| 	} | ||||
| } | ||||
|   | ||||
							
								
								
									
										3
									
								
								nft/nfctl/version/var.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								nft/nfctl/version/var.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | ||||
| package version | ||||
|  | ||||
| const Version = "v24.07.14-r1" | ||||
| @@ -1,68 +1,81 @@ | ||||
| package version | ||||
|  | ||||
| import ( | ||||
| 	"bufio" | ||||
| 	"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" | ||||
| 	"net/http" | ||||
| 	"strings" | ||||
| 	"sync" | ||||
| 	"time" | ||||
| ) | ||||
|  | ||||
| const Version = "v24.07.13-r1" | ||||
|  | ||||
| var ( | ||||
| 	lk      = &sync.Mutex{} | ||||
| 	empty   = func() {} | ||||
| 	upgrade = func(v string) func() { | ||||
| 		return func() { | ||||
| 			color.Green("\n🎉 🎉 🎉 [nfctl] New Version Found: %s", v) | ||||
| 			color.Cyan("Upgrade it with: [go install github.com/loveuer/nf/nft/nfctl@master]") | ||||
| 			fmt.Print("Or Download by: ") | ||||
| 			color.Cyan(termlink.Link("Releases", "https://github.com/loveuer/nf/releases")) | ||||
| 			fmt.Println() | ||||
| 		} | ||||
| 	} | ||||
| 	Fn   = empty | ||||
| 	OkCh = make(chan struct{}, 1) | ||||
| 	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 Check() { | ||||
| 	ready := make(chan struct{}) | ||||
| 	go func() { | ||||
| 		ready <- struct{}{} | ||||
| 		uri := "https://raw.gitcode.com/loveuer/nf/raw/master/nft/nfctl/version/version.go" | ||||
| 		prefix := "const Version = " | ||||
| 		resp, err := http.Get(uri) | ||||
| 		if err != nil { | ||||
| 			log.Debug("[Check] http get[%s] err: %v", uri, err.Error()) | ||||
| 			return | ||||
| 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()) | ||||
| } | ||||
|  | ||||
| func Check(printUpgradable bool, printNoNeedUpgrade bool, timeout ...int) string { | ||||
| 	var ( | ||||
| 		v string | ||||
| 	) | ||||
|  | ||||
| 	defer func() { | ||||
| 		if printUpgradable { | ||||
| 			if v > Version { | ||||
| 				UpgradePrint(v) | ||||
| 			} | ||||
| 		} | ||||
| 		defer resp.Body.Close() | ||||
|  | ||||
| 		scanner := bufio.NewScanner(resp.Body) | ||||
| 		scanner.Buffer(make([]byte, 16*1024), 1024*1024) | ||||
|  | ||||
| 		for scanner.Scan() { | ||||
| 			line := scanner.Text() | ||||
| 			log.Debug("[Check] version.go line: %s", line) | ||||
| 			if strings.HasPrefix(line, prefix) { | ||||
| 				v := strings.TrimPrefix(line, prefix) | ||||
| 				if len(v) > 2 { | ||||
| 					v = v[1 : len(v)-1] | ||||
| 				} | ||||
|  | ||||
| 				if v != "" && v > Version { | ||||
| 					lk.Lock() | ||||
| 					Fn = upgrade(v) | ||||
| 					lk.Unlock() | ||||
| 					OkCh <- struct{}{} | ||||
| 					return | ||||
| 				} | ||||
| 		if printNoNeedUpgrade { | ||||
| 			if v == Version { | ||||
| 				color.Cyan("Your Version: %s is Newest", Version) | ||||
| 			} | ||||
| 		} | ||||
| 	}() | ||||
| 	<-ready | ||||
|  | ||||
| 	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) | ||||
| 	} | ||||
|  | ||||
| 	resp, err := client.R().SetContext(ctx). | ||||
| 		Get(uri) | ||||
| 	if err != nil { | ||||
| 		log.Debug("[Check] http get[%s] err: %v", uri, err.Error()) | ||||
| 		return "" | ||||
| 	} | ||||
|  | ||||
| 	log.Debug("[Check] http get[%s] body:\n%s", uri, resp.String()) | ||||
|  | ||||
| 	for _, line := range strings.Split(resp.String(), "\n") { | ||||
| 		log.Debug("[Check] version.go line: %s", line) | ||||
| 		if strings.HasPrefix(line, prefix) { | ||||
| 			may := strings.TrimPrefix(line, prefix) | ||||
| 			if len(may) > 2 { | ||||
| 				v = may[1 : len(may)-1] | ||||
| 			} | ||||
|  | ||||
| 			return v | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return "" | ||||
| } | ||||
|   | ||||
							
								
								
									
										16
									
								
								nft/nfctl/version/version_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								nft/nfctl/version/version_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | ||||
| package version | ||||
|  | ||||
| import ( | ||||
| 	"github.com/loveuer/nf/nft/log" | ||||
| 	"testing" | ||||
| ) | ||||
|  | ||||
| func TestUpgradePrint(t *testing.T) { | ||||
| 	UpgradePrint("v24.07.14-r5") | ||||
| } | ||||
|  | ||||
| func TestCheck(t *testing.T) { | ||||
| 	log.SetLogLevel(log.LogLevelDebug) | ||||
| 	v := Check(15) | ||||
| 	t.Logf("got version: %s", v) | ||||
| } | ||||
		Reference in New Issue
	
	Block a user