Skip to content

Commit 2a21052

Browse files
Check if major already exists before adding
1 parent 41a8005 commit 2a21052

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

major/major.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,28 @@ func Run(dir, op, modName string, tag int) error {
6262
// It would be nicer to use modFile.AddRequire
6363
// and modFile.DropRequire, but they do not
6464
// preserve block location.
65+
var majorExists bool
6566
for _, req := range modFile.Require {
66-
if req.Mod.Path == modName {
67-
req.Mod.Path = newModPath
68-
req.Mod.Version = "latest"
67+
if req.Mod.Path == newModPath {
68+
majorExists = true
6969
break
7070
}
7171
}
72-
modFile.SetRequire(modFile.Require)
72+
if majorExists {
73+
err = modFile.DropRequire(modName)
74+
if err != nil {
75+
return fmt.Errorf("error dropping %q: %w", modName, err)
76+
}
77+
} else {
78+
for _, req := range modFile.Require {
79+
if req.Mod.Path == modName {
80+
req.Mod.Path = newModPath
81+
req.Mod.Version = "latest"
82+
break
83+
}
84+
}
85+
modFile.SetRequire(modFile.Require)
86+
}
7387
} else {
7488
modFile.Module.Syntax.Token[1] = newModPath
7589
}

migrate/migrate.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"bytes"
66
"context"
77
"fmt"
8-
"io/ioutil"
98
"net/http"
109
"net/url"
1110
"os"
@@ -76,7 +75,7 @@ type resp struct {
7675

7776
func migrate(path string, gc *github.Client, test bool) error {
7877
fmt.Printf("git clone %v\n", path)
79-
tempdir, err := ioutil.TempDir("", strings.Replace(path, "/", "_", -1))
78+
tempdir, err := os.MkdirTemp("", strings.Replace(path, "/", "_", -1))
8079
if err != nil {
8180
return errors.Wrap(err, "tempdir err")
8281
}
@@ -168,7 +167,7 @@ func gitclone(url, tempdir string) (string, error) {
168167
if err != nil {
169168
return "", fmt.Errorf("output: %s", bts.String())
170169
}
171-
ff, err := ioutil.ReadDir(tempdir)
170+
ff, err := os.ReadDir(tempdir)
172171
if err != nil {
173172
return "", err
174173
}
@@ -360,5 +359,5 @@ func rewriteGitIgnore(dir string) error {
360359
return errors.Wrap(err, "error while scanning")
361360
}
362361
f.Close()
363-
return ioutil.WriteFile(p, []byte(strings.Join(lines, "\n")+"\n"), 0o666)
362+
return os.WriteFile(p, []byte(strings.Join(lines, "\n")+"\n"), 0o666)
364363
}

mod.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
package mod
22

33
import (
4-
"io/ioutil"
4+
"os"
55
"path/filepath"
66

7-
// "github.com/marwan-at-work/vgop/modfile"
87
"github.com/pkg/errors"
98
"golang.org/x/mod/modfile"
109
)
1110

1211
// GetModFile returns an AST of the given directory's go.mod file
1312
// and returns err if file is not found.
1413
func GetModFile(dir string) (*modfile.File, error) {
15-
bts, err := ioutil.ReadFile(filepath.Join(dir, "go.mod"))
14+
bts, err := os.ReadFile(filepath.Join(dir, "go.mod"))
1615
if err != nil {
1716
return nil, errors.Wrap(err, "could not open go.mod file")
1817
}

0 commit comments

Comments
 (0)