Skip to content

Commit

Permalink
added initial work on download of artifacts
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 52f1d04 commit 37d2707
Showing 1 changed file with 71 additions and 7 deletions.
78 changes: 71 additions & 7 deletions cli/cmd/upgrade.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package cmd

import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"regexp"
"runtime"

"github.com/ksctl/cli/logger"
"github.com/ksctl/ksctl/pkg/types"
"github.com/pterm/pterm"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -52,7 +55,66 @@ func filterToUpgradeableVersions(versions []string) []string {
return upgradeableVersions
}

func downloadFile(url, localFilename string) error {
fmt.Printf("Downloading %s to %s\n", url, localFilename)
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()

out, err := os.Create(localFilename)
if err != nil {
return err
}
defer out.Close()

_, err = io.Copy(out, resp.Body)
return err
}
func verifyChecksum(filePath, expectedChecksum string) (bool, error) {
file, err := os.Open(filePath)
if err != nil {
return false, err
}
defer file.Close()

hash := sha256.New()
if _, err := io.Copy(hash, file); err != nil {
return false, err
}

calculatedChecksum := hex.EncodeToString(hash.Sum(nil))
return calculatedChecksum == expectedChecksum, nil
}

func getOsArch() (string, error) {
arch := runtime.GOARCH

if arch != "amd64" && arch != "arm64" {
return "", logCli.NewError(ctx, "Unsupported architecture")
}
return arch, nil
}

func getOs() (string, error) {
os := runtime.GOOS

if os != "linux" && os != "darwin" {
return "", logCli.NewError(ctx, "Unsupported OS", "message", "will provide support for windows based OS soon")
}
return os, nil
}

func update(version string) error {
os, err := getOs()
if err != nil {
return err
}
arch, err := getOsArch()
if err != nil {
return err
}
return nil
}

Expand All @@ -61,30 +123,32 @@ var selfUpdate = &cobra.Command{
Short: "update the ksctl cli",
Long: "setups up update for ksctl cli",
Run: func(cmd *cobra.Command, args []string) {
verbosity, _ := cmd.Flags().GetInt("verbose")
var log types.LoggerFactory = logger.NewLogger(verbosity, os.Stdout)

// if Version == "dev" {
// log.Error("Cannot update a dev version of ksctl")
// os.Exit(1)
// }

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

vers, err := fetchLatestVersion()
if err != nil {
log.Error("Failed to fetch latest version", "error", err)
logCli.Error("Failed to fetch latest version", "error", err)
os.Exit(1)
}
vers = filterToUpgradeableVersions(vers)

log.Print(ctx, "Available versions to update")
logCli.Print(ctx, "Available versions to update")
selectedOption, _ := pterm.DefaultInteractiveSelect.WithOptions(vers).Show()

newVer := selectedOption

if err := update(newVer); err != nil {
logCli.Error("Failed to update ksctl cli", "error", err)
os.Exit(1)
}

log.Success(ctx, "Updated Ksctl cli", "previousVer", Version, "newVer", newVer)
logCli.Success(ctx, "Updated Ksctl cli", "previousVer", Version, "newVer", newVer)
},
}

Expand Down

0 comments on commit 37d2707

Please sign in to comment.