(subpath string)
| 66 | } |
| 67 | |
| 68 | func (p *githubPlugin) FileContent(subpath string) ([]byte, error) { |
| 69 | contentURL, err := p.url(subpath) |
| 70 | if err != nil { |
| 71 | return nil, err |
| 72 | } |
| 73 | |
| 74 | // Cache for 24 hours. Once we store the plugin in the lockfile, we |
| 75 | // should cache this indefinitely and only invalidate if the plugin |
| 76 | // is updated. |
| 77 | ttl := 24 * time.Hour |
| 78 | |
| 79 | // This is a stopgap until plugin is stored in lockfile. |
| 80 | // DEVBOX_X indicates this is an experimental env var. |
| 81 | // Use DEVBOX_X_GITHUB_PLUGIN_CACHE_TTL to override the default TTL. |
| 82 | // e.g. DEVBOX_X_GITHUB_PLUGIN_CACHE_TTL=1h will cache the plugin for 1 hour. |
| 83 | // Note: If you want to disable cache, we recommend using a low second value instead of zero to |
| 84 | // ensure only one network request is made. |
| 85 | ttlStr := os.Getenv("DEVBOX_X_GITHUB_PLUGIN_CACHE_TTL") |
| 86 | if ttlStr != "" { |
| 87 | ttl, err = time.ParseDuration(ttlStr) |
| 88 | if err != nil { |
| 89 | return nil, err |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return githubCache.GetOrSet( |
| 94 | contentURL+ttl.String(), |
| 95 | func() ([]byte, time.Duration, error) { |
| 96 | req, err := p.request(contentURL) |
| 97 | if err != nil { |
| 98 | return nil, 0, err |
| 99 | } |
| 100 | |
| 101 | client := &http.Client{} |
| 102 | res, err := client.Do(req) |
| 103 | if err != nil { |
| 104 | return nil, 0, err |
| 105 | } |
| 106 | defer res.Body.Close() |
| 107 | if res.StatusCode != http.StatusOK { |
| 108 | authInfo := "No auth header was sent with this request." |
| 109 | if req.Header.Get("Authorization") != "" { |
| 110 | authInfo = fmt.Sprintf( |
| 111 | "The auth header `%s` was sent with this request.", |
| 112 | getRedactedAuthHeader(req), |
| 113 | ) |
| 114 | } |
| 115 | return nil, 0, usererr.New( |
| 116 | "failed to get plugin %s @ %s (Status code %d).\n%s\nPlease make "+ |
| 117 | "sure a plugin.json file exists in plugin directory.", |
| 118 | p.LockfileKey(), |
| 119 | req.URL.String(), |
| 120 | res.StatusCode, |
| 121 | authInfo, |
| 122 | ) |
| 123 | } |
| 124 | body, err := io.ReadAll(res.Body) |
| 125 | if err != nil { |
no test coverage detected