Skip to content

Commit

Permalink
buildutils: add FindDirUpwards
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGamba committed Jun 19, 2024
1 parent a30e626 commit 1208265
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions buildutils/buildutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,38 @@ func FindFileUpwards(ctx context.Context, filename string) (string, error) {
return "", fmt.Errorf("%w: %s", ErrNotFound, filename)
}

func FindDirUpwards(ctx context.Context, dir string) (string, error) {
cwd, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("failed to get cwd: %w", err)
}

check := func(dir string) bool {
base := filepath.Base(cwd)
if base == dir {
return true
}
return false
}
d := cwd
for {
found := check(d)
if found {
return d, nil
}
a, err := filepath.Abs(d)
if err != nil {
return "", fmt.Errorf("failed to get abs path: %w", err)
}
if a == "/" {
break
}
d = filepath.Join(d, "../")
}

return "", fmt.Errorf("%w: %s", ErrNotFound, dir)
}

func Touch(filename string) error {
_, err := os.Stat(filename)
if os.IsNotExist(err) {
Expand Down

0 comments on commit 1208265

Please sign in to comment.