From 035b888c23d14d98e6764d87eb834493d7ab3545 Mon Sep 17 00:00:00 2001 From: Asaf Gabai <77976014+asafgabai@users.noreply.github.com> Date: Tue, 11 Jan 2022 16:58:46 +0200 Subject: [PATCH] Move version utils to gofrog (#496) * Moved version utils to gofrog. --- artifactory/services/download.go | 2 +- artifactory/services/go/publish.go | 2 +- artifactory/services/search.go | 2 +- artifactory/services/utils/searchutil.go | 3 +- artifactory/services/utils/searchutil_test.go | 3 +- artifactory/usage/reportusage.go | 2 +- go.mod | 5 +- go.sum | 13 ++- tests/artifactoryfederatedrepository_test.go | 2 +- tests/artifactoryfederation_test.go | 2 +- utils/version/version.go | 110 ------------------ utils/version/version_test.go | 84 ------------- 12 files changed, 23 insertions(+), 207 deletions(-) delete mode 100644 utils/version/version.go delete mode 100644 utils/version/version_test.go diff --git a/artifactory/services/download.go b/artifactory/services/download.go index bd499d29a..44a681789 100644 --- a/artifactory/services/download.go +++ b/artifactory/services/download.go @@ -2,6 +2,7 @@ package services import ( biutils "github.com/jfrog/build-info-go/utils" + "github.com/jfrog/gofrog/version" "net/http" "os" "path" @@ -9,7 +10,6 @@ import ( "sort" "github.com/jfrog/jfrog-client-go/http/httpclient" - "github.com/jfrog/jfrog-client-go/utils/version" "github.com/jfrog/gofrog/parallel" "github.com/jfrog/jfrog-client-go/artifactory/services/utils" diff --git a/artifactory/services/go/publish.go b/artifactory/services/go/publish.go index 20ba0f6a6..77cc9e520 100644 --- a/artifactory/services/go/publish.go +++ b/artifactory/services/go/publish.go @@ -1,6 +1,7 @@ package _go import ( + "github.com/jfrog/gofrog/version" "github.com/jfrog/jfrog-client-go/utils/log" "net/http" "net/url" @@ -14,7 +15,6 @@ import ( "github.com/jfrog/jfrog-client-go/utils/io/content" "github.com/jfrog/jfrog-client-go/utils/io/fileutils" "github.com/jfrog/jfrog-client-go/utils/io/httputils" - "github.com/jfrog/jfrog-client-go/utils/version" ) const ArtifactoryMinSupportedVersion = "6.10.0" diff --git a/artifactory/services/search.go b/artifactory/services/search.go index 5421efee1..8baec9721 100644 --- a/artifactory/services/search.go +++ b/artifactory/services/search.go @@ -1,12 +1,12 @@ package services import ( + "github.com/jfrog/gofrog/version" "github.com/jfrog/jfrog-client-go/artifactory/services/utils" "github.com/jfrog/jfrog-client-go/auth" "github.com/jfrog/jfrog-client-go/http/jfroghttpclient" "github.com/jfrog/jfrog-client-go/utils/errorutils" "github.com/jfrog/jfrog-client-go/utils/io/content" - "github.com/jfrog/jfrog-client-go/utils/version" ) type SearchService struct { diff --git a/artifactory/services/utils/searchutil.go b/artifactory/services/utils/searchutil.go index 87b67381e..febe99015 100644 --- a/artifactory/services/utils/searchutil.go +++ b/artifactory/services/utils/searchutil.go @@ -13,8 +13,7 @@ import ( "sync" buildinfo "github.com/jfrog/build-info-go/entities" - - "github.com/jfrog/jfrog-client-go/utils/version" + "github.com/jfrog/gofrog/version" "github.com/jfrog/jfrog-client-go/utils/errorutils" "github.com/jfrog/jfrog-client-go/utils/io/content" diff --git a/artifactory/services/utils/searchutil_test.go b/artifactory/services/utils/searchutil_test.go index ddb9183ba..e193a84f0 100644 --- a/artifactory/services/utils/searchutil_test.go +++ b/artifactory/services/utils/searchutil_test.go @@ -2,11 +2,10 @@ package utils import ( "fmt" + "github.com/jfrog/gofrog/version" "path/filepath" "testing" - "github.com/jfrog/jfrog-client-go/utils/version" - "github.com/jfrog/jfrog-client-go/utils" "github.com/jfrog/jfrog-client-go/utils/io/content" "github.com/jfrog/jfrog-client-go/utils/io/fileutils" diff --git a/artifactory/usage/reportusage.go b/artifactory/usage/reportusage.go index 30845a222..f36522069 100644 --- a/artifactory/usage/reportusage.go +++ b/artifactory/usage/reportusage.go @@ -6,11 +6,11 @@ import ( "net/http" "errors" + versionutil "github.com/jfrog/gofrog/version" "github.com/jfrog/jfrog-client-go/artifactory" "github.com/jfrog/jfrog-client-go/artifactory/services/utils" "github.com/jfrog/jfrog-client-go/utils/errorutils" "github.com/jfrog/jfrog-client-go/utils/log" - versionutil "github.com/jfrog/jfrog-client-go/utils/version" ) const minArtifactoryVersion = "6.9.0" diff --git a/go.mod b/go.mod index c2d1ead9a..d36c5ff76 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/go-git/go-git/v5 v5.4.2 github.com/golang-jwt/jwt/v4 v4.1.0 github.com/gookit/color v1.4.2 - github.com/jfrog/build-info-go v0.1.6 + github.com/jfrog/build-info-go v1.0.0 github.com/jfrog/gofrog v1.1.1 github.com/mholt/archiver/v3 v3.5.1-0.20210618180617-81fac4ba96e4 github.com/pkg/errors v0.9.1 @@ -17,6 +17,7 @@ require ( ) require ( + github.com/CycloneDX/cyclonedx-go v0.4.0 // indirect github.com/Microsoft/go-winio v0.4.16 // indirect github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect github.com/acomagu/bufpipe v1.0.3 // indirect @@ -31,7 +32,9 @@ require ( github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect github.com/klauspost/compress v1.11.4 // indirect + github.com/klauspost/cpuid/v2 v2.0.6 // indirect github.com/klauspost/pgzip v1.2.5 // indirect + github.com/minio/sha256-simd v1.0.1-0.20210617151322-99e45fae3395 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/nwaples/rardecode v1.1.0 // indirect github.com/pierrec/lz4/v4 v4.1.2 // indirect diff --git a/go.sum b/go.sum index b90c31943..94efff2f7 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/CycloneDX/cyclonedx-go v0.4.0 h1:Wz4QZ9B4RXGWIWTypVLEOVJgOdFfy5mcS5PGNzUkZxU= +github.com/CycloneDX/cyclonedx-go v0.4.0/go.mod h1:rmRcf//gT7PIzovatusbWi377xqCg1FS4jyST0GH20E= github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA= github.com/Microsoft/go-winio v0.4.16 h1:FtSW/jqD+l4ba5iPBj9CODVtgfYAD8w2wS923g/cFDk= github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXOPRXwdLnMv0= @@ -12,6 +14,8 @@ github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= +github.com/bradleyjkemp/cupaloy/v2 v2.6.0 h1:knToPYa2xtfg42U3I6punFEjaGFKWQRXJwj0JTv4mTs= +github.com/bradleyjkemp/cupaloy/v2 v2.6.0/go.mod h1:bm7JXdkRd4BHJk9HpwqAI8BoAY1lps46Enkdqw6aRX0= github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= @@ -50,8 +54,8 @@ github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= -github.com/jfrog/build-info-go v0.1.6 h1:W6S2Nq65PVTpan3le66eOBvuMim2CbUTtRdnVPTCw+0= -github.com/jfrog/build-info-go v0.1.6/go.mod h1:MBrjdmZ6c7Q/otzFwx6dFrbNIjd/mcYDiEwn6nOUhcs= +github.com/jfrog/build-info-go v1.0.0 h1:4JsL8zfHDZqVpMwKICPY7qqyrqsFZzT0XzobI1Eu840= +github.com/jfrog/build-info-go v1.0.0/go.mod h1:QyeMfNGt4W0eWazrd7j6uZVN5KTpB05nxF/MwvGnTqs= github.com/jfrog/gofrog v1.1.1 h1:uRjeZWidQl4FmKP4Zpj5hSKJp3gSIWW9VUwbQdVEVRU= github.com/jfrog/gofrog v1.1.1/go.mod h1:9YN5v4LlsCfLIXpwQnzSf1wVtgjdHM20FzuIu58RMI4= github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 h1:DowS9hvgyYSX4TO5NpyC606/Z4SxnNYbT+WX27or6Ck= @@ -59,7 +63,10 @@ github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351/go.mod h1:CT github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= github.com/klauspost/compress v1.11.4 h1:kz40R/YWls3iqT9zX9AHN3WoVsrAWVyui5sxuLqiXqU= github.com/klauspost/compress v1.11.4/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= +github.com/klauspost/cpuid v1.2.0 h1:NMpwD2G9JSFOE1/TJjGSo5zG7Yb2bTe7eq1jH+irmeE= github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= +github.com/klauspost/cpuid/v2 v2.0.6 h1:dQ5ueTiftKxp0gyjKSx5+8BtPWkyQbd95m8Gys/RarI= +github.com/klauspost/cpuid/v2 v2.0.6/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/pgzip v1.2.5 h1:qnWYvvKqedOF2ulHpMG72XQol4ILEJ8k2wwRl/Km8oE= github.com/klauspost/pgzip v1.2.5/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= @@ -74,6 +81,8 @@ github.com/matryer/is v1.2.0 h1:92UTHpy8CDwaJ08GqLDzhhuixiBUUD1p3AU6PHddz4A= github.com/matryer/is v1.2.0/go.mod h1:2fLPjFQM9rhQ15aVEtbuwhJinnOqrmgXPNdZsdwlWXA= github.com/mholt/archiver/v3 v3.5.1-0.20210618180617-81fac4ba96e4 h1:S7ICJmgiEB5WbKXg3CPL/qmJKxxKf33yJSkxqbdiTQA= github.com/mholt/archiver/v3 v3.5.1-0.20210618180617-81fac4ba96e4/go.mod h1:e3dqJ7H78uzsRSEACH1joayhuSyhnonssnDhppzS1L4= +github.com/minio/sha256-simd v1.0.1-0.20210617151322-99e45fae3395 h1:GpZ9VB5YQdXbVvgCeyqzBPYijxEMehMhax1fUpCuVSc= +github.com/minio/sha256-simd v1.0.1-0.20210617151322-99e45fae3395/go.mod h1:f+LTnn56dRz2YGVXAZIW3myTjkbJhfyRDELQpWRHXto= github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= diff --git a/tests/artifactoryfederatedrepository_test.go b/tests/artifactoryfederatedrepository_test.go index 8becc51db..1ea522b0d 100644 --- a/tests/artifactoryfederatedrepository_test.go +++ b/tests/artifactoryfederatedrepository_test.go @@ -1,10 +1,10 @@ package tests import ( + "github.com/jfrog/gofrog/version" "testing" "github.com/jfrog/jfrog-client-go/artifactory/services" - "github.com/jfrog/jfrog-client-go/utils/version" "github.com/stretchr/testify/assert" ) diff --git a/tests/artifactoryfederation_test.go b/tests/artifactoryfederation_test.go index f6bfd95b4..c0af8ddf1 100644 --- a/tests/artifactoryfederation_test.go +++ b/tests/artifactoryfederation_test.go @@ -1,10 +1,10 @@ package tests import ( + "github.com/jfrog/gofrog/version" "testing" "github.com/jfrog/jfrog-client-go/artifactory/services" - "github.com/jfrog/jfrog-client-go/utils/version" "github.com/stretchr/testify/assert" ) diff --git a/utils/version/version.go b/utils/version/version.go deleted file mode 100644 index d3250a7be..000000000 --- a/utils/version/version.go +++ /dev/null @@ -1,110 +0,0 @@ -package version - -import ( - "github.com/jfrog/jfrog-client-go/utils" - "strconv" - "strings" -) - -type Version struct { - version string -} - -func NewVersion(version string) *Version { - return &Version{version: version} -} - -func (version *Version) GetVersion() string { - return version.version -} - -func (version *Version) SetVersion(artifactoryVersion string) { - version.version = artifactoryVersion -} - -// If ver1 == version returns 0 -// If ver1 > version returns 1 -// If ver1 < version returns -1 -func (version *Version) Compare(ver1 string) int { - if ver1 == version.version { - return 0 - } else if ver1 == utils.Development { - return 1 - } else if version.version == utils.Development { - return -1 - } - - ver1Tokens := strings.Split(ver1, ".") - ver2Tokens := strings.Split(version.version, ".") - - maxIndex := len(ver1Tokens) - if len(ver2Tokens) > maxIndex { - maxIndex = len(ver2Tokens) - } - - for tokenIndex := 0; tokenIndex < maxIndex; tokenIndex++ { - ver1Token := "0" - if len(ver1Tokens) >= tokenIndex+1 { - ver1Token = strings.TrimSpace(ver1Tokens[tokenIndex]) - } - ver2Token := "0" - if len(ver2Tokens) >= tokenIndex+1 { - ver2Token = strings.TrimSpace(ver2Tokens[tokenIndex]) - } - compare := compareTokens(ver1Token, ver2Token) - if compare != 0 { - return compare - } - } - - return 0 -} - -// Returns true if this version is larger or equals from the version sent as an argument. -func (version *Version) AtLeast(minVersion string) bool { - if version.Compare(minVersion) > 0 { - return false - } - return true -} - -func compareTokens(ver1Token, ver2Token string) int { - if ver1Token == ver2Token { - return 0 - } - - // Ignoring error because we strip all the non numeric values in advance. - ver1Number, ver1Suffix := splitNumberAndSuffix(ver1Token) - ver1TokenInt, _ := strconv.Atoi(ver1Number) - ver2Number, ver2Suffix := splitNumberAndSuffix(ver2Token) - ver2TokenInt, _ := strconv.Atoi(ver2Number) - - switch { - case ver1TokenInt > ver2TokenInt: - return 1 - case ver1TokenInt < ver2TokenInt: - return -1 - case len(ver1Suffix) == 0: // Version with suffix is higher than the same version without suffix - return -1 - case len(ver2Suffix) == 0: - return 1 - default: - return strings.Compare(ver1Token, ver2Token) - } -} - -func splitNumberAndSuffix(token string) (string, string) { - numeric := "" - var i int - for i = 0; i < len(token); i++ { - n := token[i : i+1] - if _, err := strconv.Atoi(n); err != nil { - break - } - numeric += n - } - if len(numeric) == 0 { - return "0", token - } - return numeric, token[i:] -} diff --git a/utils/version/version_test.go b/utils/version/version_test.go deleted file mode 100644 index c9f4cd25f..000000000 --- a/utils/version/version_test.go +++ /dev/null @@ -1,84 +0,0 @@ -package version - -import "testing" - -func TestCompare(t *testing.T) { - tests := []struct { - ver1 string - ver2 string - expected int - }{ - {"1.0.0", "1.0.0", 0}, - {"1.0.1", "1.0.0", 1}, - {"5.10.0", "5.5.2", 1}, - {"5.5.2", "5.15.2", -1}, - {"5.6.2", "5.50.2", -1}, - {"5.5.2", "5.0.2", 1}, - {"15.5.2", "6.0.2", 1}, - {"51.5.2", "6.0.2", 1}, - {"5.0.3", "5.0.20", -1}, - {"5.0.20", "5.0.3", 1}, - {"1.0.0", "1.0.1", -1}, - {"1.0.x-SNAPSHOT", "1.0.x-SNAPSHOT", 0}, - {"1.1.x-SNAPSHOT", "1.0.x-SNAPSHOT", 1}, - {"2.0.x-SNAPSHOT", "1.0.x-SNAPSHOT", 1}, - {"1.0", "1.0.x-SNAPSHOT", -1}, - {"1.1", "1.0.x-SNAPSHOT", 1}, - {"1.0.x-SNAPSHOT", "1.0", 1}, - {"1.0.x-SNAPSHOT", "1.1", -1}, - {"1", "2", -1}, - {"1.0", "2.0", -1}, - {"2.1", "2.0", 1}, - {"2.a", "2.b", -1}, - {"b", "a", 1}, - {"1.0", "1", 0}, - {"1.1", "1", 1}, - {"1", "1.1", -1}, - {"", "1", -1}, - {"1", "", 1}, - {"6.x-SNAPSHOT", "5.5.2", 1}, - {"6.x-SNAPSHOT", "6.5.0", -1}, - {"6.5.x-SNAPSHOT", "6.5.2", -1}, - {"7.x-SNAPSHOT", "6.x-SNAPSHOT", 1}, - {"6.1.x-SNAPSHOT", "6.2.x-SNAPSHOT", -1}, - {"go1.13", "go1.13.0", 0}, - {"go1.13", "go1.14.1", -1}, - {"go1.13", "go1.12.100", 1}, - } - for _, test := range tests { - t.Run(test.ver1+":"+test.ver2, func(t *testing.T) { - version := Version{version: test.ver2} - result := version.Compare(test.ver1) - if result != test.expected { - t.Error("ver1:", test.ver1, "ver2:", test.ver2, "Expecting:", test.expected, "got:", result) - } - }) - } -} - -func TestAtLeast(t *testing.T) { - tests := []struct { - ver1 string - ver2 string - expected bool - }{ - {"1.0.0", "1.0.0", true}, - {"1.0.1", "1.0.0", true}, - {"5.10.0", "5.5.2", true}, - {"1.0.x-SNAPSHOT", "1.0.x-SNAPSHOT", true}, - {"1.1.x-SNAPSHOT", "1.0.x-SNAPSHOT", true}, - {"2.0.x-SNAPSHOT", "1.0.x-SNAPSHOT", true}, - {"development", "5.5", true}, - {"6.2.0", "6.5.0", false}, - {"6.6.0", "6.8.0", false}, - } - for _, test := range tests { - t.Run(test.ver1+":"+test.ver2, func(t *testing.T) { - version := Version{version: test.ver1} - result := version.AtLeast(test.ver2) - if result != test.expected { - t.Error("ver1:", test.ver1, "ver2:", test.ver2, "Expecting:", test.expected, "got:", result) - } - }) - } -}