Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom download URLs #203

Merged
merged 7 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/releasesjson/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ 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.
radeksimko marked this conversation as resolved.
Show resolved Hide resolved
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
Loading