Skip to content

Commit

Permalink
done with update from ksctl cli
Browse files Browse the repository at this point in the history
Signed-off-by: Dipankar Das <65275144+dipankardas011@users.noreply.github.com>
  • Loading branch information
dipankardas011 committed Aug 2, 2024
1 parent 10ba5fd commit 697bf3e
Showing 1 changed file with 69 additions and 11 deletions.
80 changes: 69 additions & 11 deletions cli/cmd/upgrade.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package cmd

import (
"archive/tar"
"compress/gzip"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
Expand All @@ -20,7 +23,7 @@ import (

func fetchLatestVersion() ([]string, error) {

log.Print(ctx, "Fetching available versions")
logCli.Print(ctx, "Fetching available versions")

type Release struct {
TagName string `json:"tag_name"`
Expand Down Expand Up @@ -149,6 +152,7 @@ func update(version string) error {
checksumUri := fmt.Sprintf("%s/%s", downloadURLBase, checksumFile)

defer func() {
logCli.Print(ctx, "Cleaning up")
if err := os.Remove(checksumFile); err != nil {
logCli.Error("Failed to remove checksum file", "error", err)
}
Expand All @@ -168,15 +172,68 @@ func update(version string) error {

match, err := verifyChecksum(tarFile, checksumFile)
if err != nil {
logCli.Error("Failed to verify checksum", "error", err)
os.Exit(1)
return logCli.NewError(ctx, "Failed to verify checksum", "error", err)
}
if !match {
logCli.Error("Checksum verification failed")
os.Exit(1)
return logCli.NewError(ctx, "Checksum verification failed")
}
logCli.Success(ctx, "Checksum verification successful")

tempDir, err := os.MkdirTemp("", "ksctl-update")
if err != nil {
return logCli.NewError(ctx, "Failed to create temp dir", "error", err)
}
file, err := os.Open(tarFile)
if err != nil {
return logCli.NewError(ctx, "Failed to open tar file", "error", err)
}
defer file.Close()

gzr, err := gzip.NewReader(file)
if err != nil {
return logCli.NewError(ctx, "Failed to read gzip file", "error", err)
}
defer gzr.Close()
tr := tar.NewReader(gzr)
for {
header, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return logCli.NewError(ctx, "Failed to read tar file", "error", err)
}
if header.Name == "ksctl" {
outFile, err := os.Create(filepath.Join(tempDir, "ksctl"))
if err != nil {
return logCli.NewError(ctx, "Failed to create ksctl binary", "error", err)
}
defer outFile.Close()

if _, err := io.Copy(outFile, tr); err != nil {
return logCli.NewError(ctx, "Failed to copy ksctl binary", "error", err)
}
break
}
}

logCli.Print(ctx, "Making ksctl executable...")
if err := os.Chmod(filepath.Join(tempDir, "ksctl"), 0550); err != nil {
return logCli.NewError(ctx, "Failed to make ksctl executable", "error", err)
}

logCli.Print(ctx, "Moving ksctl to /usr/local/bin (requires sudo)...")
cmd := exec.Command("sudo", "mv", "-v", filepath.Join(tempDir, "ksctl"), "/usr/local/bin/ksctl")
err = cmd.Run()
if err != nil {
return logCli.NewError(ctx, "Failed to move ksctl to /usr/local/bin", "error", err)
}

_, err = exec.LookPath("ksctl")
if err != nil {
return logCli.NewError(ctx, "Failed to find ksctl in PATH", "error", err)
}

return nil
}

Expand All @@ -186,12 +243,12 @@ var selfUpdate = &cobra.Command{
Long: "setups up update for ksctl cli",
Run: func(cmd *cobra.Command, args []string) {

// if Version == "dev" {
// log.Error("Cannot update a dev version of ksctl")
// os.Exit(1)
// }
if Version == "dev" {
logCli.Error("Cannot update dev version", "msg", "Please use a stable version to update")
os.Exit(1)
}

logCli.Warn(ctx, "Currently no migrations are supported", "Message", "Please help us by creating a PR to support migrations. Thank you!")
logCli.Warn(ctx, "Currently no migrations are supported", "msg", "Please help us by creating a PR to support migrations. Thank you!")

vers, err := fetchLatestVersion()
if err != nil {
Expand All @@ -211,6 +268,7 @@ var selfUpdate = &cobra.Command{
}

logCli.Success(ctx, "Updated Ksctl cli", "previousVer", Version, "newVer", newVer)
logCli.Note(ctx, "Please restart your terminal to use the updated version")
},
}

Expand Down

0 comments on commit 697bf3e

Please sign in to comment.