-
Notifications
You must be signed in to change notification settings - Fork 32
/
git_project.go
64 lines (55 loc) · 1.8 KB
/
git_project.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
package types
import (
"github.com/go-playground/validator/v10"
git_url "github.com/kluctl/kluctl/pkg/git/git-url"
"github.com/kluctl/kluctl/pkg/yaml"
"strings"
)
type GitProject struct {
Url git_url.GitUrl `yaml:"url" validate:"required"`
Ref string `yaml:"ref,omitempty"`
SubDir string `yaml:"subDir,omitempty"`
}
func (gp *GitProject) UnmarshalYAML(unmarshal func(interface{}) error) error {
if err := unmarshal(&gp.Url); err == nil {
// it's a simple string
return nil
}
type raw GitProject
return unmarshal((*raw)(gp))
}
func ValidateGitProject(sl validator.StructLevel) {
gp := sl.Current().Interface().(GitProject)
if strings.Index(gp.SubDir, "/") != -1 {
sl.ReportError(gp.SubDir, "subDir", "SubDir", "subdirinvalid", "/ is not allowed in subdir")
}
}
type ExternalProject struct {
Project *GitProject `yaml:"project,omitempty"`
Path *string `yaml:"path,omitempty"`
}
func ValidateExternalProject(sl validator.StructLevel) {
p := sl.Current().Interface().(ExternalProject)
if p.Project == nil && p.Path == nil {
sl.ReportError(p, ".", ".", "empty", "either project or path must be set")
} else if p.Project != nil && p.Path != nil {
sl.ReportError(p, ".", ".", "empty", "only one of project or path can be set")
}
}
type ExternalProjects struct {
Projects []ExternalProject
}
func (gp *ExternalProjects) UnmarshalYAML(unmarshal func(interface{}) error) error {
singleProject := ExternalProject{}
if err := unmarshal(&singleProject); err == nil {
// it's a single project
gp.Projects = []ExternalProject{singleProject}
return nil
}
// try as array
return unmarshal(&gp.Projects)
}
func init() {
yaml.Validator.RegisterStructValidation(ValidateGitProject, GitProject{})
yaml.Validator.RegisterStructValidation(ValidateExternalProject, ExternalProject{})
}