|
| 1 | +package installer |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "path/filepath" |
| 10 | + "runtime" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + repo = "d4rkNinja/infynon-cli" |
| 16 | + version = "0.2.0-beta.9.0.7" |
| 17 | +) |
| 18 | + |
| 19 | +func targetTriple() (target, ext string, ok bool) { |
| 20 | + switch runtime.GOOS + "/" + runtime.GOARCH { |
| 21 | + case "windows/amd64": |
| 22 | + return "x86_64-pc-windows-msvc", ".exe", true |
| 23 | + case "linux/amd64": |
| 24 | + return "x86_64-unknown-linux-musl", "", true |
| 25 | + case "linux/arm64": |
| 26 | + return "aarch64-unknown-linux-musl", "", true |
| 27 | + case "darwin/amd64": |
| 28 | + return "x86_64-apple-darwin", "", true |
| 29 | + case "darwin/arm64": |
| 30 | + return "aarch64-apple-darwin", "", true |
| 31 | + default: |
| 32 | + return "", "", false |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func binDir() (string, error) { |
| 37 | + if exe, err := os.Executable(); err == nil { |
| 38 | + if dir := filepath.Dir(exe); dir != "." && dir != "" { |
| 39 | + return dir, nil |
| 40 | + } |
| 41 | + } |
| 42 | + if gobin := os.Getenv("GOBIN"); gobin != "" { |
| 43 | + return gobin, nil |
| 44 | + } |
| 45 | + if gopath := os.Getenv("GOPATH"); gopath != "" { |
| 46 | + return filepath.Join(gopath, "bin"), nil |
| 47 | + } |
| 48 | + home, err := os.UserHomeDir() |
| 49 | + if err != nil { |
| 50 | + return "", err |
| 51 | + } |
| 52 | + return filepath.Join(home, "go", "bin"), nil |
| 53 | +} |
| 54 | + |
| 55 | +func download(url, dest string) error { |
| 56 | + if err := os.MkdirAll(filepath.Dir(dest), 0o755); err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + |
| 60 | + tmp, err := os.CreateTemp(filepath.Dir(dest), ".infynon-download-*") |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + tmpPath := tmp.Name() |
| 65 | + removeTmp := true |
| 66 | + defer func() { |
| 67 | + if removeTmp { |
| 68 | + _ = os.Remove(tmpPath) |
| 69 | + } |
| 70 | + }() |
| 71 | + |
| 72 | + client := http.Client{Timeout: 60 * time.Second} |
| 73 | + req, err := http.NewRequest(http.MethodGet, url, nil) |
| 74 | + if err != nil { |
| 75 | + _ = tmp.Close() |
| 76 | + return err |
| 77 | + } |
| 78 | + req.Header.Set("User-Agent", "infynon-go-installer") |
| 79 | + |
| 80 | + resp, err := client.Do(req) |
| 81 | + if err != nil { |
| 82 | + _ = tmp.Close() |
| 83 | + return err |
| 84 | + } |
| 85 | + defer resp.Body.Close() |
| 86 | + |
| 87 | + if resp.StatusCode != http.StatusOK { |
| 88 | + _ = tmp.Close() |
| 89 | + return fmt.Errorf("download failed: HTTP %d from %s", resp.StatusCode, url) |
| 90 | + } |
| 91 | + |
| 92 | + if _, err := io.Copy(tmp, resp.Body); err != nil { |
| 93 | + _ = tmp.Close() |
| 94 | + return err |
| 95 | + } |
| 96 | + if err := tmp.Close(); err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + if runtime.GOOS != "windows" { |
| 100 | + if err := os.Chmod(tmpPath, 0o755); err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + } |
| 104 | + if err := os.Rename(tmpPath, dest); err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + removeTmp = false |
| 108 | + return nil |
| 109 | +} |
| 110 | + |
| 111 | +func runBinary(binPath string, args []string) { |
| 112 | + cmd := exec.Command(binPath, args...) |
| 113 | + cmd.Stdin = os.Stdin |
| 114 | + cmd.Stdout = os.Stdout |
| 115 | + cmd.Stderr = os.Stderr |
| 116 | + if err := cmd.Run(); err != nil { |
| 117 | + if exitErr, ok := err.(*exec.ExitError); ok { |
| 118 | + os.Exit(exitErr.ExitCode()) |
| 119 | + } |
| 120 | + fmt.Fprintf(os.Stderr, "[infynon] Failed to run binary: %v\n", err) |
| 121 | + os.Exit(1) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func Main() { |
| 126 | + target, ext, ok := targetTriple() |
| 127 | + if !ok { |
| 128 | + fmt.Fprintf(os.Stderr, "[infynon] Unsupported platform: %s/%s\n", runtime.GOOS, runtime.GOARCH) |
| 129 | + fmt.Fprintf(os.Stderr, " Build from source: cargo install --git https://github.com/%s\n", repo) |
| 130 | + os.Exit(1) |
| 131 | + } |
| 132 | + |
| 133 | + dir, err := binDir() |
| 134 | + if err != nil { |
| 135 | + fmt.Fprintf(os.Stderr, "[infynon] Could not resolve install directory: %v\n", err) |
| 136 | + os.Exit(1) |
| 137 | + } |
| 138 | + binPath := filepath.Join(dir, "infynon"+ext) |
| 139 | + |
| 140 | + if _, err := os.Stat(binPath); err == nil { |
| 141 | + runBinary(binPath, os.Args[1:]) |
| 142 | + return |
| 143 | + } |
| 144 | + |
| 145 | + tag := "v" + version |
| 146 | + asset := "infynon-" + target + ext |
| 147 | + url := fmt.Sprintf("https://github.com/%s/releases/download/%s/%s", repo, tag, asset) |
| 148 | + |
| 149 | + fmt.Printf("[infynon] Downloading %s from %s release...\n", asset, tag) |
| 150 | + if err := download(url, binPath); err != nil { |
| 151 | + fmt.Fprintf(os.Stderr, "[infynon] Download failed: %v\n", err) |
| 152 | + fmt.Fprintf(os.Stderr, "[infynon] Manual install: https://github.com/%s/releases/tag/%s\n", repo, tag) |
| 153 | + os.Exit(1) |
| 154 | + } |
| 155 | + |
| 156 | + fmt.Printf("[infynon] Installed to %s\n", binPath) |
| 157 | + if len(os.Args) > 1 { |
| 158 | + runBinary(binPath, os.Args[1:]) |
| 159 | + return |
| 160 | + } |
| 161 | + fmt.Println("[infynon] Run: infynon --help") |
| 162 | +} |
0 commit comments