Skip to content

Commit

Permalink
Merge pull request #416 from DopplerHQ/winget-update
Browse files Browse the repository at this point in the history
Support updating CLI installed via winget
  • Loading branch information
Piccirello authored Jul 11, 2023
2 parents c667d71 + 5892390 commit ebd045a
Show file tree
Hide file tree
Showing 11 changed files with 347 additions and 136 deletions.
17 changes: 16 additions & 1 deletion INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,24 @@ Alternatively, you can install the CLI via [shell script](#linuxmacosbsd-shell-s

## Windows

### Winget

Using winget is recommended:

```sh
$ winget install doppler
$ doppler --version
```

To update:

```sh
$ winget upgrade doppler
```

### Scoop

Using [scoop](https://scoop.sh/) is recommended:
Using [scoop](https://scoop.sh/) is supported:

```sh
$ scoop bucket add doppler https://github.com/DopplerHQ/scoop-doppler.git
Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,17 @@ For installation without brew, see the [Install](INSTALL.md#macos) page.

### Windows

Using [scoop](https://scoop.sh/) is recommended:
Using winget is recommended:

```sh
$ scoop bucket add doppler https://github.com/DopplerHQ/scoop-doppler.git
$ scoop install doppler
$ winget install doppler
$ doppler --version
```

To update:

```sh
$ scoop update doppler
$ winget upgrade doppler
```

For additional options, see the [Install](INSTALL.md#windows) page.
Expand Down
62 changes: 3 additions & 59 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"fmt"
"os"
"sync"
"time"

"github.com/DopplerHQ/cli/pkg/configuration"
"github.com/DopplerHQ/cli/pkg/controllers"
Expand Down Expand Up @@ -66,7 +65,9 @@ var rootCmd = &cobra.Command{
// --plain doesn't normally affect logging output, but due to legacy reasons it does here
// also don't want to display updates if user doesn't want to be prompted (--no-prompt/--no-interactive)
if isTTY && utils.CanLogInfo() && !plain && canPrompt {
checkVersion(cmd.CommandPath())
if available, latestVersion := controllers.CheckUpdate(cmd.CommandPath()); available {
controllers.PromptToUpdate(latestVersion)
}
}
},
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -77,63 +78,6 @@ var rootCmd = &cobra.Command{
},
}

func checkVersion(command string) {
// disable version checking on commands commonly used in production workflows
// also disable when explicitly calling 'update' command to avoid checking twice
disabledCommands := []string{"run", "secrets download", "update"}
for _, disabledCommand := range disabledCommands {
if command == fmt.Sprintf("doppler %s", disabledCommand) {
utils.LogDebug("Skipping CLI upgrade check due to disallowed command")
return
}
}

if !version.PerformVersionCheck || version.IsDevelopment() {
return
}

prevVersionCheck := configuration.VersionCheck()
// don't check more often than every 24 hours
if !time.Now().After(prevVersionCheck.CheckedAt.Add(24 * time.Hour)) {
return
}

controllers.CaptureEvent("VersionCheck", nil)

available, versionCheck, err := controllers.NewVersionAvailable(prevVersionCheck)
if err != nil {
// retry on next run
return
}

if !available {
utils.LogDebug("No CLI updates available")
// re-use existing version
versionCheck.LatestVersion = prevVersionCheck.LatestVersion
} else if utils.IsWindows() {
utils.Log(fmt.Sprintf("Update: Doppler CLI %s is available\n\nYou can update via 'scoop update doppler'\n", versionCheck.LatestVersion))
} else {
controllers.CaptureEvent("UpgradeAvailable", nil)

utils.Print(color.Green.Sprintf("An update is available."))

changes, apiError := controllers.CLIChangeLog()
if apiError.IsNil() {
printer.ChangeLog(changes, 1, false)
utils.Print("")
}

prompt := fmt.Sprintf("Install Doppler CLI %s", versionCheck.LatestVersion)
if utils.ConfirmationPrompt(prompt, true) {
controllers.CaptureEvent("UpgradeFromPrompt", nil)

installCLIUpdate()
}
}

configuration.SetVersionCheck(versionCheck)
}

// persistentValidArgsFunction Cobra parses flags after executing ValidArgsFunction, so we must manually initialize flags
func persistentValidArgsFunction(cmd *cobra.Command) {
// more info https://github.com/spf13/cobra/issues/1291
Expand Down
38 changes: 4 additions & 34 deletions pkg/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ limitations under the License.
package cmd

import (
"fmt"

"github.com/DopplerHQ/cli/pkg/controllers"
"github.com/DopplerHQ/cli/pkg/models"
"github.com/DopplerHQ/cli/pkg/printer"
"github.com/DopplerHQ/cli/pkg/utils"
"github.com/spf13/cobra"
)
Expand All @@ -30,52 +27,25 @@ var updateCmd = &cobra.Command{
Short: "Update the Doppler CLI",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
if !utils.CanUpdate() {
utils.HandleError(fmt.Errorf("this command is not yet implemented for your operating system"))
}

force := utils.GetBoolFlag(cmd, "force")
available, _, err := controllers.NewVersionAvailable(models.VersionCheck{})
available, version, err := controllers.NewVersionAvailable(models.VersionCheck{})
if err != nil {
utils.HandleError(err, "Unable to check for CLI updates")
}

if !available {
if force {
utils.Log(fmt.Sprintf("Already running the latest version but proceeding anyway due to --force flag"))
utils.Log("Already running the latest version but proceeding anyway due to --force flag")
} else {
utils.Print(fmt.Sprintf("You are already running the latest version"))
utils.Print("You are already running the latest version")
return
}
}

installCLIUpdate()
controllers.InstallUpdate(version.LatestVersion)
},
}

func installCLIUpdate() {
utils.Print("Updating...")
wasUpdated, installedVersion, controllerErr := controllers.RunInstallScript()
if !controllerErr.IsNil() {
utils.HandleError(controllerErr.Unwrap(), controllerErr.Message)
}

if wasUpdated {
utils.Print(fmt.Sprintf("Installed CLI %s", installedVersion))

if changes, apiError := controllers.CLIChangeLog(); apiError.IsNil() {
utils.Print("\nWhat's new:")
printer.ChangeLog(changes, 1, false)
utils.Print("\nTip: run 'doppler changelog' to see all latest changes")
}

utils.Print("")
} else {
utils.Print(fmt.Sprintf("You are already running the latest version"))
}

}

func init() {
updateCmd.Flags().BoolP("force", "f", false, "install the latest CLI regardless of whether there's an update available")
rootCmd.AddCommand(updateCmd)
Expand Down
Loading

0 comments on commit ebd045a

Please sign in to comment.