From 0cfaf6cfbe560611fc8aba67c0739a87055cd911 Mon Sep 17 00:00:00 2001 From: Yash Khare Date: Mon, 11 Nov 2024 23:43:29 +0530 Subject: [PATCH] modularize code Signed-off-by: Yash Khare --- src/cmd/root.go | 1 - src/cmd/update.go | 90 +++++++++++++++++++++++++---------------------- 2 files changed, 48 insertions(+), 43 deletions(-) diff --git a/src/cmd/root.go b/src/cmd/root.go index 0d1a97d..6fc92af 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -48,7 +48,6 @@ func rootCmd() *cmdBuilder.Cmd { cmd := exec.CommandContext(ctx, zcliPath, "update") cmd.Stdout = cmdData.Stdout.GetWriter() cmd.Stdin = os.Stdin - fmt.Println(cmd.Stdout) if err := cmd.Run(); err != nil { return fmt.Errorf("failed to execute 'zcli update': %v", err) } diff --git a/src/cmd/update.go b/src/cmd/update.go index 41f800f..68654bb 100644 --- a/src/cmd/update.go +++ b/src/cmd/update.go @@ -39,49 +39,11 @@ func updateCmd() *cmdBuilder.Cmd { fmt.Scanln(&input) if input == "y" { - fmt.Println("Updating zcli...") - - // Set the target based on system architecture - var target string - switch runtime.GOOS + " " + runtime.GOARCH { - case "darwin amd64": - target = "darwin-amd64" - case "darwin arm64": - target = "darwin-arm64" - case "linux 386": - target = "linux-i386" - default: - target = "linux-amd64" - } - - // Determine the URI for the download based on the target - var zcliURI = fmt.Sprintf("https://github.com/zeropsio/zcli/releases/latest/download/zcli-%s", target) - - // Define installation path - binDir := os.ExpandEnv("$HOME/.local/bin") - binPath := fmt.Sprintf("%s/zcli", binDir) - - // Create binDir if it doesn't exist - if _, err := os.Stat(binDir); os.IsNotExist(err) { - if err := os.MkdirAll(binDir, 0755); err != nil { - return fmt.Errorf("failed to create directory %s: %v", binDir, err) - } + target := determineTargetArchitecture() + if err := downloadAndInstallZCLI(ctx, target); err != nil { + return err } - - // Download zcli binary - curlCmd := fmt.Sprintf("curl --fail --location --progress-bar --output %s %s", binPath, zcliURI) - cmd := exec.Command("sh", "-c", curlCmd) - - if err := cmd.Run(); err != nil { - return fmt.Errorf("failed to download zcli: %v", err) - } - - // Make binary executable - if err := os.Chmod(binPath, 0755); err != nil { - return fmt.Errorf("failed to make zcli executable: %v", err) - } - - fmt.Println("zCLI was installed successfully to", binPath) + fmt.Println("zCLI was updated successfully to",latestVersion.TagName) } else { fmt.Println("Update canceled.") } @@ -136,3 +98,47 @@ func getLatestGitHubRelease(ctx context.Context) (GitHubRelease, error) { } return release, nil } + +func determineTargetArchitecture() string { + switch runtime.GOOS + " " + runtime.GOARCH { + case "darwin amd64": + return "darwin-amd64" + case "darwin arm64": + return "darwin-arm64" + case "linux 386": + return "linux-i386" + default: + return "linux-amd64" + } +} + +func downloadAndInstallZCLI(ctx context.Context, target string) error { + homeDir, err := os.UserHomeDir() + if err != nil { + return fmt.Errorf("failed to get home directory: %v", err) + } + + binDir := fmt.Sprintf("%s/.local/bin", homeDir) + binPath := fmt.Sprintf("%s/zcli", binDir) + + if _, err := os.Stat(binDir); os.IsNotExist(err) { + if err := os.MkdirAll(binDir, 0755); err != nil { + return fmt.Errorf("failed to create directory %s: %v", binDir, err) + } + } + + zcliURI := fmt.Sprintf("https://github.com/zeropsio/zcli/releases/latest/download/zcli-%s", target) + curlCmd := fmt.Sprintf("curl --fail --location --progress-bar --output %s %s", binPath, zcliURI) + cmd := exec.CommandContext(ctx, "sh", "-c", curlCmd) + + if err := cmd.Run(); err != nil { + return fmt.Errorf("failed to download zcli: %v", err) + } + + if err := os.Chmod(binPath, 0755); err != nil { + return fmt.Errorf("failed to make zcli executable: %v", err) + } + + fmt.Printf("zCLI was installed successfully to %s\n", binPath) + return nil +}