2018-10-04 08:54:04 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2018-10-17 22:26:54 +00:00
|
|
|
"fmt"
|
2019-01-22 00:46:08 +00:00
|
|
|
"os"
|
2018-10-17 22:26:54 +00:00
|
|
|
"reflect"
|
2018-10-04 08:54:04 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/coreos/go-semver/semver"
|
2019-04-30 10:28:10 +00:00
|
|
|
ref "github.com/docker/distribution/reference"
|
|
|
|
"github.com/sirupsen/logrus"
|
2018-10-04 08:54:04 +00:00
|
|
|
)
|
|
|
|
|
2018-10-17 22:26:54 +00:00
|
|
|
const (
|
|
|
|
WorkerThreads = 50
|
2019-06-11 18:17:16 +00:00
|
|
|
|
2019-06-26 21:37:33 +00:00
|
|
|
DefaultRKETools = "rancher/rke-tools:v0.1.31"
|
2018-10-17 22:26:54 +00:00
|
|
|
)
|
|
|
|
|
2018-10-04 08:54:04 +00:00
|
|
|
func StrToSemVer(version string) (*semver.Version, error) {
|
|
|
|
v, err := semver.NewVersion(strings.TrimPrefix(version, "v"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return v, nil
|
|
|
|
}
|
2018-10-17 22:26:54 +00:00
|
|
|
|
|
|
|
func GetObjectQueue(l interface{}) chan interface{} {
|
|
|
|
s := reflect.ValueOf(l)
|
|
|
|
c := make(chan interface{}, s.Len())
|
|
|
|
|
|
|
|
for i := 0; i < s.Len(); i++ {
|
|
|
|
c <- s.Index(i).Interface()
|
|
|
|
}
|
|
|
|
close(c)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func ErrList(e []error) error {
|
|
|
|
if len(e) > 0 {
|
|
|
|
return fmt.Errorf("%v", e)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-01-07 19:52:57 +00:00
|
|
|
|
|
|
|
// UniqueStringSlice - Input slice, retrun slice with unique elements. Will not maintain order.
|
|
|
|
func UniqueStringSlice(elements []string) []string {
|
|
|
|
encountered := map[string]bool{}
|
|
|
|
result := []string{}
|
|
|
|
|
|
|
|
for v := range elements {
|
|
|
|
if !encountered[elements[v]] {
|
|
|
|
encountered[elements[v]] = true
|
|
|
|
result = append(result, elements[v])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2019-01-22 00:46:08 +00:00
|
|
|
|
|
|
|
func IsSymlink(file string) (bool, error) {
|
|
|
|
f, err := os.Lstat(file)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if f.Mode()&os.ModeSymlink != 0 {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
2019-01-24 19:20:22 +00:00
|
|
|
|
2019-02-26 23:14:01 +00:00
|
|
|
func GetTagMajorVersion(tag string) string {
|
|
|
|
splitTag := strings.Split(tag, ".")
|
|
|
|
if len(splitTag) < 2 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return strings.Join(splitTag[:2], ".")
|
|
|
|
}
|
2019-04-23 19:44:42 +00:00
|
|
|
|
|
|
|
func IsFileExists(filePath string) (bool, error) {
|
|
|
|
if _, err := os.Stat(filePath); err == nil {
|
|
|
|
return true, nil
|
|
|
|
} else if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
} else {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
2019-04-30 10:28:10 +00:00
|
|
|
|
|
|
|
func GetImageTagFromImage(image string) (string, error) {
|
|
|
|
parsedImage, err := ref.ParseNormalizedNamed(image)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
imageTag := parsedImage.(ref.Tagged).Tag()
|
|
|
|
logrus.Debugf("Extracted version [%s] from image [%s]", imageTag, image)
|
|
|
|
return imageTag, nil
|
|
|
|
}
|