Skip to content

Commit

Permalink
refactor: improve GetBasePath to be more v2 ready
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Jan 23, 2025
1 parent 0a6243b commit 5f46982
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions pkg/fsutils/basepath.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fsutils
import (
"bytes"
"context"
"errors"
"fmt"
"os/exec"
"path/filepath"
Expand All @@ -23,7 +24,19 @@ func AllRelativePathModes() []string {
}

func GetBasePath(ctx context.Context, mode, cfgDir string) (string, error) {
if mode == "" {
// TODO(ldez): v2 the default should be cfg or gomod.
mode = RelativePathModeWd
}

switch mode {
case RelativePathModeCfg:
if cfgDir == "" {
return GetBasePath(ctx, RelativePathModeWd, cfgDir)
}

return cfgDir, nil

case RelativePathModeGoMod:
goMod, err := goenv.GetOne(ctx, goenv.GOMOD)
if err != nil {
Expand All @@ -40,21 +53,16 @@ func GetBasePath(ctx context.Context, mode, cfgDir string) (string, error) {

return root, nil

case RelativePathModeCfg:
if cfgDir == "" {
return "", fmt.Errorf("missing configuration directory")
}

return cfgDir, nil

default:
// mode "wd"
case RelativePathModeWd:
wd, err := Getwd()
if err != nil {
return "", fmt.Errorf("get wd: %w", err)
}

return wd, nil

default:
return "", errors.New("unknown relative path mode")
}
}

Expand Down

0 comments on commit 5f46982

Please sign in to comment.