github.com/jlmeeker/kismatic@v1.10.1-0.20180612190640-57f9005a1f1a/pkg/inspector/check/executable.go (about)

     1  package check
     2  
     3  import (
     4  	"fmt"
     5  	"os/exec"
     6  	"regexp"
     7  )
     8  
     9  // ExecutableInPathCheck checks whether the binary is on the executable path
    10  type ExecutableInPathCheck struct {
    11  	Name string
    12  }
    13  
    14  // Check returns true if the executable is in the path
    15  func (c ExecutableInPathCheck) Check() (bool, error) {
    16  	// Need to explicitly call bash when running against Ubuntu
    17  	if err := c.validateExecutableName(); err != nil {
    18  		return false, err
    19  	}
    20  	cmd := exec.Command("bash", "-c", fmt.Sprintf("command -v %s", c.Name))
    21  	if err := cmd.Run(); err != nil {
    22  		return false, nil
    23  	}
    24  	return true, nil
    25  }
    26  
    27  func (c ExecutableInPathCheck) validateExecutableName() error {
    28  	// We need to ensure that we are not executing arbitrary code here...
    29  	// Only allow single word binary names. Allow dashes.
    30  	r := regexp.MustCompile("^[a-zA-Z-]+$")
    31  	if !r.MatchString(c.Name) {
    32  		return fmt.Errorf("invalid binary name used in check: %s. Names must adhere to the following regexp: %s", c.Name, r.String())
    33  	}
    34  	return nil
    35  }