Skip to content

Commit

Permalink
implement update command
Browse files Browse the repository at this point in the history
  • Loading branch information
icholy committed Dec 15, 2022
1 parent de39176 commit 7e41ad3
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ The commands are:
get upgrade to a major version
list list available updates
update upgrade all modules
path modify the module path
help show this help text
`
Expand All @@ -45,6 +46,10 @@ func main() {
if err := listcmd(flag.Args()[1:]); err != nil {
log.Fatal(err)
}
case "update":
if err := updatecmd(flag.Args()[1:]); err != nil {
log.Fatal(err)
}
case "path":
if err := pathcmd(flag.Args()[1:]); err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -160,6 +165,60 @@ func getcmd(args []string) error {
})
}

func updatecmd(args []string) error {
var dir string
var pre, cached, major bool
fset := flag.NewFlagSet("update", flag.ExitOnError)
fset.BoolVar(&pre, "pre", false, "allow non-v0 prerelease versions")
fset.StringVar(&dir, "dir", ".", "working directory")
fset.BoolVar(&cached, "cached", true, "only fetch cached content from the module proxy")
fset.BoolVar(&major, "major", false, "only show newer major versions")
fset.Usage = func() {
fmt.Fprintln(os.Stderr, "Usage: gomajor update")
fset.PrintDefaults()
}
fset.Parse(args)
dependencies, err := packages.Direct(dir)
if err != nil {
return err
}
modproxy.Updates(modproxy.UpdateOptions{
Pre: pre,
Major: major,
Cached: cached,
Modules: dependencies,
OnUpdate: func(u modproxy.Update) {
if u.Err != nil {
fmt.Fprintf(os.Stderr, "%s: failed: %v\n", u.Module.Path, u.Err)
return
}
// go get
modprefix := packages.ModPrefix(u.Module.Path)
spec := packages.JoinPath(modprefix, u.Version, "") + "@" + u.Version
fmt.Println("go get", spec)
cmd := exec.Command("go", "get", spec)
cmd.Dir = dir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return
}
// rewrite import paths
err := importpaths.RewriteModule(dir, importpaths.RewriteModuleOptions{
Prefix: modprefix,
NewVersion: u.Version,
OnRewrite: func(pos token.Position, _, newpath string) {
fmt.Printf("%s %s\n", pos, newpath)
},
})
if err != nil {
fmt.Fprintf(os.Stderr, "rewrite failed: %v", err)
}
},
})
return nil
}

func pathcmd(args []string) error {
var dir, version string
var next bool
Expand Down

0 comments on commit 7e41ad3

Please sign in to comment.