forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
76 lines (68 loc) · 1.71 KB
/
validate.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
package kube
import (
"fmt"
"strings"
"github.com/jenkins-x/jx/pkg/client/clientset/versioned"
"github.com/jenkins-x/jx/pkg/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/validation"
)
const (
OptionName = "name"
OptionNamespace = "namespace"
)
func ValidateSubDomain(val interface{}) error {
str, ok := val.(string)
if !ok {
return fmt.Errorf("Expected some text!")
}
if strings.TrimSpace(str) == "" {
return fmt.Errorf("Value is required")
}
errors := validation.IsDNS1123Subdomain(str)
if len(errors) > 0 {
return fmt.Errorf(strings.Join(errors, "/n"))
}
return nil
}
func ValidateName(val interface{}) error {
str, ok := val.(string)
if !ok {
return fmt.Errorf("Expected some text!")
}
if strings.TrimSpace(str) == "" {
return fmt.Errorf("Value is required")
}
errors := validation.IsDNS1123Label(str)
if len(errors) > 0 {
return fmt.Errorf(strings.Join(errors, "/n"))
}
return nil
}
func ValidSubDomainOption(option string, value string) error {
if value != "" {
errors := validation.IsDNS1123Subdomain(value)
if len(errors) > 0 {
return util.InvalidOptionf(option, value, strings.Join(errors, "/n"))
}
}
return nil
}
func ValidNameOption(option string, value string) error {
if value != "" {
errors := validation.IsDNS1123Label(value)
if len(errors) > 0 {
return util.InvalidOptionf(option, value, strings.Join(errors, "/n"))
}
}
return nil
}
func ValidateEnvironmentDoesNotExist(jxClient *versioned.Clientset, ns string, str string) error {
if str != "" {
_, err := jxClient.JenkinsV1().Environments(ns).Get(str, metav1.GetOptions{})
if err == nil {
return fmt.Errorf("Environment %s already exists!", str)
}
}
return nil
}