-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (54 loc) · 1.14 KB
/
main.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
package main
import (
"fmt"
"strings"
"os"
)
const (
// 应该放在 config 之类的包里
DaoCloudCIName = "DaoCloud"
)
func isLowerAlnum(c rune) bool {
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')
}
func isSpecialChar(c rune) bool {
return c == '-' || c == '_' || c == '.'
}
// tag must match "[a-z0-9]+(?:[._-][a-z0-9]+)*"
func FixBuildTag(tag string) string {
lastRune := ' '
tmp := ""
tag = strings.ToLower(tag)
for _, c := range tag {
if isLowerAlnum(c) {
tmp += string(c)
} else if isSpecialChar(c) && isLowerAlnum(lastRune) {
tmp += string(c)
} else if isLowerAlnum(lastRune) {
tmp += "-"
}
lastRune = c
}
if len(tmp) < 1 {
tmp = DaoCloudCIName
}
if isSpecialChar(rune(tmp[len(tmp)-1])) {
tmp = tmp[:len(tmp)-1]
}
return tmp
}
func main() {
var imageTag string
var branch = os.Getenv("DAO_COMMIT_BRANCH")
var tag = os.Getenv("DAO_COMMIT_TAG")
var hash = os.Getenv("DAO_COMMIT_SHA")
if len(tag)>0 {
imageTag = tag
} else if len(hash)>7 {
imageTag = branch + "-" + hash[:7]
} else {
imageTag = branch
}
imageTag = FixBuildTag(imageTag)
fmt.Println(imageTag)
}