Skip to content

Commit

Permalink
modularize code
Browse files Browse the repository at this point in the history
Signed-off-by: Yash Khare <khareyash05@gmail.com>
  • Loading branch information
khareyash05 committed Nov 11, 2024
1 parent d736680 commit 0cfaf6c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 43 deletions.
1 change: 0 additions & 1 deletion src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check failure on line 52 in src/cmd/root.go

View workflow job for this annotation

GitHub Actions / Build && tests for linux amd64

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)

Check failure on line 52 in src/cmd/root.go

View workflow job for this annotation

GitHub Actions / Build && tests for linux 386

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)

Check failure on line 52 in src/cmd/root.go

View workflow job for this annotation

GitHub Actions / Build && tests for darwin amd64

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
}
Expand Down
90 changes: 48 additions & 42 deletions src/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,49 +39,11 @@ func updateCmd() *cmdBuilder.Cmd {
fmt.Scanln(&input)

Check failure on line 39 in src/cmd/update.go

View workflow job for this annotation

GitHub Actions / Build && tests for linux amd64

Error return value of `fmt.Scanln` is not checked (errcheck)

Check failure on line 39 in src/cmd/update.go

View workflow job for this annotation

GitHub Actions / Build && tests for linux 386

Error return value of `fmt.Scanln` is not checked (errcheck)

Check failure on line 39 in src/cmd/update.go

View workflow job for this annotation

GitHub Actions / Build && tests for darwin amd64

Error return value of `fmt.Scanln` is not checked (errcheck)

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.")
}
Expand Down Expand Up @@ -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)

Check failure on line 118 in src/cmd/update.go

View workflow job for this annotation

GitHub Actions / Build && tests for linux amd64

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)

Check failure on line 118 in src/cmd/update.go

View workflow job for this annotation

GitHub Actions / Build && tests for linux 386

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)

Check failure on line 118 in src/cmd/update.go

View workflow job for this annotation

GitHub Actions / Build && tests for darwin amd64

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
}

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)

Check failure on line 126 in src/cmd/update.go

View workflow job for this annotation

GitHub Actions / Build && tests for linux amd64

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)

Check failure on line 126 in src/cmd/update.go

View workflow job for this annotation

GitHub Actions / Build && tests for linux 386

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)

Check failure on line 126 in src/cmd/update.go

View workflow job for this annotation

GitHub Actions / Build && tests for darwin amd64

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
}
}

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)

Check failure on line 135 in src/cmd/update.go

View workflow job for this annotation

GitHub Actions / Build && tests for linux amd64

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)

Check failure on line 135 in src/cmd/update.go

View workflow job for this annotation

GitHub Actions / Build && tests for linux 386

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)

Check failure on line 135 in src/cmd/update.go

View workflow job for this annotation

GitHub Actions / Build && tests for darwin amd64

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
}

if err := os.Chmod(binPath, 0755); err != nil {
return fmt.Errorf("failed to make zcli executable: %v", err)

Check failure on line 139 in src/cmd/update.go

View workflow job for this annotation

GitHub Actions / Build && tests for linux amd64

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)

Check failure on line 139 in src/cmd/update.go

View workflow job for this annotation

GitHub Actions / Build && tests for linux 386

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)

Check failure on line 139 in src/cmd/update.go

View workflow job for this annotation

GitHub Actions / Build && tests for darwin amd64

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
}

fmt.Printf("zCLI was installed successfully to %s\n", binPath)
return nil
}

0 comments on commit 0cfaf6c

Please sign in to comment.