github.com/hhsnopek/up@v0.1.1/internal/cli/upgrade/upgrade.go (about)

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