summaryrefslogtreecommitdiffstats
path: root/pkg/name/repository.go
blob: 9250e362525a6ec96db60d8ef307c2024cb22915 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2018 Google LLC 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 name

import (
	"fmt"
	"strings"
)

const (
	defaultNamespace = "library"
	repositoryChars  = "abcdefghijklmnopqrstuvwxyz0123456789_-./"
	regRepoDelimiter = "/"
)

// Repository stores a docker repository name in a structured form.
type Repository struct {
	Registry
	repository string
}

// See https://docs.docker.com/docker-hub/official_repos
func hasImplicitNamespace(repo string, reg Registry) bool {
	return !strings.ContainsRune(repo, '/') && reg.RegistryStr() == DefaultRegistry
}

// RepositoryStr returns the repository component of the Repository.
func (r Repository) RepositoryStr() string {
	if hasImplicitNamespace(r.repository, r.Registry) {
		return fmt.Sprintf("%s/%s", defaultNamespace, r.repository)
	}
	return r.repository
}

// Name returns the name from which the Repository was derived.
func (r Repository) Name() string {
	regName := r.Registry.Name()
	if regName != "" {
		return regName + regRepoDelimiter + r.RepositoryStr()
	}
	// TODO: As far as I can tell, this is unreachable.
	return r.RepositoryStr()
}

func (r Repository) String() string {
	return r.Name()
}

// Scope returns the scope required to perform the given action on the registry.
// TODO(jonjohnsonjr): consider moving scopes to a separate package.
func (r Repository) Scope(action string) string {
	return fmt.Sprintf("repository:%s:%s", r.RepositoryStr(), action)
}

func checkRepository(repository string) error {
	return checkElement("repository", repository, repositoryChars, 2, 255)
}

// NewRepository returns a new Repository representing the given name, according to the given strictness.
func NewRepository(name string, opts ...Option) (Repository, error) {
	opt := makeOptions(opts...)
	if len(name) == 0 {
		return Repository{}, newErrBadName("a repository name must be specified")
	}

	var registry string
	repo := name
	parts := strings.SplitN(name, regRepoDelimiter, 2)
	if len(parts) == 2 && (strings.ContainsRune(parts[0], '.') || strings.ContainsRune(parts[0], ':')) {
		// The first part of the repository is treated as the registry domain
		// iff it contains a '.' or ':' character, otherwise it is all repository
		// and the domain defaults to Docker Hub.
		registry = parts[0]
		repo = parts[1]
	}

	if err := checkRepository(repo); err != nil {
		return Repository{}, err
	}

	reg, err := NewRegistry(registry, opts...)
	if err != nil {
		return Repository{}, err
	}
	if hasImplicitNamespace(repo, reg) && opt.strict {
		return Repository{}, newErrBadName("strict validation requires the full repository path (missing 'library')")
	}
	return Repository{reg, repo}, nil
}

// Tag returns a Tag in this Repository.
func (r Repository) Tag(identifier string) Tag {
	t := Tag{
		tag:        identifier,
		Repository: r,
	}
	t.original = t.Name()
	return t
}

// Digest returns a Digest in this Repository.
func (r Repository) Digest(identifier string) Digest {
	d := Digest{
		digest:     identifier,
		Repository: r,
	}
	d.original = d.Name()
	return d
}