Skip to content

Commit 5f46982

Browse files
committed
refactor: improve GetBasePath to be more v2 ready
1 parent 0a6243b commit 5f46982

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

Diff for: pkg/fsutils/basepath.go

+17-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package fsutils
33
import (
44
"bytes"
55
"context"
6+
"errors"
67
"fmt"
78
"os/exec"
89
"path/filepath"
@@ -23,7 +24,19 @@ func AllRelativePathModes() []string {
2324
}
2425

2526
func GetBasePath(ctx context.Context, mode, cfgDir string) (string, error) {
27+
if mode == "" {
28+
// TODO(ldez): v2 the default should be cfg or gomod.
29+
mode = RelativePathModeWd
30+
}
31+
2632
switch mode {
33+
case RelativePathModeCfg:
34+
if cfgDir == "" {
35+
return GetBasePath(ctx, RelativePathModeWd, cfgDir)
36+
}
37+
38+
return cfgDir, nil
39+
2740
case RelativePathModeGoMod:
2841
goMod, err := goenv.GetOne(ctx, goenv.GOMOD)
2942
if err != nil {
@@ -40,21 +53,16 @@ func GetBasePath(ctx context.Context, mode, cfgDir string) (string, error) {
4053

4154
return root, nil
4255

43-
case RelativePathModeCfg:
44-
if cfgDir == "" {
45-
return "", fmt.Errorf("missing configuration directory")
46-
}
47-
48-
return cfgDir, nil
49-
50-
default:
51-
// mode "wd"
56+
case RelativePathModeWd:
5257
wd, err := Getwd()
5358
if err != nil {
5459
return "", fmt.Errorf("get wd: %w", err)
5560
}
5661

5762
return wd, nil
63+
64+
default:
65+
return "", errors.New("unknown relative path mode")
5866
}
5967
}
6068

0 commit comments

Comments
 (0)