Unverified Commit 6ded477a authored by Mustaque Ahmed's avatar Mustaque Ahmed Committed by GitHub
Browse files

feat: support upgrade command (#809)


* feat: add `upgrade` command in the cmd directory

this is a wrapper around the main implementation and it plugs-in with
the cobra command.

* feat: add a pkg to support `datree upgrade` command

In this pkg we are going to write all types of helper and nested utils
methods that can be used to support the upgrade process

* feat: add `upgrade` command in the main cobra command file

* fix lint error - imports

* remove `upgrade` method from cmd file

* fix lint errors

* revert lint fix - unrelated files

* working `upgrade` command

* print success message after upgrade

* refactor: `CheckIfDatreeInstalledUsingBrew` datree

* fix: return if error encountered

* add newline and fix lint errors

* assign `runtime.GOARCH` directly to arch variable

* refactor: remove golang logic and implement fetching shell script and executing it

* Use camelCase convention - variable name
Co-authored-by: default avatarshalev007 <51760613+shalev007@users.noreply.github.com>

* use camelCase `errMsg`

* fix spelling mistake
Co-authored-by: default avatarRoy Hadad <39004075+royhadad@users.noreply.github.com>

* change script URL to `https://get.datree.io`



* resolve conflicts

* rewrite `upgrade logic` - make it simpler

* refactor: remove extra code and write one-liner command

* remove duplicate code

* add check for windows OS

* fix check for windows OS

* fix: print out stderr as well
Co-authored-by: default avatarshalev007 <51760613+shalev007@users.noreply.github.com>
Co-authored-by: default avatarRoy Hadad <39004075+royhadad@users.noreply.github.com>
Co-authored-by: default avatarRoy Hadad <roy@datree.io>
parent 7f6c8916
Showing with 82 additions and 0 deletions
+82 -0
......@@ -14,6 +14,7 @@ import (
"github.com/datreeio/datree/cmd/publish"
schemaValidator "github.com/datreeio/datree/cmd/schema-validator"
"github.com/datreeio/datree/cmd/test"
"github.com/datreeio/datree/cmd/upgrade"
"github.com/datreeio/datree/cmd/version"
"github.com/datreeio/datree/pkg/ciContext"
"github.com/datreeio/datree/pkg/cliClient"
......@@ -70,6 +71,12 @@ func NewRootCommand(app *App) *cobra.Command {
Printer: app.Context.Printer,
}))
rootCmd.AddCommand(upgrade.New(&upgrade.UpgradeCommandContext{
CliVersion: CliVersion,
Printer: app.Context.Printer,
UpgradeCliClient: app.Context.CliClient,
}))
rootCmd.AddCommand(config.New(&config.ConfigCommandContext{
CliVersion: CliVersion,
Messager: app.Context.Messager,
......
package upgrade
import (
"fmt"
"github.com/datreeio/datree/pkg/cliClient"
upgrademanager "github.com/datreeio/datree/pkg/upgradeManager"
"github.com/spf13/cobra"
)
type Printer interface {
PrintMessage(messageText string, messageColor string)
}
type UpgradeCommandContext struct {
CliVersion string
Printer Printer
UpgradeCliClient *cliClient.CliClient
}
func New(ctx *UpgradeCommandContext) *cobra.Command {
m := upgrademanager.NewUpgradeManager()
var upgradeCmd = &cobra.Command{
Use: "upgrade",
Short: "Upgrade datree to the latest version",
Long: "Upgrade datree to the latest version",
Run: func(cmd *cobra.Command, args []string) {
if m.CheckIfDatreeInstalledUsingBrew() {
ctx.Printer.PrintMessage("Looks like you installed Datree via brew, to upgrade datree run: 'brew upgrade datree'\n", "error")
return
}
if m.CheckIfOsIsWindows() {
ctx.Printer.PrintMessage("Looks like you are using windows OS, to upgrade datree run: 'iwr -useb https://get.datree.io/windows_install.ps1 | iex'\n", "error")
return
}
err := m.Upgrade()
if err != nil {
errMsg := fmt.Sprintf("Failed to upgrade datree to the latest version, error encountered:\n%s\n", err)
ctx.Printer.PrintMessage(errMsg, "error")
}
},
}
return upgradeCmd
}
package upgrademanager
import (
"os"
"os/exec"
"runtime"
"strings"
)
type UpgradeManager struct {
}
func NewUpgradeManager() *UpgradeManager {
return &UpgradeManager{}
}
func (m *UpgradeManager) CheckIfDatreeInstalledUsingBrew() bool {
_, err := exec.Command("brew", "list", "datree").Output()
return err == nil
}
func (m *UpgradeManager) CheckIfOsIsWindows() bool {
return strings.Contains(runtime.GOOS, "windows")
}
func (m *UpgradeManager) Upgrade() error {
oneLineInstallationCommand := exec.Command("bash", "-c", "curl https://get.datree.io | /bin/bash")
oneLineInstallationCommand.Stdout = os.Stdout
oneLineInstallationCommand.Stderr = os.Stderr
return oneLineInstallationCommand.Run()
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment