Skip to content

Commit 49b0b1e

Browse files
committedFeb 10, 2021
update the logic into switch format for better understanding
Signed-off-by: Stephanie <[email protected]>
1 parent d6b85b6 commit 49b0b1e

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed
 

‎pkg/validation/projects.go

+21-12
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@ func ValidateStarterProjects(starterProjects []v1alpha2.StarterProject) error {
2121
continue
2222
}
2323

24-
if len(gitSource.Remotes) == 0 {
24+
switch len(gitSource.Remotes) {
25+
case 0:
2526
errString += fmt.Sprintf("\nstarterProject %s should have at least one remote", starterProject.Name)
26-
} else if len(gitSource.Remotes) > 1 {
27-
errString += fmt.Sprintf("\nstarterProject %s should have one remote only", starterProject.Name)
28-
} else if gitSource.CheckoutFrom != nil && gitSource.CheckoutFrom.Remote != "" {
29-
err := validateRemoteMap(gitSource.Remotes, gitSource.CheckoutFrom.Remote, starterProject.Name)
30-
if err != nil {
31-
errString += fmt.Sprintf("\n%s", err.Error())
27+
case 1:
28+
if gitSource.CheckoutFrom.Remote != "" {
29+
err := validateRemoteMap(gitSource.Remotes, gitSource.CheckoutFrom.Remote, starterProject.Name)
30+
if err != nil {
31+
errString += fmt.Sprintf("\n%s", err.Error())
32+
}
3233
}
34+
default: // len(gitSource.Remotes) >= 2
35+
errString += fmt.Sprintf("\nstarterProject %s should have one remote only", starterProject.Name)
3336
}
3437
}
3538

@@ -56,15 +59,21 @@ func ValidateProjects(projects []v1alpha2.Project) error {
5659
continue
5760
}
5861

59-
if len(gitSource.Remotes) == 0 {
62+
switch len(gitSource.Remotes) {
63+
case 0:
6064
errString += fmt.Sprintf("\nprojects %s should have at least one remote", project.Name)
61-
} else if len(gitSource.Remotes) > 1 || (gitSource.CheckoutFrom != nil && gitSource.CheckoutFrom.Remote != "") {
62-
if len(gitSource.Remotes) > 1 && (gitSource.CheckoutFrom == nil || (gitSource.CheckoutFrom != nil && gitSource.CheckoutFrom.Remote == "")) {
65+
case 1:
66+
if gitSource.CheckoutFrom != nil {
67+
if err := validateRemoteMap(gitSource.Remotes, gitSource.CheckoutFrom.Remote, project.Name); err != nil {
68+
errString += fmt.Sprintf("\n%s", err.Error())
69+
}
70+
}
71+
default: // len(gitSource.Remotes) >= 2
72+
if gitSource.CheckoutFrom == nil || gitSource.CheckoutFrom.Remote == "" {
6373
errString += fmt.Sprintf("\nproject %s has more than one remote defined, but has no checkoutfrom remote defined", project.Name)
6474
continue
6575
}
66-
err := validateRemoteMap(gitSource.Remotes, gitSource.CheckoutFrom.Remote, project.Name)
67-
if err != nil {
76+
if err := validateRemoteMap(gitSource.Remotes, gitSource.CheckoutFrom.Remote, project.Name); err != nil {
6877
errString += fmt.Sprintf("\n%s", err.Error())
6978
}
7079
}

0 commit comments

Comments
 (0)