Skip to content

Commit

Permalink
use flag.TextVar for --rewrite flag
Browse files Browse the repository at this point in the history
  • Loading branch information
icholy committed Sep 25, 2024
1 parent ad13f95 commit c7de2f1
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,15 @@ func listcmd(args []string) error {
}

func getcmd(args []string) error {
var dir, rewrite string
var rewrite regexp.Regexp
var dir string
var pre, cached, major bool
fset := flag.NewFlagSet("get", flag.ExitOnError)
fset.BoolVar(&pre, "pre", false, "allow non-v0 prerelease versions")
fset.BoolVar(&major, "major", false, "only get newer major versions")
fset.StringVar(&dir, "dir", ".", "working directory")
fset.BoolVar(&cached, "cached", true, "only fetch cached content from the module proxy")
fset.StringVar(&rewrite, "rewrite", "", "exact package version to upgrade")
fset.TextVar(&rewrite, "rewrite", regexp.MustCompile(".*"), "only rewrite imports matching this regex")
fset.Usage = func() {
fmt.Fprintln(os.Stderr, "Usage: gomajor get <pathspec>")
fset.PrintDefaults()
Expand Down Expand Up @@ -220,24 +221,17 @@ func getcmd(args []string) error {
if err := cmd.Run(); err != nil {
return err
}
var rewriteRegex *regexp.Regexp
if rewrite != "" {
if rewriteRegex, err = regexp.Compile(rewrite); err != nil {
return err
}
}
// rewrite imports
err = importpaths.RewriteModule(dir, importpaths.RewriteModuleOptions{
PkgDir: pkgdir,
Prefix: modprefix,
NewVersion: version,
OnRewrite: func(pos token.Position, oldpath, newpath string) error {
if rewriteRegex != nil && !rewriteRegex.MatchString(oldpath) {
if !rewrite.MatchString(oldpath) {
return importpaths.ErrSkip
}

fmt.Printf("%s %s\n", pos, newpath)

return nil
},
})
Expand Down

0 comments on commit c7de2f1

Please sign in to comment.