-
Notifications
You must be signed in to change notification settings - Fork 3
/
modfile.go
64 lines (58 loc) · 2.03 KB
/
modfile.go
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
53
54
55
56
57
58
59
60
61
62
63
64
package workspace
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/spf13/viper"
"github.com/turbot/pipe-fittings/app_specific"
"github.com/turbot/pipe-fittings/constants"
"github.com/turbot/pipe-fittings/error_helpers"
)
// FindModFilePath search up the directory tree to find the modfile
func FindModFilePath(folder string) (string, error) {
folder, err := filepath.Abs(folder)
if err != nil {
return "", err
}
for _, modFilePath := range app_specific.ModFilePaths(folder) {
_, err = os.Stat(modFilePath)
if err == nil {
// found the modfile
return modFilePath, nil
}
}
// if the file wasn't found, search in the parent directory
parent := filepath.Dir(folder)
if folder == parent {
// this typically means that we are already in the root directory
return "", ErrorNoModDefinition
}
return FindModFilePath(filepath.Dir(folder))
}
func HomeDirectoryModfileCheck(ctx context.Context, workspacePath string) error {
// bypass all the checks if ConfigKeyBypassHomeDirModfileWarning is set - it means home dir modfile check
// has already happened before
if viper.GetBool(constants.ConfigKeyBypassHomeDirModfileWarning) {
return nil
}
// get the cmd and home dir
home, _ := os.UserHomeDir()
var modFileExists bool
for _, modFilePath := range app_specific.ModFilePaths(workspacePath) {
if _, err := os.Stat(modFilePath); err != nil {
modFileExists = true
}
}
// check if your workspace path is home dir and if modfile exists
if workspacePath == home && modFileExists {
// for other cmds - if home dir has modfile, just warn
defaultModFileName := app_specific.ModFileNames()[0]
warningText := fmt.Sprintf("You have a %s file in your home directory. This is not recommended.\nAs a result, %s will try to load all the files in home and its sub-directories, which can cause performance issues.\nBest practice is to put %s files in their own directories.\nHit Ctrl+C to stop.\n",
defaultModFileName,
app_specific.AppName,
defaultModFileName)
error_helpers.ShowWarning(warningText)
}
return nil
}