-
Notifications
You must be signed in to change notification settings - Fork 243
/
command_composite.go
62 lines (55 loc) · 1.67 KB
/
command_composite.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
}