github.com/rainforestapp/rainforest-cli@v2.12.0+incompatible/update.go (about)

     1  package main
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/equinox-io/equinox"
     7  	"github.com/urfave/cli"
     8  )
     9  
    10  const equinoxAppID = "app_carcVJmQBRm"
    11  
    12  // publicKey is a ECDSA key used to sign the cli binaries
    13  var publicKey = []byte(`
    14  -----BEGIN ECDSA PUBLIC KEY-----
    15  MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEoTjUnZL6bSXdEV9LKD+0zNekogPp8lwf
    16  8s/auLQXwCHFii7fH8bLwSW+4a9+eF8bWo8FYk4pfSo3WT5DUqGl9qHcnv22MMCK
    17  eiFH+GffIMk09RFqkcMh5rIPu3ykm5V8
    18  -----END ECDSA PUBLIC KEY-----
    19  `)
    20  
    21  func update(channel string, silent bool) error {
    22  	opts := equinox.Options{}
    23  	if channel == "" && releaseChannel != "" {
    24  		opts.Channel = releaseChannel
    25  	} else if channel != "" {
    26  		opts.Channel = channel
    27  	} else {
    28  		// fallback to stable
    29  		opts.Channel = "stable"
    30  	}
    31  
    32  	if err := opts.SetPublicKeyPEM(publicKey); err != nil {
    33  		return err
    34  	}
    35  
    36  	// check for the update
    37  	if !silent {
    38  		log.Printf("Checking for update on %v channel.", opts.Channel)
    39  	}
    40  	resp, err := equinox.Check(equinoxAppID, opts)
    41  	switch {
    42  	case err == equinox.NotAvailableErr:
    43  		if !silent {
    44  			log.Println("No update available, already at the latest version!")
    45  		}
    46  		return nil
    47  	case err != nil:
    48  		return err
    49  	}
    50  
    51  	// fetch the update and apply it
    52  	if !silent {
    53  		log.Print("Found a cli update, applying it.")
    54  	}
    55  	err = resp.Apply()
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	if !silent {
    61  		log.Printf("Updated to new version: %s!\n", resp.ReleaseVersion)
    62  	}
    63  	return nil
    64  }
    65  
    66  func updateCmd(c cliContext) error {
    67  	channel := c.Args().First()
    68  	if !(channel == "beta" || channel == "stable" || channel == "") {
    69  		return cli.NewExitError("Invalid release channel - use 'stable' or 'beta'", 1)
    70  	}
    71  	err := update(channel, false)
    72  	if err != nil {
    73  		return cli.NewExitError(err.Error(), 1)
    74  	}
    75  	return nil
    76  }
    77  
    78  func autoUpdate(c cliContext, updateFinishedChan chan<- struct{}) {
    79  	if !c.Bool("skip-update") {
    80  		update("", true)
    81  	}
    82  	updateFinishedChan <- struct{}{}
    83  }