-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmodule.go
More file actions
35 lines (28 loc) · 694 Bytes
/
Copy pathmodule.go
File metadata and controls
35 lines (28 loc) · 694 Bytes
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
package gomoddirectives
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/ldez/grignotin/goenv"
"golang.org/x/mod/modfile"
)
// GetModuleFile gets module file.
func GetModuleFile() (*modfile.File, error) {
goMod, err := goenv.GetOne(context.Background(), goenv.GOMOD)
if err != nil {
return nil, err
}
mod, err := parseGoMod(goMod)
if err != nil {
return nil, fmt.Errorf("failed to parse go.mod (%s): %w", goMod, err)
}
return mod, nil
}
func parseGoMod(goMod string) (*modfile.File, error) {
raw, err := os.ReadFile(filepath.Clean(goMod))
if err != nil {
return nil, fmt.Errorf("reading go.mod file: %w", err)
}
return modfile.Parse("go.mod", raw, nil)
}