Skip to content

Commit

Permalink
check for docker command before running fn eval
Browse files Browse the repository at this point in the history
  • Loading branch information
natasha41575 committed Jun 4, 2021
1 parent cbbb32e commit 180490a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
9 changes: 3 additions & 6 deletions internal/util/cmdutil/cmdutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"path/filepath"
"strings"

"github.com/GoogleContainerTools/kpt/internal/errors"
"github.com/GoogleContainerTools/kpt/internal/fnruntime"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -80,18 +79,16 @@ func ResolveAbsAndRelPaths(path string) (string, string, error) {
// DockerCmdAvailable runs `docker ps` to check that the docker command is
// available, and returns an error with installation instructions if it is not
func DockerCmdAvailable() error {
const op errors.Op = "docker.check"

suggestedText := `Docker is required to run this command.
To install docker, follow the instructions at https://docs.docker.com/get-docker/
suggestedText := `docker must be running to use this command
To install docker, follow the instructions at https://docs.docker.com/get-docker/.
`
buffer := &bytes.Buffer{}

cmd := exec.Command("docker", "version")
cmd.Stderr = buffer
err := cmd.Run()
if err != nil {
return errors.E(op, fmt.Errorf("%s", suggestedText))
return fmt.Errorf("%s", suggestedText)
}
return nil
}
Expand Down
6 changes: 6 additions & 0 deletions thirdparty/cmdconfig/commands/cmdeval/cmdeval.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,12 @@ func (r *EvalFnRunner) preRunE(c *cobra.Command, args []string) error {
if r.Image == "" && r.ExecPath == "" {
return errors.Errorf("must specify --image or --exec-path")
}
if r.Image != "" {
err := cmdutil.DockerCmdAvailable()
if err != nil {
return err
}
}
if err := cmdutil.ValidateImagePullPolicyValue(r.ImagePullPolicy); err != nil {
return err
}
Expand Down

0 comments on commit 180490a

Please sign in to comment.