diff --git a/api/search.go b/api/search.go index a6d2a4ce2..4da0068c0 100644 --- a/api/search.go +++ b/api/search.go @@ -68,7 +68,7 @@ func (s *SearchAPI) Get() { } } - projectSorter := &utils.ProjectSorter{Projects: projects} + projectSorter := &models.ProjectSorter{Projects: projects} sort.Sort(projectSorter) projectResult := []map[string]interface{}{} for _, p := range projects { diff --git a/models/project.go b/models/project.go index e16f0ce5f..0105f6a79 100644 --- a/models/project.go +++ b/models/project.go @@ -37,3 +37,23 @@ type Project struct { Role int `json:"current_user_role_id"` RepoCount int `json:"repo_count"` } + +// ProjectSorter holds an array of projects +type ProjectSorter struct { + Projects []Project +} + +// Len returns the length of array in ProjectSorter +func (ps *ProjectSorter) Len() int { + return len(ps.Projects) +} + +// Less defines the comparison rules of project +func (ps *ProjectSorter) Less(i, j int) bool { + return ps.Projects[i].Name < ps.Projects[j].Name +} + +// Swap swaps the position of i and j +func (ps *ProjectSorter) Swap(i, j int) { + ps.Projects[i], ps.Projects[j] = ps.Projects[j], ps.Projects[i] +} diff --git a/models/replication_job.go b/models/replication_job.go index 4203705f1..4c3fc11d4 100644 --- a/models/replication_job.go +++ b/models/replication_job.go @@ -19,6 +19,7 @@ import ( "time" "github.com/astaxie/beego/validation" + "github.com/vmware/harbor/utils" ) const ( @@ -129,6 +130,8 @@ func (r *RepTarget) Valid(v *validation.Validation) { v.SetError("endpoint", "can not be empty") } + r.URL = utils.FormatEndpoint(r.URL) + if len(r.URL) > 64 { v.SetError("endpoint", "max length is 64") } diff --git a/utils/registry/auth/authorizer.go b/utils/registry/auth/authorizer.go index 8dd4ef862..5f81e1e22 100644 --- a/utils/registry/auth/authorizer.go +++ b/utils/registry/auth/authorizer.go @@ -21,7 +21,7 @@ import ( "net/http" au "github.com/docker/distribution/registry/client/auth" - "github.com/vmware/harbor/utils/registry/utils" + "github.com/vmware/harbor/utils" ) // Authorizer authorizes requests according to the schema diff --git a/utils/registry/registry.go b/utils/registry/registry.go index 05ec2c33e..49b2cd124 100644 --- a/utils/registry/registry.go +++ b/utils/registry/registry.go @@ -21,8 +21,8 @@ import ( "net/url" "strings" + "github.com/vmware/harbor/utils" registry_error "github.com/vmware/harbor/utils/registry/error" - "github.com/vmware/harbor/utils/registry/utils" ) // Registry holds information of a registry entity diff --git a/utils/registry/repository.go b/utils/registry/repository.go index c4f48e04a..04a3b6b61 100644 --- a/utils/registry/repository.go +++ b/utils/registry/repository.go @@ -30,8 +30,8 @@ import ( "github.com/docker/distribution/manifest/schema1" "github.com/docker/distribution/manifest/schema2" + "github.com/vmware/harbor/utils" registry_error "github.com/vmware/harbor/utils/registry/error" - "github.com/vmware/harbor/utils/registry/utils" ) // Repository holds information of a repository entity diff --git a/utils/registry/utils/utils.go b/utils/registry/utils/utils.go deleted file mode 100644 index dc62c4c13..000000000 --- a/utils/registry/utils/utils.go +++ /dev/null @@ -1,44 +0,0 @@ -/* - Copyright (c) 2016 VMware, Inc. All Rights Reserved. - 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 ( - "net/url" - "strings" -) - -// FormatEndpoint formats endpoint -func FormatEndpoint(endpoint string) string { - endpoint = strings.TrimSpace(endpoint) - endpoint = strings.TrimRight(endpoint, "/") - if !strings.HasPrefix(endpoint, "http://") && - !strings.HasPrefix(endpoint, "https://") { - endpoint = "http://" + endpoint - } - - return endpoint -} - -// ParseEndpoint parses endpoint to a URL -func ParseEndpoint(endpoint string) (*url.URL, error) { - endpoint = FormatEndpoint(endpoint) - - u, err := url.Parse(endpoint) - if err != nil { - return nil, err - } - return u, nil -} diff --git a/utils/utils.go b/utils/utils.go index bc2b5a712..653e5409c 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -16,9 +16,8 @@ package utils import ( + "net/url" "strings" - - "github.com/vmware/harbor/models" ) // Repository holds information about repository @@ -34,22 +33,25 @@ func (r *Repository) GetProject() string { return r.Name[0:strings.LastIndex(r.Name, "/")] } -// ProjectSorter holds an array of projects -type ProjectSorter struct { - Projects []models.Project +// FormatEndpoint formats endpoint +func FormatEndpoint(endpoint string) string { + endpoint = strings.TrimSpace(endpoint) + endpoint = strings.TrimRight(endpoint, "/") + if !strings.HasPrefix(endpoint, "http://") && + !strings.HasPrefix(endpoint, "https://") { + endpoint = "http://" + endpoint + } + + return endpoint } -// Len returns the length of array in ProjectSorter -func (ps *ProjectSorter) Len() int { - return len(ps.Projects) -} +// ParseEndpoint parses endpoint to a URL +func ParseEndpoint(endpoint string) (*url.URL, error) { + endpoint = FormatEndpoint(endpoint) -// Less defines the comparison rules of project -func (ps *ProjectSorter) Less(i, j int) bool { - return ps.Projects[i].Name < ps.Projects[j].Name -} - -// Swap swaps the position of i and j -func (ps *ProjectSorter) Swap(i, j int) { - ps.Projects[i], ps.Projects[j] = ps.Projects[j], ps.Projects[i] + u, err := url.Parse(endpoint) + if err != nil { + return nil, err + } + return u, nil }