-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
228 lines (187 loc) · 3.9 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"sort"
"github.com/spf13/cobra"
"golang.org/x/mod/semver"
)
const (
tmpDir = "_modmerge"
)
var (
outFile string
)
func init() {
cmd.Flags().StringVarP(&outFile, "outfile", "o", "go.mod.new", "The name of the output file.")
}
var cmd = &cobra.Command{
Use: "modmerge -o <file> [<files>...]",
Short: "Merge multiple go.mod files into a single file",
Long: "Merge multiple go.mod files into a single file",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("requires at least one go.mod file")
}
// check that we can open all files
for _, a := range args {
_, err := os.Stat(a)
if err != nil {
return fmt.Errorf("unable to open file %s - %s", a, err)
}
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
log.Println("reading module files...")
md, err := readModules(args)
if err != nil {
log.Fatal(err)
}
log.Println("merging module files...")
final := combineModules(md)
log.Printf("writng final result to %s...", outFile)
err = writeRes(args[0], final, outFile)
if err != nil {
log.Fatal(err)
}
log.Println("Successfully wrote merged modules to", outFile)
},
}
func writeRes(base string, mods map[string]string, dest string) error {
bb, err := ioutil.ReadFile(base)
if err != nil {
return err
}
dir, err := ioutil.TempDir("", tmpDir)
if err != nil {
return err
}
defer func() {
err := os.RemoveAll(dir)
if err != nil {
log.Println(err)
}
}()
err = ioutil.WriteFile(filepath.Join(dir, "go.mod"), bb, 0666)
if err != nil {
return err
}
// get a list of all package names
var pkgs []string
for m := range mods {
pkgs = append(pkgs, m)
}
sort.Strings(pkgs)
editArgs := []string{"mod", "edit"}
for _, mod := range pkgs {
editArgs = append(editArgs, "-require="+mod+"@"+mods[mod])
}
c := exec.Command("go", editArgs...)
c.Stderr = os.Stderr
c.Dir = dir
err = c.Run()
if err != nil {
return err
}
return copyFile(filepath.Join(dir, "go.mod"), dest)
}
func copyFile(in string, out string) error {
fb, err := ioutil.ReadFile(in)
if err != nil {
return err
}
err = ioutil.WriteFile(out, fb, 0666)
if err != nil {
return err
}
return nil
}
func combineModules(deps []map[string]string) map[string]string {
res := map[string]string{}
for _, d := range deps {
for pkg, version := range d {
maxVersion, ok := res[pkg]
if !ok || semver.Compare(version, maxVersion) > 0 {
res[pkg] = version
}
}
}
return res
}
func readModules(modFiles []string) ([]map[string]string, error) {
var deps []map[string]string
for _, mf := range modFiles {
log.Println("reading file:", mf)
md, err := ioutil.ReadFile(mf)
if err != nil {
return nil, err
}
mv, err := moduleVersions(md)
if err != nil {
return nil, err
}
deps = append(deps, mv)
}
return deps, nil
}
type mod struct {
Path string `json:"path"`
Version string `json:"version"`
}
func moduleVersions(modData []byte) (map[string]string, error) {
dir, err := ioutil.TempDir("", tmpDir)
if err != nil {
return nil, err
}
defer func() {
err := os.RemoveAll(dir)
if err != nil {
log.Println(err)
}
}()
err = ioutil.WriteFile(filepath.Join(dir, "go.mod"), modData, 0666)
if err != nil {
return nil, err
}
var out bytes.Buffer
c := exec.Command("go", "list", "-m", "-json", "all")
c.Stdout = &out
c.Stderr = os.Stderr
c.Dir = dir
err = c.Run()
if err != nil {
return nil, err
}
dec := json.NewDecoder(&out)
mods := map[string]string{}
for {
m := &mod{}
err := dec.Decode(m)
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
if m.Version != "" {
mods[m.Path] = m.Version
}
}
return mods, nil
}
// usage:
// modmerge <files>...
func main() {
if err := cmd.Execute(); err != nil {
log.Fatal(err)
}
}