File: path.go

package info (click to toggle)
vagrant 2.3.7%2Bgit20230731.5fc64cde%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 17,616 kB
  • sloc: ruby: 111,820; sh: 462; makefile: 123; ansic: 34; lisp: 1
file content (52 lines) | stat: -rw-r--r-- 1,376 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package config

import (
	"os"

	"github.com/hashicorp/vagrant-plugin-sdk/helper/path"
	"github.com/hashicorp/vagrant-plugin-sdk/helper/paths"
)

// 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 {
		dir, err = paths.VagrantCwd()
		if err != nil {
			return nil, err
		}
	}

	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()
	}
}