@@ -2,51 +2,48 @@ package validation
2
2
3
3
import (
4
4
"fmt"
5
- "reflect"
6
5
"strings"
7
6
8
7
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
9
8
)
10
9
11
- // ValidateCommands validates the devfile commands:
12
- // 1. if the command id is all numeric, an error is returned
13
- // 2. if there are commands with duplicate IDs, an error is returned
14
- // 2. checks if its either a valid command
15
- // 3. checks if commands belonging to a specific group obeys the rule of 1 default command
10
+ // ValidateCommands validates the devfile commands and checks :
11
+ // 1. the command id is not all numeric
12
+ // 2. there are no duplicate command ids
13
+ // 3. the command type is not invalid
14
+ // 4. commands belonging to a specific group obeys the rule of 1 default command
16
15
func ValidateCommands (commands []v1alpha2.Command , components []v1alpha2.Component ) (err error ) {
17
- processedCommands := make (map [string ]string , len (commands ))
18
- groupCommandMap := make (map [v1alpha2.CommandGroup ][]v1alpha2.Command )
16
+ // processedCommands := make(map[string]string, len(commands))
17
+ groupKindCommandMap := make (map [v1alpha2.CommandGroupKind ][]v1alpha2.Command )
19
18
commandMap := getCommandsMap (commands )
20
19
20
+ err = v1alpha2 .CheckDuplicateKeys (commands )
21
+ if err != nil {
22
+ return err
23
+ }
24
+
21
25
for _ , command := range commands {
22
26
// Check if command id is all numeric
23
27
if isInt (command .Id ) {
24
28
return & InvalidNameOrIdError {id : command .Id , resourceType : "command" }
25
29
}
26
30
27
- // Check if the command is in the list of already processed commands
28
- // If there's a hit, it means more than one command share the same ID and we should error out
29
- if _ , exists := processedCommands [command .Id ]; exists {
30
- return & InvalidCommandError {commandId : command .Id , reason : "duplicate commands present with the same id" }
31
- }
32
- processedCommands [command .Id ] = command .Id
33
-
34
31
parentCommands := make (map [string ]string )
35
32
err = validateCommand (command , parentCommands , commandMap , components )
36
33
if err != nil {
37
34
return err
38
35
}
39
36
40
- commandGroup := * getGroup (command )
41
- if ! reflect . DeepEqual ( commandGroup , v1alpha2. CommandGroup {}) {
42
- groupCommandMap [commandGroup ] = append (groupCommandMap [commandGroup ], command )
37
+ commandGroup := getGroup (command )
38
+ if commandGroup != nil {
39
+ groupKindCommandMap [commandGroup . Kind ] = append (groupKindCommandMap [commandGroup . Kind ], command )
43
40
}
44
41
}
45
42
46
43
groupErrors := ""
47
- for group , commands := range groupCommandMap {
44
+ for groupKind , commands := range groupKindCommandMap {
48
45
if err = validateGroup (commands ); err != nil {
49
- groupErrors += fmt .Sprintf ("\n command group %s error - %s" , group . Kind , err .Error ())
46
+ groupErrors += fmt .Sprintf ("\n command group %s error - %s" , groupKind , err .Error ())
50
47
}
51
48
}
52
49
@@ -156,7 +153,7 @@ func validateCommandComponent(command v1alpha2.Command, components []v1alpha2.Co
156
153
}
157
154
158
155
// validateCompositeCommand checks that the specified composite command is valid. The command:
159
- // 1. should not reference itself via s subcommand
156
+ // 1. should not reference itself via a subcommand
160
157
// 2. should not indirectly reference itself via a subcommand which is a composite command
161
158
// 3. should reference a valid devfile command
162
159
// 4. should have a valid exec sub command
0 commit comments