...

Source file src/github.com/redhat-developer/odo/pkg/libdevfile/command_composite.go

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

     1  package libdevfile
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
     9  	"github.com/devfile/library/v2/pkg/devfile/parser"
    10  )
    11  
    12  // compositeCommand is a command implementation that represents non-parallel composite commands
    13  type compositeCommand struct {
    14  	command    v1alpha2.Command
    15  	devfileObj parser.DevfileObj
    16  }
    17  
    18  var _ command = (*compositeCommand)(nil)
    19  
    20  // newCompositeCommand creates a new command implementation which will execute the provided commands in the specified order
    21  func newCompositeCommand(devfileObj parser.DevfileObj, command v1alpha2.Command) *compositeCommand {
    22  	return &compositeCommand{
    23  		command:    command,
    24  		devfileObj: devfileObj,
    25  	}
    26  }
    27  
    28  func (o *compositeCommand) CheckValidity() error {
    29  	allCommands, err := allCommandsMap(o.devfileObj)
    30  	if err != nil {
    31  		return err
    32  	}
    33  	cmds := o.command.Composite.Commands
    34  	for _, cmd := range cmds {
    35  		if _, ok := allCommands[strings.ToLower(cmd)]; !ok {
    36  			return fmt.Errorf("composite command %q references command %q not found in devfile", o.command.Id, cmd)
    37  		}
    38  	}
    39  	return nil
    40  }
    41  
    42  // Execute loops over each command and executes them serially
    43  func (o *compositeCommand) Execute(ctx context.Context, handler Handler, parentGroup *v1alpha2.CommandGroup) error {
    44  	allCommands, err := allCommandsMap(o.devfileObj)
    45  	if err != nil {
    46  		return err
    47  	}
    48  	for _, devfileCmd := range o.command.Composite.Commands {
    49  		cmd, err := newCommand(o.devfileObj, allCommands[strings.ToLower(devfileCmd)])
    50  		if err != nil {
    51  			return err
    52  		}
    53  		if parentGroup == nil {
    54  			parentGroup = o.command.Composite.Group
    55  		}
    56  		err = cmd.Execute(ctx, handler, parentGroup)
    57  		if err != nil {
    58  			return err
    59  		}
    60  	}
    61  	return nil
    62  }
    63  

View as plain text