github.com/exercism/configlet@v3.9.3-0.20200318193232-c70be6269e71+incompatible/cmd/upgrade.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/exercism/cli/cli"
     7  	"github.com/exercism/configlet/ui"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  // upgradeCmd downloads and installs the most recent version of Configlet.
    12  var upgradeCmd = &cobra.Command{
    13  	Use:   "upgrade",
    14  	Short: "Upgrade to the latest version of Configlet.",
    15  	Long: `Upgrade to the latest version of Configlet.
    16  
    17  This finds and downloads the latest release, if you don't
    18  already have it.
    19  
    20  On Windows the old Configlet will be left on disk, marked as hidden.
    21  The next time you upgrade, the hidden file will be overwritten.
    22  You can always delete this file.
    23  	`,
    24  
    25  	Run: func(cmd *cobra.Command, args []string) {
    26  		runUpdate(configletCLI)
    27  	},
    28  }
    29  
    30  // runUpdate updates Configlet to the latest available version, if it is out of date.
    31  func runUpdate(c cli.Updater) {
    32  	ok, err := c.IsUpToDate()
    33  	if err != nil {
    34  		ui.PrintError(err)
    35  	}
    36  
    37  	if ok {
    38  		fmt.Println("Your CLI version is up to date.")
    39  		return
    40  	}
    41  
    42  	if err := c.Upgrade(); err != nil {
    43  		ui.PrintError(err)
    44  	}
    45  }
    46  
    47  func init() {
    48  	RootCmd.AddCommand(upgradeCmd)
    49  }