/
namespace.go
167 lines (139 loc) · 3.8 KB
/
namespace.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package project
import (
"fmt"
"regexp"
"github.com/ActiveState/cli/internal/fileutils"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/pkg/projectfile"
"github.com/go-openapi/strfmt"
)
// NamespaceRegex matches the org and project name in a namespace, eg. org/project
const NamespaceRegex = `^([\w-_]+)\/([\w-_\.]+)(?:#([-a-fA-F0-9]*))?$`
// ProjectRegex matches the project name for a namespace with omitted org.
const ProjectRegex = `^([\w-_\.]+)(?:#([-a-fA-F0-9]*))?$`
// Namespaced represents a project namespace of the form <org/project>
type Namespaced struct {
Owner string
Project string
CommitID *strfmt.UUID
AllowOmitOwner bool
}
type ConfigAble interface {
projectfile.ConfigGetter
}
func NewNamespace(owner, project, commitID string) *Namespaced {
ns := &Namespaced{
Owner: owner,
Project: project,
}
if commitID != "" {
commitUUID := strfmt.UUID(commitID)
ns.CommitID = &commitUUID
}
return ns
}
// Set implements the captain argmarshaler interface.
func (ns *Namespaced) Set(v string) error {
if ns == nil {
return fmt.Errorf("cannot set nil value")
}
parsedNs, err := ParseNamespace(v)
if err != nil {
if !ns.AllowOmitOwner {
return err
}
parsedNs, err = ParseProjectNoOwner(v)
if err != nil {
return err
}
}
*ns = *parsedNs
return nil
}
// String implements the fmt.Stringer interface.
func (ns *Namespaced) String() string {
if ns == nil {
return ""
}
var sep string
if ns.IsValid() {
sep = "/"
}
return fmt.Sprintf("%s%s%s", ns.Owner, sep, ns.Project)
}
// Type returns the human readable type name of Namespaced.
func (ns *Namespaced) Type() string {
return "namespace"
}
// IsValid returns whether or not the namespace is set sufficiently.
func (ns *Namespaced) IsValid() bool {
return ns != nil && ns.Project != ""
}
// Validate returns a failure if the namespace is not valid.
func (ns *Namespaced) Validate() error {
if ns == nil || !ns.IsValid() {
return locale.NewInputError("err_invalid_namespace", "", ns.String())
}
return nil
}
// ParseNamespace returns a valid project namespace
func ParseNamespace(raw string) (*Namespaced, error) {
rx := regexp.MustCompile(NamespaceRegex)
groups := rx.FindStringSubmatch(raw)
if len(groups) < 3 {
return nil, locale.NewInputError("err_invalid_namespace", "", raw)
}
names := Namespaced{
Owner: groups[1],
Project: groups[2],
}
if len(groups) > 3 && len(groups[3]) > 0 {
uuidString := groups[3]
if !strfmt.IsUUID(uuidString) {
return nil, locale.NewInputError("err_invalid_commit_id", "", uuidString)
}
uuid := strfmt.UUID(uuidString)
names.CommitID = &uuid
}
return &names, nil
}
func ParseProjectNoOwner(raw string) (*Namespaced, error) {
rx := regexp.MustCompile(ProjectRegex)
groups := rx.FindStringSubmatch(raw)
if len(groups) < 2 {
return nil, locale.NewInputError("err_invalid_project_name", "", raw)
}
names := Namespaced{
Project: groups[1],
AllowOmitOwner: true,
}
if len(groups) > 2 && len(groups[2]) > 0 {
uuidString := groups[2]
if !strfmt.IsUUID(uuidString) {
return nil, locale.NewInputError("err_invalid_commit_id", "", uuidString)
}
uuid := strfmt.UUID(uuidString)
names.CommitID = &uuid
}
return &names, nil
}
// NameSpaceForConfig returns a valid project namespace.
// This version prefers to create a namespace from a configFile if it exists
func NameSpaceForConfig(configFile string) *Namespaced {
if !fileutils.FileExists(configFile) {
return nil
}
prj, err := FromPath(configFile)
if err != nil {
return nil
}
names := Namespaced{
Owner: prj.Owner(),
Project: prj.Name(),
}
commitID := strfmt.UUID(prj.LegacyCommitID()) // Not using localcommit due to import cycle. See anti-pattern comment in localcommit pkg.
if commitID != "" {
names.CommitID = &commitID
}
return &names
}