mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-26 04:05:40 +01:00
clean code of common util (#15287)
Remove the useless code from the common util Signed-off-by: Wang Yan <wangyan@vmware.com>
This commit is contained in:
parent
494d74d32d
commit
248be37c3e
@ -1,22 +0,0 @@
|
||||
// Copyright Project Harbor Authors
|
||||
//
|
||||
// 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 error
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
// ErrDupProject is the error returned when creating a duplicate project
|
||||
var ErrDupProject = errors.New("duplicate project")
|
@ -1,94 +0,0 @@
|
||||
// Copyright Project Harbor Authors
|
||||
//
|
||||
// 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 utils
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Link : HTTP link header
|
||||
type Link struct {
|
||||
// URL : url part of header
|
||||
URL string
|
||||
// Rel : prev or next
|
||||
Rel string
|
||||
}
|
||||
|
||||
// Links : multiple link
|
||||
type Links []*Link
|
||||
|
||||
// Prev returns the URL indicated by "prev" rel.
|
||||
func (l Links) Prev() string {
|
||||
prev := ""
|
||||
for _, link := range l {
|
||||
if strings.ToLower(link.Rel) == "prev" {
|
||||
prev = link.URL
|
||||
break
|
||||
}
|
||||
}
|
||||
return prev
|
||||
}
|
||||
|
||||
// Next returns the URL indicated by "next" rel.
|
||||
func (l Links) Next() string {
|
||||
next := ""
|
||||
for _, link := range l {
|
||||
if link.Rel == "next" {
|
||||
next = link.URL
|
||||
break
|
||||
}
|
||||
}
|
||||
return next
|
||||
}
|
||||
|
||||
// ParseLink parses the raw link header to Links
|
||||
func ParseLink(raw string) Links {
|
||||
links := Links{}
|
||||
|
||||
for _, l := range strings.Split(raw, ",") {
|
||||
link := parseSingleLink(l)
|
||||
if link != nil {
|
||||
links = append(links, link)
|
||||
}
|
||||
}
|
||||
|
||||
return links
|
||||
}
|
||||
|
||||
func parseSingleLink(raw string) *Link {
|
||||
link := &Link{}
|
||||
|
||||
for _, str := range strings.Split(raw, ";") {
|
||||
str = strings.TrimSpace(str)
|
||||
if strings.HasPrefix(str, "<") && strings.HasSuffix(str, ">") {
|
||||
str = strings.Trim(str, "<>")
|
||||
link.URL = str
|
||||
continue
|
||||
}
|
||||
|
||||
parts := strings.SplitN(str, "=", 2)
|
||||
if len(parts) != 2 || strings.ToLower(parts[0]) != "rel" {
|
||||
continue
|
||||
}
|
||||
|
||||
link.Rel = strings.ToLower(strings.Trim(parts[1], "\""))
|
||||
}
|
||||
|
||||
if len(link.URL) == 0 || len(link.Rel) == 0 {
|
||||
link = nil
|
||||
}
|
||||
|
||||
return link
|
||||
}
|
@ -1,61 +0,0 @@
|
||||
// Copyright Project Harbor Authors
|
||||
//
|
||||
// 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 utils
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
scanAllMarker *TimeMarker
|
||||
scanOverviewMarker = &TimeMarker{
|
||||
interval: 15 * time.Second,
|
||||
}
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
// TimeMarker is used to control an action not to be taken frequently within the interval
|
||||
type TimeMarker struct {
|
||||
sync.RWMutex
|
||||
next time.Time
|
||||
interval time.Duration
|
||||
}
|
||||
|
||||
// Mark tries to mark a future time, which is after the duration of interval from the time it's called.
|
||||
func (t *TimeMarker) Mark() {
|
||||
t.Lock()
|
||||
defer t.Unlock()
|
||||
t.next = time.Now().Add(t.interval)
|
||||
}
|
||||
|
||||
// Check returns true if the current time is after the mark by this marker, and the caction the mark guards and be taken.
|
||||
func (t *TimeMarker) Check() bool {
|
||||
t.RLock()
|
||||
defer t.RUnlock()
|
||||
return time.Now().After(t.next)
|
||||
}
|
||||
|
||||
// Next returns the time of the next mark.
|
||||
func (t *TimeMarker) Next() time.Time {
|
||||
t.RLock()
|
||||
defer t.RUnlock()
|
||||
return t.next
|
||||
}
|
||||
|
||||
// ScanOverviewMarker ...
|
||||
func ScanOverviewMarker() *TimeMarker {
|
||||
return scanOverviewMarker
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
// Copyright Project Harbor Authors
|
||||
//
|
||||
// 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 utils
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestTimeMarker(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
m := &TimeMarker{
|
||||
interval: 1 * time.Second,
|
||||
}
|
||||
r1 := m.Check()
|
||||
assert.True(r1)
|
||||
m.Mark()
|
||||
r2 := m.Check()
|
||||
assert.False(r2)
|
||||
t.Log("Sleep for 2 seconds...")
|
||||
time.Sleep(2 * time.Second)
|
||||
r3 := m.Check()
|
||||
assert.True(r3)
|
||||
}
|
@ -209,16 +209,6 @@ func SafeCastFloat64(value interface{}) float64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// ParseOfftime ...
|
||||
func ParseOfftime(offtime int64) (hour, minite, second int) {
|
||||
offtime = offtime % (3600 * 24)
|
||||
hour = int(offtime / 3600)
|
||||
offtime = offtime % 3600
|
||||
minite = int(offtime / 60)
|
||||
second = int(offtime % 60)
|
||||
return
|
||||
}
|
||||
|
||||
// TrimLower ...
|
||||
func TrimLower(str string) string {
|
||||
return strings.TrimSpace(strings.ToLower(str))
|
||||
@ -268,11 +258,6 @@ func IsContainIllegalChar(s string, illegalChar []string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// IsDigest A sha256 is a string with 64 characters.
|
||||
func IsDigest(ref string) bool {
|
||||
return strings.HasPrefix(ref, "sha256:") && len(ref) == 71
|
||||
}
|
||||
|
||||
// ParseJSONInt ...
|
||||
func ParseJSONInt(value interface{}) (int, bool) {
|
||||
switch value.(type) {
|
||||
@ -295,13 +280,3 @@ func FindNamedMatches(regex *regexp.Regexp, str string) map[string]string {
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
// ParamPlaceholderForIn returns a string that contains placeholders for sql keyword "in"
|
||||
// e.g. n=3, returns "?,?,?"
|
||||
func ParamPlaceholderForIn(n int) string {
|
||||
placeholders := []string{}
|
||||
for i := 0; i < n; i++ {
|
||||
placeholders = append(placeholders, "?")
|
||||
}
|
||||
return strings.Join(placeholders, ",")
|
||||
}
|
||||
|
@ -160,43 +160,6 @@ func TestGenerateRandomStringWithLen(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseLink(t *testing.T) {
|
||||
raw := ""
|
||||
links := ParseLink(raw)
|
||||
if len(links) != 0 {
|
||||
t.Errorf("unexpected length: %d != %d", len(links), 0)
|
||||
}
|
||||
raw = "a;b,c"
|
||||
links = ParseLink(raw)
|
||||
if len(links) != 0 {
|
||||
t.Errorf("unexpected length: %d != %d", len(links), 0)
|
||||
}
|
||||
|
||||
raw = `</api/users?page=1&page_size=100>; rel="prev"`
|
||||
links = ParseLink(raw)
|
||||
if len(links) != 1 {
|
||||
t.Errorf("unexpected length: %d != %d", len(links), 1)
|
||||
}
|
||||
prev := `/api/users?page=1&page_size=100`
|
||||
if links.Prev() != prev {
|
||||
t.Errorf("unexpected prev: %s != %s", links.Prev(), prev)
|
||||
}
|
||||
|
||||
raw = `</api/users?page=1&page_size=100>; rel="prev", </api/users?page=3&page_size=100>; rel="next"`
|
||||
links = ParseLink(raw)
|
||||
if len(links) != 2 {
|
||||
t.Errorf("unexpected length: %d != %d", len(links), 2)
|
||||
}
|
||||
prev = `/api/users?page=1&page_size=100`
|
||||
if links.Prev() != prev {
|
||||
t.Errorf("unexpected prev: %s != %s", links.Prev(), prev)
|
||||
}
|
||||
next := `/api/users?page=3&page_size=100`
|
||||
if links.Next() != next {
|
||||
t.Errorf("unexpected prev: %s != %s", links.Next(), next)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTestTCPConn(t *testing.T) {
|
||||
server := httptest.NewServer(nil)
|
||||
defer server.Close()
|
||||
@ -354,29 +317,6 @@ func TestSafeCastFloat64(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseOfftime(t *testing.T) {
|
||||
cases := []struct {
|
||||
offtime int64
|
||||
hour int
|
||||
minite int
|
||||
second int
|
||||
}{
|
||||
{0, 0, 0, 0},
|
||||
{1, 0, 0, 1},
|
||||
{60, 0, 1, 0},
|
||||
{3600, 1, 0, 0},
|
||||
{3661, 1, 1, 1},
|
||||
{3600*24 + 60, 0, 1, 0},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
h, m, s := ParseOfftime(c.offtime)
|
||||
assert.Equal(t, c.hour, h)
|
||||
assert.Equal(t, c.minite, m)
|
||||
assert.Equal(t, c.second, s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrimLower(t *testing.T) {
|
||||
type args struct {
|
||||
str string
|
||||
@ -426,9 +366,3 @@ func TestGetStrValueOfAnyType(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsDigest(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
assert.False(IsDigest("latest"))
|
||||
assert.True(IsDigest("sha256:1359608115b94599e5641638bac5aef1ddfaa79bb96057ebf41ebc8d33acf8a7"))
|
||||
}
|
||||
|
@ -1,24 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
const nameComponent = `[a-z0-9]+((?:[._]|__|[-]*)[a-z0-9]+)*`
|
||||
|
||||
// TagRegexp is regular expression to match image tags, for example, 'v1.0'
|
||||
var TagRegexp = regexp.MustCompile(`^[\w][\w.-]{0,127}$`)
|
||||
|
||||
// RepoRegexp is regular expression to match repo name, for example, 'busybox', 'stage/busybox'
|
||||
var RepoRegexp = regexp.MustCompile(fmt.Sprintf("^%s(/%s)*$", nameComponent, nameComponent))
|
||||
|
||||
// ValidateTag validates whether a tag is valid.
|
||||
func ValidateTag(tag string) bool {
|
||||
return TagRegexp.MatchString(tag)
|
||||
}
|
||||
|
||||
// ValidateRepo validates whether a repo name is valid.
|
||||
func ValidateRepo(repo string) bool {
|
||||
return RepoRegexp.MatchString(repo)
|
||||
}
|
@ -1,183 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestValidateTag(t *testing.T) {
|
||||
cases := []struct {
|
||||
tag string
|
||||
valid bool
|
||||
}{
|
||||
{
|
||||
"v1.0",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"1.0.0",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"v1.0-alpha.0",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"1__",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"__v1.0",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"_...",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"_-_",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"--v1.0",
|
||||
false,
|
||||
},
|
||||
{
|
||||
".0.1",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"-0.1",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"0.1.*",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"0.1.?",
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
if c.valid {
|
||||
assert.True(t, ValidateTag(c.tag))
|
||||
} else {
|
||||
assert.False(t, ValidateTag(c.tag))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateRepo(t *testing.T) {
|
||||
cases := []struct {
|
||||
repo string
|
||||
valid bool
|
||||
}{
|
||||
{
|
||||
"a",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"a_a",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"a__a",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"a-a",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"a--a",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"a---a",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"a.a",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"a/b.b",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"a_a/b-b",
|
||||
true,
|
||||
},
|
||||
{
|
||||
".a",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"_a",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"-a",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"a.",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"a_",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"a-",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"a..a",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"a___a",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"a.-a",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"a_-a",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"a*",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"A/_a",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"A/.a",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"Aaaa",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"aaaA",
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
if c.valid {
|
||||
assert.True(t, ValidateRepo(c.repo))
|
||||
} else {
|
||||
assert.False(t, ValidateRepo(c.repo))
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user