Skip to content

Commit

Permalink
update command merged into version (#217)
Browse files Browse the repository at this point in the history
Signed-off-by: Kartikay <[email protected]>
  • Loading branch information
kartikaysaxena authored Apr 16, 2024
1 parent 47ba363 commit 2ff8c1f
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 101 deletions.
2 changes: 0 additions & 2 deletions pkg/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

"github.com/litmuschaos/litmusctl/pkg/cmd/run"
"github.com/litmuschaos/litmusctl/pkg/cmd/save"
"github.com/litmuschaos/litmusctl/pkg/cmd/update"

"github.com/litmuschaos/litmusctl/pkg/cmd/connect"
"github.com/litmuschaos/litmusctl/pkg/cmd/delete"
Expand Down Expand Up @@ -75,7 +74,6 @@ func init() {
rootCmd.AddCommand(upgrade.UpgradeCmd)
rootCmd.AddCommand(save.SaveCmd)
rootCmd.AddCommand(run.RunCmd)
rootCmd.AddCommand(update.UpdateCmd)

// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
Expand Down
98 changes: 0 additions & 98 deletions pkg/cmd/update/update.go

This file was deleted.

95 changes: 94 additions & 1 deletion pkg/cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ limitations under the License.
package version

import (
"io"
"net/http"
"os"
"os/exec"
"runtime"

"github.com/litmuschaos/litmusctl/pkg/utils"

homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
)

Expand All @@ -44,3 +48,92 @@ var VersionCmd = &cobra.Command{
utils.White_B.Print("]\n")
},
}

var UpdateCmd = &cobra.Command{
Use: "update",
Short: "Changes the version of litmusctl",
Args: cobra.ExactArgs(1),
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
updateVersion := args[0]
homeDir, err := homedir.Dir()
if err != nil {
utils.PrintError(err)
}

var assetURL string = "https://litmusctl-production-bucket.s3.amazonaws.com/"
var binaryName string = "litmusctl"
switch runtime.GOOS {
case "windows":
if runtime.GOARCH == "386" {
binaryName += "-windows-386-" + updateVersion + ".tar.gz"
assetURL += binaryName
} else if runtime.GOARCH == "amd64" {
binaryName += "-windows-amd64-" + updateVersion + ".tar.gz"
assetURL += binaryName
} else {
binaryName += "-windows-arm64-" + updateVersion + ".tar.gz"
assetURL += binaryName
}
case "linux":
if runtime.GOARCH == "arm64" {
binaryName += "-linux-arm64-" + updateVersion + ".tar.gz"
assetURL += binaryName
} else if runtime.GOARCH == "amd64" {
binaryName += "-linux-amd64-" + updateVersion + ".tar.gz"
assetURL += binaryName
} else if runtime.GOARCH == "arm" {
binaryName += "-linux-arm-" + updateVersion + ".tar.gz"
assetURL += binaryName
} else {
binaryName += "-linux-386-" + updateVersion + ".tar.gz"
assetURL += binaryName
}
case "darwin":
if runtime.GOARCH == "amd64" {
binaryName += "-darwin-amd64-" + updateVersion + ".tar.gz"
assetURL += binaryName
}
}

utils.White.Print("Downloading:\n")

resp2, err := http.Get(assetURL)
if err != nil {
utils.PrintError(err)
}

tempFile, err := os.CreateTemp("", binaryName)
if err != nil {
utils.PrintError(err)
return
}
defer os.Remove(tempFile.Name())

_, err = io.Copy(tempFile, resp2.Body)
if err != nil {
utils.PrintError(err)
return
}
utils.White_B.Print("OK\n")

tempFile.Close()

tarCmd := exec.Command("tar", "xzf", tempFile.Name(), "-C", homeDir)
tarCmd.Stdout = os.Stdout
tarCmd.Stderr = os.Stderr

utils.White_B.Print("Extracting binary...\n")
err = tarCmd.Run()
if err != nil {
utils.PrintError(err)
return
}

utils.White_B.Print("Binary extracted successfully\n")
},
}

func init() {
VersionCmd.AddCommand(UpdateCmd)
}

0 comments on commit 2ff8c1f

Please sign in to comment.