MCPcopy
hub / github.com/kptdev/kpt / IsInsideDir

Function IsInsideDir

internal/cmdexport/pathutil/pathutil.go:55–79  ·  view source on GitHub ↗

IsInsideDir checks if path is within directory. Both path and directory have to be absolute paths.

(path string, directory string)

Source from the content-addressed store, hash-verified

53// IsInsideDir checks if path is within directory.
54// Both path and directory have to be absolute paths.
55func IsInsideDir(path string, directory string) (isInside bool, err error) {
56 if !filepath.IsAbs(path) {
57 err = fmt.Errorf(
58 "argument `path` (%s) is not an absolute path",
59 path,
60 )
61 return
62 }
63 if !filepath.IsAbs(directory) {
64 err = fmt.Errorf(
65 "argument `directory` (%s) is not an absolute path",
66 directory,
67 )
68 return
69 }
70
71 rel, err := filepath.Rel(directory, path)
72 if err != nil {
73 return false, err
74 }
75
76 isInside = rel == "." || !strings.HasPrefix(rel, ".")
77
78 return
79}

Callers 2

CheckFnPathsMethod · 0.92
TestIsInsideDirectoryFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestIsInsideDirectoryFunction · 0.68