Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
update driver lookup to allow for cli drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
simongdavies authored and glyn committed Jul 10, 2019
1 parent 791661f commit 465d589
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 4 deletions.
20 changes: 20 additions & 0 deletions pkg/driver/command_driver_nix.go
@@ -0,0 +1,20 @@
// +build !windows

package driver

import (
"fmt"
"os"
"os/exec"
)

// CheckDriverExists checks to see if the named driver exists
func (d *CommandDriver) CheckDriverExists() bool {
cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("command -v %s", d.cliName()))
cmd.Env = os.Environ()
if err := cmd.Run(); err != nil {
return false
}

return true
}
43 changes: 42 additions & 1 deletion pkg/driver/command_driver_test.go
@@ -1,5 +1,46 @@
package driver

import "github.com/deislabs/cnab-go/driver"
import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"

"github.com/deislabs/cnab-go/driver"
)

var _ driver.Driver = &CommandDriver{}

func TestCheckDriverExists(t *testing.T) {
name := "missing-driver"
cmddriver := &CommandDriver{Name: name}
if cmddriver.CheckDriverExists() {
t.Errorf("Expected driver %s not to exist", name)
}

name = "existing-driver"
cmddriver = &CommandDriver{Name: name}
dirname, err := ioutil.TempDir("", "duffle")
if err != nil {
t.Fatal(err)
}

defer os.RemoveAll(dirname)
filename := fmt.Sprintf("%s/duffle-%s", dirname, name)
newfile, err := os.Create(filename)
if err != nil {
t.Fatal(err)
}

newfile.Chmod(0755)
path := os.Getenv("PATH")
pathlist := []string{dirname, path}
newpath := strings.Join(pathlist, string(os.PathListSeparator))
defer os.Setenv("PATH", path)
os.Setenv("PATH", newpath)
if !cmddriver.CheckDriverExists() {
t.Fatalf("Expected driver %s to exist", name)
}

}
19 changes: 19 additions & 0 deletions pkg/driver/command_driver_windows.go
@@ -0,0 +1,19 @@
// +build windows

package driver

import (
"os"
"os/exec"
)

// CheckDriverExists checks to see if the named driver exists
func (d *CommandDriver) CheckDriverExists() bool {
cmd := exec.Command("where", d.cliName())
cmd.Env = os.Environ()
if err := cmd.Run(); err != nil {
return false
}

return true
}
9 changes: 6 additions & 3 deletions pkg/driver/lookup.go
Expand Up @@ -15,9 +15,12 @@ func Lookup(name string) (driver.Driver, error) {
return &KubernetesDriver{}, nil
case "debug":
return &driver.DebugDriver{}, nil
case "command":
return &CommandDriver{Name: name}, nil
default:
return nil, fmt.Errorf("unsupported driver: %s", name)
cmddriver := &CommandDriver{Name: name}
if cmddriver.CheckDriverExists() {
return cmddriver, nil
}

return nil, fmt.Errorf("unsupported driver or driver not found in PATH: %s", name)
}
}

0 comments on commit 465d589

Please sign in to comment.