-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
145 lines (132 loc) · 3.81 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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var log = logrus.New()
// Rootcmd is the root command
var RootCmd = &cobra.Command{
Use: "match-versions",
Short: "match-versions matches the go mod versions in one module to another",
Long: `match-versions takes a path to a go module (a package that has a
go.mod file) and changes the current module's go.mod file to import
the versions specified in the given module.`,
Run: Match,
}
func init() {
RootCmd.Flags().String("get", "", "path to module from which we are getting the versions")
RootCmd.Flags().String("set", "", "path of module to which we are setting the versions")
}
func main() {
if err := RootCmd.Execute(); err != nil {
log.Error(err)
}
}
// Match is the command for matching and updating go.mod files
func Match(cmd *cobra.Command, args []string) {
getPath, err := cmd.Flags().GetString("get")
if err != nil {
log.Error(err)
return
}
setPath, err := cmd.Flags().GetString("set")
if err != nil {
log.Error(err)
return
}
if getPath == "" || setPath == "" {
log.Error("the 'get' flag and 'set' flag must both be set")
return
}
if _, err := os.Stat(getPath); os.IsNotExist(err) {
log.Errorf("directory '%s' does not exist", getPath)
return
}
getGoModPath := filepath.Join(getPath, "go.mod")
if _, err := os.Stat(getGoModPath); os.IsNotExist(err) {
log.Errorf("go.mod file does not exist at '%s'", getPath)
return
}
if _, err := os.Stat(setPath); os.IsNotExist(err) {
log.Errorf("directory '%s' does not exist", setPath)
return
}
setGoModPath := filepath.Join(setPath, "go.mod")
if _, err := os.Stat(setGoModPath); os.IsNotExist(err) {
log.Errorf("go.mod file does not exist at '%s'", setPath)
return
}
// parse gomod file and print all requireds
getMap, _, err := parseFileToRequireMap(getGoModPath)
if err != nil {
log.Errorf("error parsing get go.mod file: %s", err)
return
}
setMap, setKeys, err := parseFileToRequireMap(setGoModPath)
if err != nil {
log.Errorf("error parsing set go.mod file: %s", err)
}
for _, key := range setKeys {
val, ok := getMap[key]
if !ok {
continue
}
setMap[key] = val
}
encodeModFile(setGoModPath, setMap, setKeys)
}
func parseFileToRequireMap(path string) (map[string]string, []string, error) {
bytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, nil, err
}
modString := string(bytes)
openIndex := strings.Index(modString, "require (")
if openIndex < 0 {
return nil, nil, fmt.Errorf("could not find the list of required imports")
}
modString = modString[openIndex+9:]
closeIndex := strings.Index(modString, ")")
if closeIndex < 0 {
return nil, nil, fmt.Errorf("go.mod file contains formatting errors")
}
modString = modString[:closeIndex]
replaceArray := strings.Split(strings.TrimSpace(modString), "\n")
replaceMap := map[string]string{}
for i, replace := range replaceArray {
split := strings.SplitN(strings.TrimSpace(replace), " ", 2)
replaceArray[i] = split[0]
replaceMap[split[0]] = split[1]
}
return replaceMap, replaceArray, nil
}
func encodeModFile(path string, requireMap map[string]string, requireKeys []string) error {
bytes, err := ioutil.ReadFile(path)
if err != nil {
return err
}
modString := string(bytes)
openIndex := strings.Index(modString, "require (")
if openIndex < 0 {
return fmt.Errorf("could not find the list of required imports")
}
start := modString[:openIndex+9]
closeIndex := strings.Index(modString, ")")
if closeIndex < 0 {
return fmt.Errorf("go.mod file contains formatting errors")
}
end := modString[closeIndex:]
require := ""
for _, key := range requireKeys {
require += fmt.Sprintf("\n\t%s %s", key, requireMap[key])
}
require += "\n"
start += require
start += end
return ioutil.WriteFile(path, []byte(start), 0644)
}