Skip to content

Commit

Permalink
Merge pull request #118 from DopplerHQ/tom_update_windows
Browse files Browse the repository at this point in the history
Support CLI updates on Windows via scoop
  • Loading branch information
Piccirello authored Aug 11, 2020
2 parents 2731dc0 + dc0f600 commit 3d39505
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func checkVersion(command string) {
// re-use existing version
versionCheck.LatestVersion = prevVersionCheck.LatestVersion
} else {
utils.Log(fmt.Sprintf("Doppler CLI %s is now available\n", versionCheck.LatestVersion))
utils.Log(fmt.Sprintf("Doppler CLI %s is now available, update via `doppler update`\n", versionCheck.LatestVersion))
}

configuration.SetVersionCheck(versionCheck)
Expand Down
32 changes: 18 additions & 14 deletions pkg/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package cmd

import (
"errors"
"fmt"

"github.com/DopplerHQ/cli/pkg/controllers"
Expand All @@ -28,10 +29,6 @@ var updateCmd = &cobra.Command{
Short: "update the Doppler CLI",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
if utils.IsWindows() {
utils.HandleError(fmt.Errorf("this command is not yet implemented for your operating system"))
}

force := utils.GetBoolFlag(cmd, "force")
available, _, err := controllers.NewVersionAvailable()
if err != nil {
Expand All @@ -47,17 +44,24 @@ var updateCmd = &cobra.Command{
}
}

// utils.Log(fmt.Sprintf("Doppler CLI %s is now available", check.LatestVersion))
utils.Log("Updating...")
wasUpdated, installedVersion, controllerErr := controllers.RunInstallScript()
if !controllerErr.IsNil() {
utils.HandleError(controllerErr.Unwrap(), controllerErr.Message)
}

if wasUpdated {
utils.Log(fmt.Sprintf("Doppler CLI was upgraded to %s!", installedVersion))
if utils.IsWindows() {
if controllers.IsInstalledViaScoop() {
controllers.UpdateViaScoop(force)
} else {
utils.HandleError(errors.New("Doppler CLI must be installed via Scoop"), "Unsupported install method")
}
} else {
utils.Log(fmt.Sprintf("You are already running the latest version"))
utils.Log("Updating...")
wasUpdated, installedVersion, controllerErr := controllers.RunInstallScript()
if !controllerErr.IsNil() {
utils.HandleError(controllerErr.Unwrap(), controllerErr.Message)
}

if wasUpdated {
utils.Log(fmt.Sprintf("Doppler CLI was upgraded to %s!", installedVersion))
} else {
utils.Log(fmt.Sprintf("You are already running the latest version"))
}
}
},
}
Expand Down
65 changes: 65 additions & 0 deletions pkg/controllers/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,68 @@ func RunInstallScript() (bool, string, Error) {

return wasUpdated, newVersion.String(), Error{}
}

// IsInstalledViaScoop checks whether the CLI was installed via Scoop on Windows
func IsInstalledViaScoop() bool {
command := []string{"scoop", "list", "doppler"}
out, err := exec.Command(command[0], command[1:]...).CombinedOutput() // #nosec G204
strOut := string(out)
if utils.Debug {
fmt.Println(fmt.Sprintf("Executing \"%s\"", strings.Join(command, " ")))
fmt.Println(strOut)
}

if err != nil {
return false
}

// Ex: `doppler 3.7.1 [doppler]`
re := regexp.MustCompile(`doppler\s+\d+\.\d+\.\d+\s+\[doppler\]`)
matches := re.FindStringSubmatch(strOut)
if matches != nil && len(matches) == 1 {
return true
}
return false
}

// UpdateViaScoop attempts to update the CLI via Scoop on Windows
func UpdateViaScoop(force bool) {
utils.Log("Updating via scoop...")
command := []string{"scoop", "update", "doppler"}
if force {
command = append(command, "-f")
}
out, err := exec.Command(command[0], command[1:]...).CombinedOutput() // #nosec G204
strOut := string(out)
// log output before checking error
if utils.Debug {
fmt.Println(fmt.Sprintf("Executing \"%s\"", strings.Join(command, " ")))
fmt.Println(strOut)
}

if err != nil {
utils.HandleError(err, "Unable to update the Doppler CLI")
}

// Ex: `doppler: 3.7.1 (latest version)`
re := regexp.MustCompile(`doppler:\s+\d+\.\d+\.\d+\s+\(latest version\)`)
if loc := re.FindStringIndex(strOut); loc != nil {
utils.Log("You are already running the latest version")
return
}

// Ex: `'doppler' (3.7.1) was installed successfully!`
re = regexp.MustCompile(`'doppler' \((\d+\.\d+\.\d+)\) was installed successfully!`)
matches := re.FindStringSubmatch(strOut)
if matches != nil && len(matches) == 2 {
if force && version.Normalize(matches[1]) == version.Normalize(version.ProgramVersion) {
utils.Log(fmt.Sprintf("Reinstalled Doppler CLI v%s!", matches[1]))
return
}

utils.Log(fmt.Sprintf("Doppler CLI was upgraded to v%s!", matches[1]))
return
}

utils.HandleError(errors.New("Unable to determine new CLI version"))
}

0 comments on commit 3d39505

Please sign in to comment.