...

Source file src/github.com/redhat-developer/odo/pkg/devfile/validate/validate.go

Documentation: github.com/redhat-developer/odo/pkg/devfile/validate

     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  // ValidateDevfileData validates whether sections of devfile are odo compatible
    14  // after invoking the generic devfile validation
    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  		// Validate all the devfile components before validating commands
    31  		if err := validateComponents(components); err != nil {
    32  			return err
    33  		}
    34  
    35  		// Validate all the devfile commands before validating events
    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  	// Successful
    45  	klog.V(2).Info("Successfully validated devfile sections")
    46  	return nil
    47  
    48  }
    49  
    50  // getCommandsMap returns a map of the command Id to the command
    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