Skip to content

Commit

Permalink
Check if major already exists before adding
Browse files Browse the repository at this point in the history
  • Loading branch information
marwan-at-work committed Nov 6, 2023
1 parent 41a8005 commit 2a21052
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
22 changes: 18 additions & 4 deletions major/major.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,28 @@ func Run(dir, op, modName string, tag int) error {
// It would be nicer to use modFile.AddRequire
// and modFile.DropRequire, but they do not
// preserve block location.
var majorExists bool
for _, req := range modFile.Require {
if req.Mod.Path == modName {
req.Mod.Path = newModPath
req.Mod.Version = "latest"
if req.Mod.Path == newModPath {
majorExists = true
break
}
}
modFile.SetRequire(modFile.Require)
if majorExists {
err = modFile.DropRequire(modName)
if err != nil {
return fmt.Errorf("error dropping %q: %w", modName, err)
}
} else {
for _, req := range modFile.Require {
if req.Mod.Path == modName {
req.Mod.Path = newModPath
req.Mod.Version = "latest"
break
}
}
modFile.SetRequire(modFile.Require)
}
} else {
modFile.Module.Syntax.Token[1] = newModPath
}
Expand Down
7 changes: 3 additions & 4 deletions migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -76,7 +75,7 @@ type resp struct {

func migrate(path string, gc *github.Client, test bool) error {
fmt.Printf("git clone %v\n", path)
tempdir, err := ioutil.TempDir("", strings.Replace(path, "/", "_", -1))
tempdir, err := os.MkdirTemp("", strings.Replace(path, "/", "_", -1))
if err != nil {
return errors.Wrap(err, "tempdir err")
}
Expand Down Expand Up @@ -168,7 +167,7 @@ func gitclone(url, tempdir string) (string, error) {
if err != nil {
return "", fmt.Errorf("output: %s", bts.String())
}
ff, err := ioutil.ReadDir(tempdir)
ff, err := os.ReadDir(tempdir)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -360,5 +359,5 @@ func rewriteGitIgnore(dir string) error {
return errors.Wrap(err, "error while scanning")
}
f.Close()
return ioutil.WriteFile(p, []byte(strings.Join(lines, "\n")+"\n"), 0o666)
return os.WriteFile(p, []byte(strings.Join(lines, "\n")+"\n"), 0o666)
}
5 changes: 2 additions & 3 deletions mod.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package mod

import (
"io/ioutil"
"os"
"path/filepath"

// "github.com/marwan-at-work/vgop/modfile"
"github.com/pkg/errors"
"golang.org/x/mod/modfile"
)

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

0 comments on commit 2a21052

Please sign in to comment.