Skip to content

Commit

Permalink
Merge pull request #7 from storyicon/fix/protoc-only-use-regular-version
Browse files Browse the repository at this point in the history
fix(protoc): only use regular version
  • Loading branch information
storyicon authored Aug 28, 2021
2 parents 8bdd192 + ef9cf8a commit 3613a20
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
10 changes: 8 additions & 2 deletions pkg/component/pluginmanager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (b *BasicPluginManager) IsProtocInstalled(ctx context.Context, version stri
return IsProtocInstalled(ctx, b.storageDir, version)
}

// GetProtocLatestVersion is used to geet the latest version of protoc
// GetProtocLatestVersion is used to get the latest version of protoc
func (b *BasicPluginManager) GetProtocLatestVersion(ctx context.Context) (string, error) {
ctx, cancel := consts.GetContextWithPerCommandTimeout(ctx)
defer cancel()
Expand All @@ -216,7 +216,13 @@ func (b *BasicPluginManager) GetProtocLatestVersion(ctx context.Context) (string
if len(versions) == 0 {
return "", errors.New("no version list")
}
return versions[len(versions)-1], nil
regularVersions := make([]string, 0, len(versions))
for _, version := range versions {
if util.IsRegularVersion(version) {
regularVersions = append(regularVersions, version)
}
}
return regularVersions[len(regularVersions)-1], nil
}

// ListProtocVersions is used to list protoc version
Expand Down
12 changes: 11 additions & 1 deletion pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,17 @@ func GetExitCode(err error) int {
return 1
}

var regexpEnvironmentVar = regexp.MustCompile(`\$[A-Za-z_]+`)
var (
regexpEnvironmentVar = regexp.MustCompile(`\$[A-Za-z_]+`)
regexpRegularVersion = regexp.MustCompile(`^v[0-9]+\.[0-9]+\.[0-9]+$`)
)


// IsRegularVersion is used to determine whether the version number is a regular version number
// Regular: va.b.c, and a, b, c are all numbers
func IsRegularVersion(s string) bool {
return regexpRegularVersion.MatchString(s)
}

// RenderWithEnv is used to render string with env
func RenderWithEnv(s string, ext map[string]string) string {
Expand Down
47 changes: 47 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,50 @@ func TestSortSemanticVersion(t *testing.T) {
})
}
}

func TestIsRegularVersion(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want bool
}{
{
args: args{"v0.0.0"},
want: true,
},
{
args: args{"v11.0.0"},
want: true,
},
{
args: args{"v0.11.0"},
want: true,
},
{
args: args{"v0.0.11"},
want: true,
},
{
args: args{"0.0.11"},
want: false,
},
{
args: args{"0.0.a"},
want: false,
},
{
args: args{"v0.0.1-rc0"},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsRegularVersion(tt.args.s); got != tt.want {
t.Errorf("IsRegularVersion() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 3613a20

Please sign in to comment.