Paul Hinze 30774c7345
Fix VAGRANT_CWD handling
VAGRANT_CWD was not being honored in the Go-side Vagrantfile-finding
code. Add that in and also fix a couple of bugs in the path walking
logic.
2022-04-25 12:26:12 -05:00

63 lines
1.6 KiB
Go

package config
import (
"fmt"
"os"
"github.com/hashicorp/vagrant-plugin-sdk/helper/path"
)
// Filename is the default filename for the Vagrant configuration.
const Filename = "Vagrantfile"
func GetVagrantfileName() string {
if f := os.Getenv("VAGRANT_VAGRANTFILE"); f != "" {
return f
}
return Filename
}
// FindPath looks for our configuration file starting at "start" and
// traversing parent directories until it is found. If it is found, the
// path is returned. If it is not found, an empty string is returned.
// Error will be non-nil only if an error occurred.
//
// If start is empty, start will be the current working directory. If
// filename is empty, it will default to the Filename constant.
func FindPath(dir path.Path, filename string) (p path.Path, err error) {
if dir == nil {
cwd, ok := os.LookupEnv("VAGRANT_CWD")
if ok {
if _, err := os.Stat(cwd); os.IsNotExist(err) {
return nil, fmt.Errorf("VAGRANT_CWD set to path (%s) that does not exist", cwd)
} else {
dir = path.NewPath(cwd)
}
} else {
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
dir = path.NewPath(cwd)
}
}
if filename == "" {
filename = GetVagrantfileName()
}
p = dir
for {
p = p.Join(filename)
if _, err = os.Stat(p.String()); err == nil || !os.IsNotExist(err) {
return
}
// since we just tacked a filename on above, the first Parent() call is
// the directory of the file and the second is the actual parent dir
if p.Parent().String() == p.Parent().Parent().String() {
return nil, nil
}
p = p.Parent().Parent()
}
}