...
1 package validate
2
3 import (
4 "fmt"
5 "strings"
6
7 devfilev1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
8 v2 "github.com/devfile/library/v2/pkg/devfile/parser/data/v2"
9 parsercommon "github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
10 "k8s.io/klog"
11 )
12
13
14
15 func ValidateDevfileData(data interface{}) error {
16
17 switch d := data.(type) {
18 case *v2.DevfileV2:
19 components, err := d.GetComponents(parsercommon.DevfileOptions{})
20 if err != nil {
21 return err
22 }
23 commands, err := d.GetCommands(parsercommon.DevfileOptions{})
24 if err != nil {
25 return err
26 }
27
28 commandsMap := getCommandsMap(commands)
29
30
31 if err := validateComponents(components); err != nil {
32 return err
33 }
34
35
36 if err := validateCommands(commandsMap); err != nil {
37 return err
38 }
39
40 default:
41 return fmt.Errorf("unknown devfile type %T", d)
42 }
43
44
45 klog.V(2).Info("Successfully validated devfile sections")
46 return nil
47
48 }
49
50
51 func getCommandsMap(commands []devfilev1.Command) map[string]devfilev1.Command {
52 commandMap := make(map[string]devfilev1.Command, len(commands))
53 for _, command := range commands {
54 command.Id = strings.ToLower(command.Id)
55 commandMap[command.Id] = command
56 }
57 return commandMap
58 }
59
View as plain text