...

Source file src/github.com/redhat-developer/odo/pkg/api/utils.go

Documentation: github.com/redhat-developer/odo/pkg/api

     1  package api
     2  
     3  import (
     4  	v1alpha2 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
     5  	"github.com/devfile/library/v2/pkg/devfile/parser"
     6  	"github.com/devfile/library/v2/pkg/devfile/parser/data"
     7  	"github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
     8  
     9  	"github.com/redhat-developer/odo/pkg/libdevfile"
    10  )
    11  
    12  func GetDevfileData(devfileObj parser.DevfileObj) (*DevfileData, error) {
    13  	commands, err := getDevfileCommands(devfileObj.Data)
    14  	if err != nil {
    15  		return nil, err
    16  	}
    17  	return &DevfileData{
    18  		Devfile:              devfileObj.Data,
    19  		Commands:             commands,
    20  		SupportedOdoFeatures: getSupportedOdoFeatures(devfileObj.Data),
    21  	}, nil
    22  }
    23  
    24  func getSupportedOdoFeatures(devfileData data.DevfileData) *SupportedOdoFeatures {
    25  	return &SupportedOdoFeatures{
    26  		Dev:    libdevfile.HasRunCommand(devfileData),
    27  		Deploy: libdevfile.HasDeployCommand(devfileData),
    28  		Debug:  libdevfile.HasDebugCommand(devfileData),
    29  	}
    30  }
    31  
    32  func getDevfileCommands(devfileData data.DevfileData) ([]DevfileCommand, error) {
    33  	commands, err := devfileData.GetCommands(common.DevfileOptions{})
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	toGroupFn := func(g *v1alpha2.CommandGroup) (group DevfileCommandGroup, isDefault *bool) {
    39  		if g == nil {
    40  			return "", nil
    41  		}
    42  		switch g.Kind {
    43  		case v1alpha2.BuildCommandGroupKind:
    44  			group = BuildCommandGroup
    45  		case v1alpha2.RunCommandGroupKind:
    46  			group = RunCommandGroup
    47  		case v1alpha2.DebugCommandGroupKind:
    48  			group = DebugCommandGroup
    49  		case v1alpha2.TestCommandGroupKind:
    50  			group = TestCommandGroup
    51  		case v1alpha2.DeployCommandGroupKind:
    52  			group = DeployCommandGroup
    53  		}
    54  
    55  		return group, g.IsDefault
    56  	}
    57  
    58  	var result []DevfileCommand
    59  	for _, cmd := range commands {
    60  		var (
    61  			cmdType      DevfileCommandType
    62  			cmdComponent string
    63  			cmdCompType  DevfileComponentType
    64  			cmdLine      string
    65  		)
    66  		var cmdGroup *v1alpha2.CommandGroup
    67  		switch {
    68  		case cmd.Apply != nil:
    69  			cmdType = ApplyCommandType
    70  			cmdComponent = cmd.Apply.Component
    71  			cmdGroup = cmd.Apply.Group
    72  		case cmd.Exec != nil:
    73  			cmdType = ExecCommandType
    74  			cmdComponent = cmd.Exec.Component
    75  			cmdGroup = cmd.Exec.Group
    76  			cmdLine = cmd.Exec.CommandLine
    77  		case cmd.Composite != nil:
    78  			cmdType = CompositeCommandType
    79  			cmdGroup = cmd.Composite.Group
    80  		}
    81  
    82  		var imageName string
    83  		var comp v1alpha2.Component
    84  		if cmdComponent != "" {
    85  			var ok bool
    86  			comp, ok, err = libdevfile.FindComponentByName(devfileData, cmdComponent)
    87  			if err != nil {
    88  				return nil, err
    89  			}
    90  			if ok {
    91  				switch {
    92  				case comp.Kubernetes != nil:
    93  					cmdCompType = KubernetesComponentType
    94  				case comp.Openshift != nil:
    95  					cmdCompType = OpenshiftComponentType
    96  				case comp.Container != nil:
    97  					cmdCompType = ContainerComponentType
    98  				case comp.Image != nil:
    99  					cmdCompType = ImageComponentType
   100  					imageName = comp.Image.ImageName
   101  				}
   102  			}
   103  		}
   104  		g, isDefault := toGroupFn(cmdGroup)
   105  		c := DevfileCommand{
   106  			Name:          cmd.Id,
   107  			Type:          cmdType,
   108  			Group:         g,
   109  			IsDefault:     isDefault,
   110  			CommandLine:   cmdLine,
   111  			Component:     cmdComponent,
   112  			ComponentType: cmdCompType,
   113  			ImageName:     imageName,
   114  		}
   115  		result = append(result, c)
   116  	}
   117  
   118  	return result, nil
   119  }
   120  

View as plain text