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
|
package gomoddirectives
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"golang.org/x/mod/modfile"
)
type modInfo struct {
Path string `json:"Path"`
Dir string `json:"Dir"`
GoMod string `json:"GoMod"`
GoVersion string `json:"GoVersion"`
Main bool `json:"Main"`
}
// GetModuleFile gets module file.
func GetModuleFile() (*modfile.File, error) {
// https://github.com/golang/go/issues/44753#issuecomment-790089020
cmd := exec.Command("go", "list", "-m", "-json")
raw, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("command go list: %w: %s", err, string(raw))
}
var v modInfo
err = json.NewDecoder(bytes.NewBuffer(raw)).Decode(&v)
if err != nil {
return nil, fmt.Errorf("unmarshaling error: %w: %s", err, string(raw))
}
if v.GoMod == "" {
return nil, errors.New("working directory is not part of a module")
}
raw, err = os.ReadFile(v.GoMod)
if err != nil {
return nil, fmt.Errorf("reading go.mod file: %w", err)
}
return modfile.Parse("go.mod", raw, nil)
}
|