Skip to content

Commit

Permalink
Review
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy committed Feb 23, 2022
1 parent 669f5c1 commit 9f5923c
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 29 deletions.
12 changes: 5 additions & 7 deletions pkg/component/exec_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,21 @@ func (o *execHandler) Execute(command v1alpha2.Command) error {
}

func getCmdline(command v1alpha2.Command) []string {
exe := command.Exec

// deal with environment variables
var cmdLine string
setEnvVariable := util.GetCommandStringFromEnvs(exe.Env)
setEnvVariable := util.GetCommandStringFromEnvs(command.Exec.Env)

if setEnvVariable == "" {
cmdLine = exe.CommandLine
cmdLine = command.Exec.CommandLine
} else {
cmdLine = setEnvVariable + " && " + exe.CommandLine
cmdLine = setEnvVariable + " && " + command.Exec.CommandLine
}

// Change to the workdir and execute the command
var cmd []string
if exe.WorkingDir != "" {
if command.Exec.WorkingDir != "" {
// since we are using /bin/sh -c, the command needs to be within a single double quote instance, for example "cd /tmp && pwd"
cmd = []string{ShellExecutable, "-c", "cd " + exe.WorkingDir + " && " + cmdLine}
cmd = []string{ShellExecutable, "-c", "cd " + command.Exec.WorkingDir + " && " + cmdLine}
} else {
cmd = []string{ShellExecutable, "-c", cmdLine}
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/libdevfile/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
type command interface {
CheckValidity() error
Execute(handler Handler) error
UnExecute() error
}

// newCommand returns a command implementation, depending on the type of the command
Expand All @@ -39,7 +38,7 @@ func newCommand(devfileObj parser.DevfileObj, devfileCmd v1alpha2.Command) (comm
cmd = newExecCommand(devfileObj, devfileCmd)
}

if err := cmd.CheckValidity(); err != nil {
if err = cmd.CheckValidity(); err != nil {
return nil, err
}
return cmd, nil
Expand Down
10 changes: 2 additions & 8 deletions pkg/libdevfile/command_apply.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package libdevfile

import (
"fmt"

"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/pkg/devfile/parser"
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
Expand Down Expand Up @@ -35,11 +33,11 @@ func (o *applyCommand) Execute(handler Handler) error {
}

if len(devfileComponents) == 0 {
return fmt.Errorf("component %q does not exists", o.command.Apply.Component)
return NewComponentNotExistError(o.command.Apply.Component)
}

if len(devfileComponents) != 1 {
return fmt.Errorf("more than one component with the same name, should not happen")
return NewComponentsWithSameNameError()
}

component, err := newComponent(o.devfileObj, devfileComponents[0])
Expand All @@ -49,7 +47,3 @@ func (o *applyCommand) Execute(handler Handler) error {

return component.Apply(handler)
}

func (o *applyCommand) UnExecute() error {
return nil
}
4 changes: 0 additions & 4 deletions pkg/libdevfile/command_composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,3 @@ func (o *compositeCommand) Execute(handler Handler) error {
}
return nil
}

func (o *compositeCommand) UnExecute() error {
return nil
}
4 changes: 0 additions & 4 deletions pkg/libdevfile/command_composite_parallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,3 @@ func (o *parallelCompositeCommand) Execute(handler Handler) error {
}
return nil
}

func (p *parallelCompositeCommand) UnExecute() error {
return nil
}
4 changes: 0 additions & 4 deletions pkg/libdevfile/command_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,3 @@ func (o *execCommand) CheckValidity() error {
func (o *execCommand) Execute(handler Handler) error {
return handler.Execute(o.command)
}

func (o *execCommand) UnExecute() error {
return nil
}
25 changes: 25 additions & 0 deletions pkg/libdevfile/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,28 @@ func NewMoreThanOneDefaultCommandFoundError(kind v1alpha2.CommandGroupKind) More
func (e MoreThanOneDefaultCommandFoundError) Error() string {
return fmt.Sprintf("more than one default %s command found in devfile, this should not happen", e.kind)
}

// ComponentNotExistError is returned when a component referenced in a command or component does not exist
type ComponentNotExistError struct {
name string
}

func NewComponentNotExistError(name string) ComponentNotExistError {
return ComponentNotExistError{
name: name,
}
}

func (e ComponentNotExistError) Error() string {
return fmt.Sprintf("component %q does not exists", e.name)
}

type ComponentsWithSameNameError struct{}

func NewComponentsWithSameNameError() ComponentsWithSameNameError {
return ComponentsWithSameNameError{}
}

func (e ComponentsWithSameNameError) Error() string {
return "more than one component with the same name, should not happen"
}

0 comments on commit 9f5923c

Please sign in to comment.