Files
sealos/pkg/utils/versionutil/version.go
T
zzjin ed4fcd77ab Update SEALOS_SYS_KUBE_VERSION usage. (#2264)
* Remove unused pkg template func.

Signed-off-by: zzjin <tczzjin@gmail.com>

* Update direct usage of SEALOS_SYS_KUBE_VERSION.
Remove unused template code.

Signed-off-by: zzjin <tczzjin@gmail.com>

* Update go.mod

Signed-off-by: zzjin <tczzjin@gmail.com>

* Update nosemgrep

Signed-off-by: zzjin <tczzjin@gmail.com>

* Fix nosempreg

Signed-off-by: zzjin <tczzjin@gmail.com>

* Fix ci and add template test.

Signed-off-by: zzjin <tczzjin@gmail.com>

* Add license

Signed-off-by: zzjin <tczzjin@gmail.com>

* Fix nosemgrep.

Signed-off-by: zzjin <tczzjin@gmail.com>

* Fix lint.

Signed-off-by: zzjin <tczzjin@gmail.com>

* Remove override to return error as except

Signed-off-by: zzjin <tczzjin@gmail.com>

Signed-off-by: zzjin <tczzjin@gmail.com>
2022-12-15 17:14:04 +08:00

54 lines
1.3 KiB
Go

/*
Copyright 2021 cuisongliu@qq.com.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package versionutil
import (
"strings"
"github.com/labring/sealos/pkg/utils/logger"
)
// Compare is version compare
// if v1 >= v2 return true, else return false
func Compare(v1, v2 string) bool {
v1 = strings.Replace(v1, "v", "", -1)
v2 = strings.Replace(v2, "v", "", -1)
v1 = strings.Split(v1, "-")[0]
v2 = strings.Split(v2, "-")[0]
v1List := strings.Split(v1, ".")
v2List := strings.Split(v2, ".")
if len(v1List) != 3 || len(v2List) != 3 {
logger.Error("error version format %s %s", v1, v2)
return false
}
if v1List[0] > v2List[0] {
return true
} else if v1List[0] < v2List[0] {
return false
}
if v1List[1] > v2List[1] {
return true
} else if v1List[1] < v2List[1] {
return false
}
if v1List[2] > v2List[2] {
return true
}
return true
}