-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_deps.go
57 lines (50 loc) · 1.21 KB
/
module_deps.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
package depbump
import (
"encoding/json"
"os"
"path/filepath"
"github.com/yyle88/erero"
"github.com/yyle88/osexec"
"github.com/yyle88/osexistpath"
"golang.org/x/mod/modfile"
)
type Module struct {
Path string `json:"Path"`
}
type Require struct {
Path string `json:"Path"`
Version string `json:"Version"`
Indirect bool `json:"Indirect"`
}
type ModInfo struct {
Module *Module `json:"Module"`
Go string `json:"Go"`
Require []*Require `json:"Require"`
}
func GetModInfo(projectPath string) (*ModInfo, error) {
output, err := osexec.ExecInPath(projectPath, "go", "mod", "edit", "-json")
if err != nil {
return nil, erero.Wro(err)
}
var modInfo ModInfo
if err := json.Unmarshal(output, &modInfo); err != nil {
return nil, erero.Wro(err)
}
return &modInfo, nil
}
func ParseModuleFile(projectPath string) (*modfile.File, error) {
const fileName = "go.mod"
modPath, err := osexistpath.FILE(filepath.Join(projectPath, fileName))
if err != nil {
return nil, erero.Wro(err)
}
modData, err := os.ReadFile(modPath)
if err != nil {
return nil, erero.Wro(err)
}
modFile, err := modfile.Parse(fileName, modData, nil)
if err != nil {
return nil, erero.Wro(err)
}
return modFile, nil
}