Files
odo/pkg/libdevfile/command_composite.go
Philippe Martin 9a239c4e77 Use a single handler for executing all commands (#6826)
* Document current implementations of command handlers

* Add unit tests for execHAndler

* Refactor pkg/devfile/image to inject Backend as dependency

* Use same handler for kubedev/podmandev

* Fail after SelectBackend==nil only if backend is needed

* Move runHandler to dev/common

* Unit tests for runHandler

* Create a component.ExecuteTerminatingCommand

* ExecuteTerminatingCommand/ExecuteNonTerminatingCommand for Handler

* Fix calling other command types

* Consider parent group to determine if a command is terminating

* Replace component.execHandler by common.runHandler

* Remove execHandler

* Make runHandler and most of fields private and pass containersRunning to handler

* Pass containersRunning value

* deploy using common Handler

* Fix tests

* Use specific Dev/Deploy mode for Apply

* Fix cmdline for job

* Fix unit tests

* Pass appName and componentName with ctx to handler

* Move handler to pkg/component package

* Update doc

* Unit tests Deploy

* Unit tests Build

* Unit tests Run

* Unit tests PostStart

* Unit tests PreStop

* Update doc

* Fix Podman tests

* Fix hotReload on podman

* Change podman version timeout to 30s for tests

* Cleanup + fix doc
2023-05-26 11:01:21 -04:00

63 lines
1.7 KiB
Go

package libdevfile
import (
"context"
"fmt"
"strings"
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/v2/pkg/devfile/parser"
)
// compositeCommand is a command implementation that represents non-parallel composite commands
type compositeCommand struct {
command v1alpha2.Command
devfileObj parser.DevfileObj
}
var _ command = (*compositeCommand)(nil)
// newCompositeCommand creates a new command implementation which will execute the provided commands in the specified order
func newCompositeCommand(devfileObj parser.DevfileObj, command v1alpha2.Command) *compositeCommand {
return &compositeCommand{
command: command,
devfileObj: devfileObj,
}
}
func (o *compositeCommand) CheckValidity() error {
allCommands, err := allCommandsMap(o.devfileObj)
if err != nil {
return err
}
cmds := o.command.Composite.Commands
for _, cmd := range cmds {
if _, ok := allCommands[strings.ToLower(cmd)]; !ok {
return fmt.Errorf("composite command %q references command %q not found in devfile", o.command.Id, cmd)
}
}
return nil
}
// Execute loops over each command and executes them serially
func (o *compositeCommand) Execute(ctx context.Context, handler Handler, parentGroup *v1alpha2.CommandGroup) error {
allCommands, err := allCommandsMap(o.devfileObj)
if err != nil {
return err
}
for _, devfileCmd := range o.command.Composite.Commands {
cmd, err := newCommand(o.devfileObj, allCommands[strings.ToLower(devfileCmd)])
if err != nil {
return err
}
if parentGroup == nil {
parentGroup = o.command.Composite.Group
}
err = cmd.Execute(ctx, handler, parentGroup)
if err != nil {
return err
}
}
return nil
}