-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
76 lines (64 loc) · 2.09 KB
/
repository.go
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
// Copyright (c) The OpenTofu Authors
// SPDX-License-Identifier: MPL-2.0
package vcs
import (
"regexp"
)
// nameRe is a common understanding of acceptable repository addresses. This may need to be changed later if it turns
// out that other VCS' support different address styles.
var nameRe = regexp.MustCompile(`^[a-zA-Z0-9_\-]+$`)
// RepositoryAddr holds a reference to a repository. For simplicity, the current system does not support more complex
// URL structures.
type RepositoryAddr struct {
Org OrganizationAddr
// Name is the URL fragment of a repository.
Name string
}
func (r RepositoryAddr) String() string {
return string(r.Org) + "/" + r.Name
}
// Validate checks the assumptions the registry makes about repositories.
func (r RepositoryAddr) Validate() error {
if err := r.Org.Validate(); err != nil {
return &InvalidRepositoryAddrError{RepositoryAddr: r, Cause: err}
}
if !nameRe.MatchString(r.Name) {
return &InvalidRepositoryAddrError{
RepositoryAddr: r,
}
}
return nil
}
type InvalidRepositoryAddrError struct {
RepositoryString string
RepositoryAddr RepositoryAddr
Cause error
}
func (r InvalidRepositoryAddrError) Error() string {
if r.Cause != nil {
if r.RepositoryString != "" {
return "Failed to parse repository address: " + string(r.RepositoryString) + " (" + r.Cause.Error() + ")"
}
return "Failed to parse repository address: " + string(r.RepositoryAddr.String()) + " (" + r.Cause.Error() + ")"
}
if r.RepositoryString != "" {
return "Failed to parse repository address: " + string(r.RepositoryString)
}
return "Failed to parse repository address: " + string(r.RepositoryAddr.String())
}
func (r InvalidRepositoryAddrError) Unwrap() error {
return r.Cause
}
type RepositoryNotFoundError struct {
RepositoryAddr RepositoryAddr
Cause error
}
func (r RepositoryNotFoundError) Error() string {
if r.Cause != nil {
return "Repository not found: " + r.RepositoryAddr.String() + " (" + r.Cause.Error() + ")"
}
return "Repository not found: " + r.RepositoryAddr.String()
}
func (r RepositoryNotFoundError) Unwrap() error {
return r.Cause
}