github.com/orteth01/up@v0.2.0/internal/cli/upgrade/upgrade.go (about) 1 package upgrade 2 3 import ( 4 "fmt" 5 "runtime" 6 "time" 7 8 "github.com/pkg/errors" 9 "github.com/tj/go-update" 10 "github.com/tj/go/term" 11 "github.com/tj/kingpin" 12 13 "github.com/apex/up/internal/cli/root" 14 "github.com/apex/up/internal/progressreader" 15 "github.com/apex/up/internal/stats" 16 "github.com/apex/up/internal/util" 17 ) 18 19 func init() { 20 cmd := root.Command("upgrade", "Install the latest release of Up.") 21 cmd.Action(func(_ *kingpin.ParseContext) error { 22 version := root.Cmd.GetVersion() 23 start := time.Now() 24 25 defer util.Pad()() 26 term.HideCursor() 27 defer term.ShowCursor() 28 29 // update polls(1) from tj/gh-polls on github 30 p := &update.Project{ 31 Owner: "apex", 32 Repo: "up", 33 Command: "up", 34 Version: version, 35 } 36 37 // fetch the new releases 38 releases, err := p.LatestReleases() 39 if err != nil { 40 return errors.Wrap(err, "fetching releases") 41 } 42 43 // no updates 44 if len(releases) == 0 { 45 fmt.Printf(" No updates required, you're good :)\n") 46 return nil 47 } 48 49 // latest release 50 latest := releases[0] 51 52 // find the tarball for this system 53 a := latest.FindTarball(runtime.GOOS, runtime.GOARCH) 54 if a == nil { 55 return errors.Errorf("failed to find a binary for %s %s", runtime.GOOS, runtime.GOARCH) 56 } 57 58 // download tarball to a tmp dir 59 tarball, err := a.DownloadProxy(progressreader.New) 60 if err != nil { 61 return errors.Wrap(err, "downloading tarball") 62 } 63 64 // install it 65 if err := p.Install(tarball); err != nil { 66 return errors.Wrap(err, "installing") 67 } 68 69 term.ClearAll() 70 fmt.Printf("\n Updated %s to %s :)\n", version, latest.Version) 71 72 stats.Track("Upgrade", map[string]interface{}{ 73 "from": version, 74 "to": latest.Version, 75 "duration": time.Since(start).Round(time.Millisecond), 76 }) 77 78 return nil 79 }) 80 }