Skip to content

Commit

Permalink
check for docker command before running fn eval (#2155)
Browse files Browse the repository at this point in the history
  • Loading branch information
natasha41575 committed Jun 7, 2021
1 parent aafba55 commit 59e01de
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
Expand Up @@ -23,7 +23,6 @@ import (
"path/filepath"
"strings"

"github.com/GoogleContainerTools/kpt/internal/errors"
"github.com/GoogleContainerTools/kpt/internal/fnruntime"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/kyaml/kio"
Expand Down Expand Up @@ -85,18 +84,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
Expand Up @@ -233,6 +233,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 59e01de

Please sign in to comment.