-
Notifications
You must be signed in to change notification settings - Fork 11
/
validations.go
66 lines (53 loc) · 1.85 KB
/
validations.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
/*This file is part of kuberpult.
Kuberpult is free software: you can redistribute it and/or modify
it under the terms of the Expat(MIT) License as published by
the Free Software Foundation.
Kuberpult is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
MIT License for more details.
You should have received a copy of the MIT License
along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>.
Copyright 2023 freiheit.com*/
package valid
import (
"regexp"
"strings"
)
const (
MaxAppNameLen = 39
AppNameRegExp = `\A[a-z0-9]+(?:-[a-z0-9]+)*\z`
TeamNameRegExp = AppNameRegExp
EnvNameRegExp = AppNameRegExp
SHA1CommitIDLength = 40
commitIDPrefixRegExp = `^[0-9a-fA-F]*$`
)
var (
applicationNameRx = regexp.MustCompile(AppNameRegExp)
teamNameRx = regexp.MustCompile(TeamNameRegExp)
envNameRx = regexp.MustCompile(EnvNameRegExp)
commitIDPrefixRx = regexp.MustCompile(commitIDPrefixRegExp)
)
// {application}-{environment} should be a valid dns name
func EnvironmentName(env string) bool {
return len(env) < 21 && envNameRx.MatchString(env)
}
func ApplicationName(name string) bool {
return len(name) <= MaxAppNameLen && applicationNameRx.MatchString(name)
}
func TeamName(name string) bool {
return len(name) < 21 && teamNameRx.MatchString(name)
}
// Lock names must be valid file names
func LockId(lockId string) bool {
return len(lockId) < 100 && len(lockId) > 1 && lockId != ".." && lockId != "." && !strings.ContainsAny(lockId, "/")
}
func SHA1CommitID(commitID string) bool {
if len(commitID) != SHA1CommitIDLength {
return false
}
return commitIDPrefixRx.MatchString(commitID)
}
func SHA1CommitIDPrefix(prefix string) bool {
return commitIDPrefixRx.MatchString(prefix)
}