|
| 1 | +package artifacts |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/tar" |
| 5 | + "compress/gzip" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "os/exec" |
| 11 | + "path/filepath" |
| 12 | + "runtime" |
| 13 | +) |
| 14 | + |
| 15 | +type release struct { |
| 16 | + Name string |
| 17 | + Org string |
| 18 | + Version string |
| 19 | + Arch func(string, string) string |
| 20 | +} |
| 21 | + |
| 22 | +func DownloadArtifacts() (map[string]string, error) { |
| 23 | + var artifacts = []release{ |
| 24 | + { |
| 25 | + Name: "reth", |
| 26 | + Org: "paradigmxyz", |
| 27 | + Version: "v1.0.2", |
| 28 | + Arch: func(goos, goarch string) string { |
| 29 | + if goos == "linux" { |
| 30 | + return "x86_64-unknown-linux-gnu" |
| 31 | + } else if goos == "darwin" && goarch == "arm64" { // Apple M1 |
| 32 | + return "aarch64-apple-darwin" |
| 33 | + } else if goos == "darwin" && goarch == "amd64" { |
| 34 | + return "x86_64-apple-darwin" |
| 35 | + } |
| 36 | + return "" |
| 37 | + }, |
| 38 | + }, |
| 39 | + { |
| 40 | + Name: "lighthouse", |
| 41 | + Org: "sigp", |
| 42 | + Version: "v5.2.1", |
| 43 | + Arch: func(goos, goarch string) string { |
| 44 | + if goos == "linux" { |
| 45 | + return "x86_64-unknown-linux-gnu" |
| 46 | + } else if goos == "darwin" && goarch == "arm64" { // Apple M1 |
| 47 | + return "x86_64-apple-darwin-portable" |
| 48 | + } else if goos == "darwin" && goarch == "amd64" { |
| 49 | + return "x86_64-apple-darwin" |
| 50 | + } |
| 51 | + return "" |
| 52 | + }, |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + homeDir, err := os.UserHomeDir() |
| 57 | + if err != nil { |
| 58 | + return nil, fmt.Errorf("error getting user home directory: %w", err) |
| 59 | + } |
| 60 | + |
| 61 | + // Define the path for our custom home directory |
| 62 | + customHomeDir := filepath.Join(homeDir, ".playground") |
| 63 | + |
| 64 | + // Create output directory if it doesn't exist |
| 65 | + if err := os.MkdirAll(customHomeDir, 0755); err != nil { |
| 66 | + return nil, fmt.Errorf("error creating output directory: %v", err) |
| 67 | + } |
| 68 | + |
| 69 | + goos := runtime.GOOS |
| 70 | + goarch := runtime.GOARCH |
| 71 | + |
| 72 | + fmt.Printf("Architecture detected: %s/%s\n", goos, goarch) |
| 73 | + |
| 74 | + // Try to download the release binaries for 'reth' and 'lighthouse'. It works as follows: |
| 75 | + // 1. Check under $HOME/.playground if the binary-<version> exists. If exists, use it. |
| 76 | + // 2. If the binary does not exists, use the arch and os to download the binary from the release page. |
| 77 | + // 3. If the architecture is not supported, check if the binary is found in PATH. |
| 78 | + releases := make(map[string]string) |
| 79 | + for _, artifact := range artifacts { |
| 80 | + outPath := filepath.Join(customHomeDir, artifact.Name+"-"+artifact.Version) |
| 81 | + _, err := os.Stat(outPath) |
| 82 | + if err != nil && !os.IsNotExist(err) { |
| 83 | + return nil, fmt.Errorf("error checking file existence: %v", err) |
| 84 | + } |
| 85 | + |
| 86 | + if err != nil { |
| 87 | + archVersion := artifact.Arch(goos, goarch) |
| 88 | + if archVersion == "" { |
| 89 | + // Case 2. The architecture is not supported. |
| 90 | + fmt.Printf("unsupported OS/Arch: %s/%s\n", goos, goarch) |
| 91 | + if _, err := exec.LookPath(artifact.Name); err != nil { |
| 92 | + return nil, fmt.Errorf("error looking up binary in PATH: %v", err) |
| 93 | + } else { |
| 94 | + outPath = artifact.Name |
| 95 | + fmt.Printf("Using %s from PATH\n", artifact.Name) |
| 96 | + } |
| 97 | + } else { |
| 98 | + // Case 3. Download the binary from the release page |
| 99 | + releasesURL := fmt.Sprintf("https://github.com/%s/%s/releases/download/%s/%s-%s-%s.tar.gz", artifact.Org, artifact.Name, artifact.Version, artifact.Name, artifact.Version, archVersion) |
| 100 | + fmt.Printf("Downloading %s: %s\n", outPath, releasesURL) |
| 101 | + |
| 102 | + if err := downloadArtifact(releasesURL, artifact.Name, outPath); err != nil { |
| 103 | + return nil, fmt.Errorf("error downloading artifact: %v", err) |
| 104 | + } |
| 105 | + } |
| 106 | + } else { |
| 107 | + // Case 1. Use the binary in $HOME/.playground |
| 108 | + fmt.Printf("%s already exists, skipping download\n", outPath) |
| 109 | + } |
| 110 | + |
| 111 | + releases[artifact.Name] = outPath |
| 112 | + } |
| 113 | + |
| 114 | + return releases, nil |
| 115 | +} |
| 116 | + |
| 117 | +func downloadArtifact(url string, expectedFile string, outPath string) error { |
| 118 | + // Download the file |
| 119 | + resp, err := http.Get(url) |
| 120 | + if err != nil { |
| 121 | + return fmt.Errorf("error downloading file: %v", err) |
| 122 | + } |
| 123 | + defer resp.Body.Close() |
| 124 | + |
| 125 | + // Create a gzip reader |
| 126 | + gzipReader, err := gzip.NewReader(resp.Body) |
| 127 | + if err != nil { |
| 128 | + return fmt.Errorf("error creating gzip reader: %v", err) |
| 129 | + } |
| 130 | + defer gzipReader.Close() |
| 131 | + |
| 132 | + // Create a tar reader |
| 133 | + tarReader := tar.NewReader(gzipReader) |
| 134 | + |
| 135 | + // Extract the file |
| 136 | + var found bool |
| 137 | + for { |
| 138 | + header, err := tarReader.Next() |
| 139 | + if err == io.EOF { |
| 140 | + break |
| 141 | + } |
| 142 | + if err != nil { |
| 143 | + return fmt.Errorf("error reading tar: %v", err) |
| 144 | + } |
| 145 | + |
| 146 | + if header.Typeflag == tar.TypeReg { |
| 147 | + if header.Name != expectedFile { |
| 148 | + return fmt.Errorf("unexpected file in archive: %s", header.Name) |
| 149 | + } |
| 150 | + outFile, err := os.Create(outPath) |
| 151 | + if err != nil { |
| 152 | + return fmt.Errorf("error creating output file: %v", err) |
| 153 | + } |
| 154 | + defer outFile.Close() |
| 155 | + |
| 156 | + if _, err := io.Copy(outFile, tarReader); err != nil { |
| 157 | + return fmt.Errorf("error writing output file: %v", err) |
| 158 | + } |
| 159 | + |
| 160 | + // change permissions |
| 161 | + if err := os.Chmod(outPath, 0755); err != nil { |
| 162 | + return fmt.Errorf("error changing permissions: %v", err) |
| 163 | + } |
| 164 | + found = true |
| 165 | + break // Assuming there's only one file per repo |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + if !found { |
| 170 | + return fmt.Errorf("file not found in archive: %s", expectedFile) |
| 171 | + } |
| 172 | + return nil |
| 173 | +} |
0 commit comments