Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove wget dependency #69

Merged
merged 4 commits into from
Sep 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 47 additions & 9 deletions playground/gaia/get_gaiad.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,58 @@ package gaia

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

"github.com/hanchon/hanchond/playground/filesmanager"
)

func GetGaiadBinary(isDarwin bool, version string) error {
darwinURL := "https://github.com/cosmos/gaia/releases/download/" + version + "/gaiad-" + version + "-darwin-arm64"
url := " https://github.com/cosmos/gaia/releases/download/" + version + "/gaiad-" + version + "-linux-amd64"
if isDarwin {
url = darwinURL
arch := runtime.GOARCH
if arch != "arm64" {
arch = "amd64"
}
systemOS := "darwin"
if !isDarwin {
systemOS = "linux"
}

url := fmt.Sprintf("https://github.com/cosmos/gaia/releases/download/%s/gaiad-%s-%s-%s", version, version, systemOS, arch)

path := filesmanager.GetGaiadPath()
cmdString := fmt.Sprintf("wget %s -O %s && chmod +x %s", url, path, path)
command := exec.Command("bash", "-c", cmdString)
_, err := command.CombinedOutput()
return err

req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("failed to download Gaia: %s", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to download gaiad binary: status code %d", resp.StatusCode)
}

outFile, err := os.Create(path)
if err != nil {
return fmt.Errorf("failed to create file: %s", err)
}
defer outFile.Close()

_, err = io.Copy(outFile, resp.Body)
if err != nil {
return fmt.Errorf("failed to save gaiad binary: %s", err)
}

err = os.Chmod(path, 0o755)
if err != nil {
return fmt.Errorf("failed to set file permissions: %s", err)
}

return nil
}
Loading