Example #1
0
// tests Path as well
func TestDir(t *testing.T) {
	expectContains := "github.com/fastly/go-utils/executable"
	dir, err := executable.Dir()
	if err != nil {
		t.Fatalf("unable to get test dir, err: %v", err)
	}
	if !strings.Contains(dir, expectContains) {
		t.Errorf("wrong executable dir, got: %v, expectedContains: %v", dir, expectContains)
	}
}
Example #2
0
// LocatePackagedPEMDir locates the path of the packaged PEM store which is the
// directory named "certs". functions that take a (name string) parameter look
// for files named ${name}-key.pem and/or ${name}-cert.pem in this directory.
func LocatePackagedPEMDir() (dir string, err error) {
	if packagedCertDir != "" {
		dir = packagedCertDir
		return
	} else if _certPath != "" {
		packagedCertDir = _certPath
		dir = packagedCertDir
		return
	}

	var binDir string
	binDir, err = executable.Dir()
	if err != nil {
		return
	}

	cwd, _ := os.Getwd()

	searchList := []string{
		binDir + "../certs", // deb: certs is one up from bin
		cwd + "/testcerts",  // tests: certs is 4 up from _test files
	}

	// src-containing git checkout: $GOPATH/certs
	if goPath := os.Getenv("GOPATH"); goPath != "" {
		for _, gp := range filepath.SplitList(goPath) {
			searchList = append(searchList, gp+"/certs")
		}
	}

	// non-src git checkout: search for a certs/ starting deepest-first at
	// $GOPATH/src/foo/bar/, where foo/bar is the package calling into this
	// one. This works if foo/bar has subpackages which call this function,
	// even indirectly by way of the other functions in this package.
	if callerDir, ok := callerPkgDir(); ok {
		if certsDir, ok := searchUpwards(callerDir, "certs"); ok {
			searchList = append(searchList, certsDir)
		}
	}

	for _, l := range searchList {
		d := filepath.Clean(l)
		var info os.FileInfo
		if info, err = os.Stat(d); err == nil && info.IsDir() {
			packagedCertDir = d
			dir = packagedCertDir
			return
		}
	}
	err = fmt.Errorf("couldn't locate packaged PEMs in any of %v", searchList)
	return
}