|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/tar" |
| 5 | + "compress/gzip" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "log" |
| 10 | + "net/http" |
| 11 | + "os" |
| 12 | + "os/exec" |
| 13 | + "path/filepath" |
| 14 | + "runtime" |
| 15 | + "strings" |
| 16 | + |
| 17 | + "github.com/apparentlymart/go-userdirs/userdirs" |
| 18 | + "github.com/gofrs/flock" |
| 19 | +) |
| 20 | + |
| 21 | +// This is a simple wrapper program which downloads, caches, and runs the |
| 22 | +// appropriate `componentize-go` binary for the current platform. |
| 23 | +// |
| 24 | +// Although `componentize-go` is written in Rust, we can use this wrapper to |
| 25 | +// make it available using e.g. `go install` and/or `go tool`. |
| 26 | +func main() { |
| 27 | + // This is hard-coded to point to the latest canary release, which is |
| 28 | + // appropriate for the `main` branch, but should be changed to the tag |
| 29 | + // name for each tagged release. |
| 30 | + // |
| 31 | + // TODO: Can we automate updating this for each release? |
| 32 | + release := "canary" |
| 33 | + |
| 34 | + directories := userdirs.ForApp( |
| 35 | + "componentize-go", |
| 36 | + "bytecodealliance", |
| 37 | + "com.github.bytecodealliance-componentize-go", |
| 38 | + ) |
| 39 | + binDirectory := filepath.Join(directories.CacheDir, "bin") |
| 40 | + versionPath := filepath.Join(directories.CacheDir, "version.txt") |
| 41 | + lockFilePath := filepath.Join(directories.CacheDir, "lock") |
| 42 | + binaryPath := filepath.Join(binDirectory, "componentize-go") |
| 43 | + |
| 44 | + maybeDownload(release, binDirectory, versionPath, lockFilePath, binaryPath) |
| 45 | + |
| 46 | + run(binaryPath) |
| 47 | +} |
| 48 | + |
| 49 | +// Download the specified version of `componentize-go` if we haven't already. |
| 50 | +func maybeDownload(release, binDirectory, versionPath, lockFilePath, binaryPath string) { |
| 51 | + if err := os.MkdirAll(binDirectory, 0755); err != nil { |
| 52 | + log.Fatalf("unable to create directory `%v`: %v", binDirectory, err) |
| 53 | + } |
| 54 | + |
| 55 | + // Lock the lock file to prevent concurrent downloads. |
| 56 | + lockFile := flock.New(lockFilePath) |
| 57 | + if err := lockFile.Lock(); err != nil { |
| 58 | + log.Fatalf("unable to lock file `%v`: %v", lockFilePath, err) |
| 59 | + } |
| 60 | + defer lockFile.Unlock() |
| 61 | + |
| 62 | + versionBytes, err := os.ReadFile(versionPath) |
| 63 | + var version string |
| 64 | + if err != nil { |
| 65 | + version = "<unknown>" |
| 66 | + } else { |
| 67 | + version = strings.TrimSpace(string(versionBytes)) |
| 68 | + } |
| 69 | + |
| 70 | + // If the binary doesn't already exist and/or the version doesn't match |
| 71 | + // the desired release, download it. |
| 72 | + if _, err := os.Stat(binaryPath); errors.Is(err, os.ErrNotExist) || version != release { |
| 73 | + base := fmt.Sprintf( |
| 74 | + "https://github.com/bytecodealliance/componentize-go/releases/download/%v", |
| 75 | + release, |
| 76 | + ) |
| 77 | + |
| 78 | + url := fmt.Sprintf("%v/componentize-go-%v-%v.tar.gz", base, runtime.GOOS, runtime.GOARCH) |
| 79 | + |
| 80 | + fmt.Printf("Downloading `componentize-go` binary from %v and extracting to %v\n", url, binDirectory) |
| 81 | + |
| 82 | + response, err := http.Get(url) |
| 83 | + if err != nil { |
| 84 | + log.Fatalf("unable to download URL `%v`: %v", url, err) |
| 85 | + } |
| 86 | + defer response.Body.Close() |
| 87 | + |
| 88 | + if response.StatusCode < 200 || response.StatusCode > 299 { |
| 89 | + log.Fatalf("unexpected status for URL `%v`: %v", url, response.StatusCode) |
| 90 | + } |
| 91 | + |
| 92 | + uncompressed, err := gzip.NewReader(response.Body) |
| 93 | + if err != nil { |
| 94 | + log.Fatalf("unable to decompress content of URL `%v`: %v", url, err) |
| 95 | + } |
| 96 | + defer uncompressed.Close() |
| 97 | + |
| 98 | + untarred := tar.NewReader(uncompressed) |
| 99 | + for { |
| 100 | + header, err := untarred.Next() |
| 101 | + if err == io.EOF { |
| 102 | + break |
| 103 | + } else if err != nil { |
| 104 | + log.Fatalf("unable to untar content of URL `%v`: %v", url, err) |
| 105 | + } |
| 106 | + path := filepath.Join(binDirectory, header.Name) |
| 107 | + file, err := os.Create(path) |
| 108 | + if err != nil { |
| 109 | + log.Fatalf("unable to create file `%v`: %v", path, err) |
| 110 | + } |
| 111 | + if _, err := io.Copy(file, untarred); err != nil { |
| 112 | + log.Fatalf("unable to untar content of URL `%v`: %v", url, err) |
| 113 | + } |
| 114 | + file.Close() |
| 115 | + } |
| 116 | + |
| 117 | + if err := os.Chmod(binaryPath, 0755); err != nil { |
| 118 | + log.Fatalf("unable to make file `%v` executable: %v", binaryPath, err) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + // If we just downloaded a new version, remember which one so we don't |
| 123 | + // download it redundantly next time. |
| 124 | + if version != release { |
| 125 | + if err := os.WriteFile(versionPath, []byte(release), 0600); err != nil { |
| 126 | + log.Fatalf("unable to write version to `%v`: %v", versionPath, err) |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +// Run the specified binary, forwarding all our arguments to it and piping its |
| 132 | +// stdout and stderr back to the user. |
| 133 | +func run(binaryPath string) { |
| 134 | + command := exec.Command(binaryPath, os.Args[1:]...) |
| 135 | + command.Stdout = os.Stdout |
| 136 | + command.Stderr = os.Stderr |
| 137 | + |
| 138 | + if err := command.Start(); err != nil { |
| 139 | + log.Fatalf("unable to start `%v` command: %v", binaryPath, err) |
| 140 | + } |
| 141 | + |
| 142 | + if err := command.Wait(); err != nil { |
| 143 | + if exiterr, ok := err.(*exec.ExitError); ok { |
| 144 | + code := exiterr.ExitCode() |
| 145 | + if code != 0 { |
| 146 | + os.Exit(code) |
| 147 | + } |
| 148 | + } else { |
| 149 | + log.Fatalf("trouble running `%v` command: %v", binaryPath, err) |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments