-
Notifications
You must be signed in to change notification settings - Fork 17
/
mod.go
42 lines (35 loc) · 974 Bytes
/
mod.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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os/exec"
)
type modInfo struct {
Path string `json:"Path"` // module name
Dir string `json:"Dir"` // absolute path to the module
GoMod string `json:"GoMod"` // absolute path to the go.mod
GoVersion string `json:"GoVersion"`
Main bool `json:"Main"`
}
func getModuleInfo(dir string) (modInfo, error) {
// https://github.com/golang/go/issues/44753#issuecomment-790089020
cmd := exec.Command("go", "list", "-m", "-json")
if dir != "" {
cmd.Dir = dir
}
raw, err := cmd.CombinedOutput()
if err != nil {
return modInfo{}, 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 modInfo{}, fmt.Errorf("unmarshaling error: %w: %s", err, string(raw))
}
if v.GoMod == "" {
return modInfo{}, errors.New("working directory is not part of a module")
}
return v, nil
}