Skip to content

Commit

Permalink
feat: upgrade cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
cchalop1 committed Jul 9, 2022
1 parent c13600d commit 62e8dc5
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
64 changes: 64 additions & 0 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package cmd

import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/railwayapp/cli/constants"
"github.com/railwayapp/cli/entity"
)

var OriginalInstallationMethod string

func getExectuablePath() string {
ex, err := os.Executable()
if err != nil {
panic(err)
}
exPath := filepath.Dir(ex)
return exPath
}

func getUpdateCommand(installationMethod string) (*exec.Cmd, error) {
if installationMethod == "brew" {
return exec.Command("brew", "upgrade", "railway"), nil
} else if installationMethod == "curl" {
return exec.Command("curl", "-fsSL", "https://railway.app/install.sh", "|", "sh"), nil
} else if installationMethod == "npm" {
currentPath := getExectuablePath()
if strings.Contains(currentPath, "npm") {
return exec.Command("npm", "i", "-g", "@railway/cli"), nil
} else if strings.Contains(currentPath, "yarn") {
return exec.Command("yarn", "global", "add", "@railway/cli"), nil
}
}
return nil, errors.New("installation methodes is not recognized")
}

func (h *Handler) Upgrade(ctx context.Context, req *entity.CommandRequest) error {
currentVersion, _ := h.ctrl.GetLatestVersion()
if currentVersion == constants.Version {
fmt.Printf("\nYou are currently up to date")
return nil
}

updateCommand, e := getUpdateCommand(OriginalInstallationMethod)
fmt.Println(updateCommand)
if e != nil {
fmt.Printf(e.Error())
return nil
}

if h.ctrl.RunUpdateCommand(updateCommand) == nil {
fmt.Printf("Error when we try to run upload command")
return nil
}
fmt.Printf("upload run sucressfuly")

return nil
}
5 changes: 5 additions & 0 deletions controller/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

import (
"context"
"os/exec"
)

func (c *Controller) GetLatestVersion() (string, error) {
Expand All @@ -11,3 +12,7 @@ func (c *Controller) GetLatestVersion() (string, error) {
}
return *rep.TagName, nil
}
func (c *Controller) RunUpdateCommand(updateCommand *exec.Cmd) error {
err := updateCommand.Run()
return err
}
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func addRootCmd(cmd *cobra.Command) *cobra.Command {
return cmd
}

var OriginalInstallationMethod string

// contextualize converts a HandlerFunction to a cobra function
func contextualize(fn entity.HandlerFunction, panicFn entity.PanicFunction) entity.CobraFunction {
return func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -63,6 +65,7 @@ func contextualize(fn entity.HandlerFunction, panicFn entity.PanicFunction) enti
}

func init() {
cmd.OriginalInstallationMethod = OriginalInstallationMethod
// Initializes all commands
handler := cmd.New()

Expand Down Expand Up @@ -126,6 +129,12 @@ func init() {
Deprecated: "Please use 'railway variables' instead", /**/
})

addRootCmd(&cobra.Command{
Use: "upgrade",
RunE: contextualize(handler.Upgrade, handler.Panic),
Short: "upgrade railway cil version if new one",
})

variablesCmd := addRootCmd(&cobra.Command{
Use: "variables",
Aliases: []string{"vars"},
Expand Down

0 comments on commit 62e8dc5

Please sign in to comment.