Skip to content

Commit fa64aaa

Browse files
committed
style: adjustment code
1 parent 20e1525 commit fa64aaa

File tree

2 files changed

+38
-41
lines changed

2 files changed

+38
-41
lines changed

cmd/sponge/commands/generate/common.go

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ package generate
44
import (
55
"bufio"
66
"bytes"
7-
"context"
87
"encoding/json"
98
"errors"
109
"fmt"
1110
"net/url"
1211
"os"
1312
"path/filepath"
1413
"regexp"
14+
"sort"
1515
"strconv"
1616
"strings"
1717
"text/template"
@@ -802,29 +802,54 @@ func getSubFiles(selectFiles map[string][]string, replaceFiles map[string][]stri
802802
return files
803803
}
804804

805+
type Version struct {
806+
major string
807+
minor string
808+
patch string
809+
goVersion string
810+
}
811+
805812
func getLocalGoVersion() string {
806813
result, err := gobash.Exec("go", "version")
807814
if err != nil {
808815
return defaultGoModVersion
809816
}
810817

811-
pattern := `go(\d+\.\d+)`
812-
re := regexp.MustCompile(pattern)
813-
matches := re.FindStringSubmatch(string(result))
814-
if len(matches) < 2 {
815-
return defaultGoModVersion
818+
versions := []string{
819+
strings.ReplaceAll(defaultGoModVersion, " ", ""),
820+
string(result),
816821
}
817822

818-
localGoVersion := "go " + matches[1]
819-
if localGoVersion < defaultGoModVersion {
820-
return defaultGoModVersion
823+
versionRegex := regexp.MustCompile(`go(\d+)\.(\d+)(\.(\d+))?`)
824+
825+
var versionList []Version
826+
for _, v := range versions {
827+
matches := versionRegex.FindStringSubmatch(v)
828+
if len(matches) >= 3 {
829+
goVersion := "go " + matches[1] + "." + matches[2]
830+
if matches[4] != "" {
831+
goVersion += "." + matches[4]
832+
}
833+
versionList = append(versionList, Version{major: matches[1], minor: matches[2], patch: matches[4], goVersion: goVersion})
834+
}
821835
}
822836

823-
if len(localGoVersion) != 6 && len(localGoVersion) != 7 {
837+
// descending sort by major, minor, patch
838+
sort.Slice(versionList, func(i, j int) bool {
839+
if versionList[i].major != versionList[j].major {
840+
return utils.StrToInt(versionList[i].major) > utils.StrToInt(versionList[j].major)
841+
}
842+
if versionList[i].minor != versionList[j].minor {
843+
return utils.StrToInt(versionList[i].minor) > utils.StrToInt(versionList[j].minor)
844+
}
845+
return utils.StrToInt(versionList[i].patch) > utils.StrToInt(versionList[j].patch)
846+
})
847+
848+
if len(versionList) == 0 {
824849
return defaultGoModVersion
825850
}
826851

827-
return localGoVersion
852+
return versionList[0].goVersion
828853
}
829854

830855
func dbDriverErr(driver string) error {
@@ -1055,30 +1080,3 @@ func getGRPCServiceFields() []replacer.Field {
10551080
},
10561081
}
10571082
}
1058-
1059-
func clearCurrentLine() {
1060-
fmt.Print("\033[2K\r")
1061-
}
1062-
1063-
func PrintWaiting(ctx context.Context, runningTip string, finishTip string) {
1064-
defer func() {
1065-
clearCurrentLine()
1066-
fmt.Println(finishTip)
1067-
}()
1068-
symbols := []string{runningTip + ".", runningTip + "..", runningTip + "...", runningTip + "....", runningTip + ".....", runningTip + "......"}
1069-
index := 0
1070-
fmt.Printf("\r%s", symbols[index])
1071-
for {
1072-
select {
1073-
case <-ctx.Done():
1074-
return
1075-
case <-time.After(500 * time.Millisecond):
1076-
index++
1077-
if index >= len(symbols) {
1078-
index = 0
1079-
clearCurrentLine()
1080-
}
1081-
fmt.Printf("\r%s", symbols[index])
1082-
}
1083-
}
1084-
}

pkg/etcdcli/etcdcli.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ func Init(endpoints []string, opts ...Option) (*clientv3.Client, error) {
2828
conf := clientv3.Config{
2929
Endpoints: endpoints,
3030
DialTimeout: o.dialTimeout,
31-
DialKeepAliveTime: 10 * time.Second,
32-
DialKeepAliveTimeout: 3 * time.Second,
33-
DialOptions: []grpc.DialOption{grpc.WithBlock()},
31+
DialKeepAliveTime: 20 * time.Second,
32+
DialKeepAliveTimeout: 10 * time.Second,
3433
AutoSyncInterval: o.autoSyncInterval,
3534
Logger: o.logger,
3635
Username: o.username,

0 commit comments

Comments
 (0)