| | | 1 | | package neovim |
| | | 2 | | |
| | | 3 | | import ( |
| | | 4 | | "context" |
| | | 5 | | "fmt" |
| | | 6 | | "io" |
| | | 7 | | "net/http" |
| | | 8 | | "os" |
| | | 9 | | "path/filepath" |
| | | 10 | | |
| | | 11 | | "github.com/jedi-knights/neospec/internal/domain" |
| | | 12 | | ) |
| | | 13 | | |
| | | 14 | | // githubReleaseBase is the base URL for Neovim's GitHub releases. |
| | | 15 | | const githubReleaseBase = "https://github.com/neovim/neovim/releases/download" |
| | | 16 | | |
| | | 17 | | // Downloader fetches Neovim release archives from GitHub. |
| | | 18 | | type Downloader struct { |
| | | 19 | | client *http.Client |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | // NewDownloader creates a Downloader with the default HTTP client. |
| | | 23 | | func NewDownloader() *Downloader { |
| | | 24 | | return &Downloader{client: &http.Client{}} |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | // Download fetches the release archive for version+platform and writes it to |
| | | 28 | | // destPath, creating parent directories as needed. |
| | 10 | 29 | | func (d *Downloader) Download(ctx context.Context, v domain.Version, p domain.Platform, destPath string) (retErr error) |
| | 10 | 30 | | assetName := v.AssetName(p) |
| | 1 | 31 | | if assetName == "" { |
| | 1 | 32 | | return fmt.Errorf("no asset defined for platform %s", p) |
| | 1 | 33 | | } |
| | | 34 | | |
| | 9 | 35 | | url := fmt.Sprintf("%s/%s/%s", githubReleaseBase, v.Tag, assetName) |
| | 9 | 36 | | |
| | 1 | 37 | | if err := os.MkdirAll(filepath.Dir(destPath), 0o755); err != nil { |
| | 1 | 38 | | return fmt.Errorf("creating download dir: %w", err) |
| | 1 | 39 | | } |
| | | 40 | | |
| | | 41 | | // This error branch is structurally unreachable: http.NewRequestWithContext |
| | | 42 | | // only fails when the method or URL is malformed. Both are constructed from |
| | | 43 | | // the compile-time constant http.MethodGet and a well-formed URL built from |
| | | 44 | | // the fixed githubReleaseBase prefix. No user-supplied input reaches here. |
| | 8 | 45 | | req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) |
| | 0 | 46 | | if err != nil { |
| | 0 | 47 | | return fmt.Errorf("creating request: %w", err) |
| | 0 | 48 | | } |
| | | 49 | | |
| | 8 | 50 | | resp, err := d.client.Do(req) |
| | 1 | 51 | | if err != nil { |
| | 1 | 52 | | return fmt.Errorf("fetching %s: %w", url, err) |
| | 1 | 53 | | } |
| | 7 | 54 | | defer resp.Body.Close() |
| | 7 | 55 | | |
| | 2 | 56 | | if resp.StatusCode != http.StatusOK { |
| | 2 | 57 | | return fmt.Errorf("unexpected HTTP %d fetching %s", resp.StatusCode, url) |
| | 2 | 58 | | } |
| | | 59 | | |
| | 5 | 60 | | f, err := os.Create(destPath) |
| | 1 | 61 | | if err != nil { |
| | 1 | 62 | | return fmt.Errorf("creating archive file: %w", err) |
| | 1 | 63 | | } |
| | 4 | 64 | | defer func() { |
| | 0 | 65 | | if cerr := f.Close(); cerr != nil && retErr == nil { |
| | 0 | 66 | | retErr = cerr |
| | 0 | 67 | | } |
| | | 68 | | }() |
| | | 69 | | |
| | 1 | 70 | | if _, err := io.Copy(f, resp.Body); err != nil { |
| | 1 | 71 | | return fmt.Errorf("writing archive: %w", err) |
| | 1 | 72 | | } |
| | 3 | 73 | | return nil |
| | | 74 | | } |