Skip to content

Commit

Permalink
Add support for custom download URLs (#203)
Browse files Browse the repository at this point in the history
* WIP: Add support for custom download URLs

* Add note about index.json

* Adjust comment

* Add flag to CLI

* Address review comment

* Update comment

* Update internal/releasesjson/downloader.go

---------

Co-authored-by: Radek Simko <radeksimko@users.noreply.github.com>
  • Loading branch information
james0209 and radeksimko committed May 8, 2024
1 parent 7de7b37 commit 704a29e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
5 changes: 3 additions & 2 deletions internal/releasesjson/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ func (d *Downloader) DownloadAndUnpack(ctx context.Context, pv *ProductVersion,

archiveURL := pb.URL
if d.BaseURL != "" {
// ensure that absolute download links from mocked responses
// are still pointing to the mock server if one is set
// If custom URL is set, use that instead of the one from the JSON.
// Also ensures that absolute download links from mocked responses
// are still pointing to the mock server if one is set.
baseURL, err := url.Parse(d.BaseURL)
if err != nil {
return "", err
Expand Down
13 changes: 8 additions & 5 deletions releases/exact_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ type ExactVersion struct {
// instead of built-in pubkey to verify signature of downloaded checksums
ArmoredPublicKey string

apiBaseURL string
// ApiBaseURL is an optional field that specifies a custom URL to download the product from.
// If ApiBaseURL is set, the product will be downloaded from this base URL instead of the default site.
// Note: The directory structure of the custom URL must match the HashiCorp releases site (including the index.json files).
ApiBaseURL string
logger *log.Logger
pathsToRemove []string
}
Expand Down Expand Up @@ -102,8 +105,8 @@ func (ev *ExactVersion) Install(ctx context.Context) (string, error) {
ev.log().Printf("will install into dir at %s", dstDir)

rels := rjson.NewReleases()
if ev.apiBaseURL != "" {
rels.BaseURL = ev.apiBaseURL
if ev.ApiBaseURL != "" {
rels.BaseURL = ev.ApiBaseURL
}
rels.SetLogger(ev.log())
installVersion := ev.Version
Expand All @@ -124,8 +127,8 @@ func (ev *ExactVersion) Install(ctx context.Context) (string, error) {
if ev.ArmoredPublicKey != "" {
d.ArmoredPublicKey = ev.ArmoredPublicKey
}
if ev.apiBaseURL != "" {
d.BaseURL = ev.apiBaseURL
if ev.ApiBaseURL != "" {
d.BaseURL = ev.ApiBaseURL
}

licenseDir := ""
Expand Down
13 changes: 8 additions & 5 deletions releases/latest_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ type LatestVersion struct {
// instead of built-in pubkey to verify signature of downloaded checksums
ArmoredPublicKey string

apiBaseURL string
// ApiBaseURL is an optional field that specifies a custom URL to download the product from.
// If ApiBaseURL is set, the product will be downloaded from this base URL instead of the default site.
// Note: The directory structure of the custom URL must match the HashiCorp releases site (including the index.json files).
ApiBaseURL string
logger *log.Logger
pathsToRemove []string
}
Expand Down Expand Up @@ -98,8 +101,8 @@ func (lv *LatestVersion) Install(ctx context.Context) (string, error) {
lv.log().Printf("will install into dir at %s", dstDir)

rels := rjson.NewReleases()
if lv.apiBaseURL != "" {
rels.BaseURL = lv.apiBaseURL
if lv.ApiBaseURL != "" {
rels.BaseURL = lv.ApiBaseURL
}
rels.SetLogger(lv.log())
versions, err := rels.ListProductVersions(ctx, lv.Product.Name)
Expand All @@ -125,8 +128,8 @@ func (lv *LatestVersion) Install(ctx context.Context) (string, error) {
if lv.ArmoredPublicKey != "" {
d.ArmoredPublicKey = lv.ArmoredPublicKey
}
if lv.apiBaseURL != "" {
d.BaseURL = lv.apiBaseURL
if lv.ApiBaseURL != "" {
d.BaseURL = lv.ApiBaseURL
}
licenseDir := ""
if lv.Enterprise != nil {
Expand Down
6 changes: 3 additions & 3 deletions releases/releases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestLatestVersion_basic(t *testing.T) {
lv := &LatestVersion{
Product: product.Terraform,
ArmoredPublicKey: getTestPubKey(t),
apiBaseURL: testutil.NewTestServer(t, mockApiRoot).URL,
ApiBaseURL: testutil.NewTestServer(t, mockApiRoot).URL,
}
lv.SetLogger(testutil.TestLogger())

Expand Down Expand Up @@ -95,7 +95,7 @@ func TestLatestVersion_prereleases(t *testing.T) {
Product: product.Terraform,
IncludePrereleases: true,
ArmoredPublicKey: getTestPubKey(t),
apiBaseURL: testutil.NewTestServer(t, mockApiRoot).URL,
ApiBaseURL: testutil.NewTestServer(t, mockApiRoot).URL,
}
lv.SetLogger(testutil.TestLogger())

Expand Down Expand Up @@ -167,7 +167,7 @@ func BenchmarkExactVersion(b *testing.B) {
Product: product.Terraform,
Version: version.Must(version.NewVersion("0.14.11")),
ArmoredPublicKey: getTestPubKey(b),
apiBaseURL: testutil.NewTestServer(b, mockApiRoot).URL,
ApiBaseURL: testutil.NewTestServer(b, mockApiRoot).URL,
InstallDir: installDir,
}
ev.SetLogger(testutil.TestLogger())
Expand Down

0 comments on commit 704a29e

Please sign in to comment.