diff --git a/major/major.go b/major/major.go index 2283e4e..ecd6e86 100644 --- a/major/major.go +++ b/major/major.go @@ -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 } diff --git a/migrate/migrate.go b/migrate/migrate.go index ae17292..9cb97c0 100644 --- a/migrate/migrate.go +++ b/migrate/migrate.go @@ -5,7 +5,6 @@ import ( "bytes" "context" "fmt" - "io/ioutil" "net/http" "net/url" "os" @@ -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") } @@ -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 } @@ -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) } diff --git a/mod.go b/mod.go index 3a0f300..f6c1c1e 100644 --- a/mod.go +++ b/mod.go @@ -1,10 +1,9 @@ package mod import ( - "io/ioutil" + "os" "path/filepath" - // "github.com/marwan-at-work/vgop/modfile" "github.com/pkg/errors" "golang.org/x/mod/modfile" ) @@ -12,7 +11,7 @@ import ( // 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") }