Skip to content

Commit

Permalink
refactor URLTransformer
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Vaumoron <dvaumoron@gmail.com>
  • Loading branch information
dvaumoron committed Oct 30, 2024
1 parent 4b8c7dd commit 507a8ff
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 45 deletions.
10 changes: 5 additions & 5 deletions config/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,23 @@ func (r RemoteConfig) GetRemoteURL() string {
return strings.TrimRight(remoteURL, "/")
}

func (r RemoteConfig) GetRewriteRule() []string {
func (r RemoteConfig) GetRewriteRule() download.URLTransformer {
oldBase := r.Data["old_base_url"]
newBase := r.Data["new_base_url"]
if oldBase != "" && newBase != "" {
return []string{oldBase, newBase}
return download.NewURLTransformer(oldBase, newBase)
}

if r.GetInstallMode() == InstallModeDirect {
return nil // build correct url
return download.NoTransform // build correct url
}

listURL := r.GetListURL()
remoteURL := r.GetRemoteURL()
defaultList := listURL == r.defaultURL
defaultRemote := remoteURL == r.defaultURL
if defaultList && defaultRemote {
return nil // no special behaviour, no rewriting
return download.NoTransform // no special behaviour, no rewriting
}

oldBase = r.defaultBaseURL
Expand All @@ -125,7 +125,7 @@ func (r RemoteConfig) GetRewriteRule() []string {
newBase = remoteURL
}

return []string{oldBase, newBase}
return download.NewURLTransformer(oldBase, newBase)
}

func (r RemoteConfig) getValueForcedDefault(name string, forcedValue string, defaultValue string) string {
Expand Down
20 changes: 8 additions & 12 deletions pkg/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ const ruleSize = 2

type RequestOption = func(*http.Request)

func ApplyURLTranformer(urlTransformer func(string) (string, error), baseURLs ...string) ([]string, error) {
transformedURLs := make([]string, 0, len(baseURLs))
for _, baseURL := range baseURLs {
func ApplyURLTransformer(urlTransformer URLTransformer, baseURLs ...string) ([]string, error) {
transformedURLs := make([]string, len(baseURLs))
for index, baseURL := range baseURLs {
transformedURL, err := urlTransformer(baseURL)
if err != nil {
return nil, err
}

transformedURLs = append(transformedURLs, transformedURL)
transformedURLs[index] = transformedURL
}

return transformedURLs, nil
Expand Down Expand Up @@ -79,16 +79,12 @@ func JSON(ctx context.Context, url string, display func(string), requestOptions

func NoDisplay(string) {}

func URLTranformer(rewriteRule []string) func(string) (string, error) {
if len(rewriteRule) < ruleSize {
return noTransform
}
type URLTransformer = func(string) (string, error)

prevBaseURL := rewriteRule[0]
baseURL := rewriteRule[1]
func NewURLTransformer(prevBaseURL string, baseURL string) URLTransformer {
prevLen := len(prevBaseURL)
if prevLen == 0 || baseURL == "" {
return noTransform
return NoTransform
}

return func(urlValue string) (string, error) {
Expand All @@ -106,6 +102,6 @@ func WithBasicAuth(username string, password string) RequestOption {
}
}

func noTransform(value string) (string, error) {
func NoTransform(value string) (string, error) {
return value, nil
}
21 changes: 3 additions & 18 deletions pkg/download/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
func TestURLTransformer(t *testing.T) {
t.Parallel()

urlTransformer := download.URLTranformer([]string{"https://releases.hashicorp.com", "http://localhost:8080"})
urlTransformer := download.NewURLTransformer("https://releases.hashicorp.com", "http://localhost:8080")

value, err := urlTransformer("https://releases.hashicorp.com/terraform/1.7.0/terraform_1.7.0_linux_386.zip")
if err != nil {
Expand All @@ -39,25 +39,10 @@ func TestURLTransformer(t *testing.T) {
}
}

func TestURLTransformerDisabled(t *testing.T) {
t.Parallel()

urlTransformer := download.URLTranformer(nil)

value, err := urlTransformer("test_value")
if err != nil {
t.Fatal("Unexpected error :", err)
}

if value != "test_value" {
t.Error("Unexpected result, get :", value)
}
}

func TestURLTransformerPrefix(t *testing.T) {
t.Parallel()

urlTransformer := download.URLTranformer([]string{"https://github.com", "https://go.dev"})
urlTransformer := download.NewURLTransformer("https://github.com", "https://go.dev")

initialValue := "https://releases.hashicorp.com/terraform/1.7.0/terraform_1.7.0_darwin_amd64.zip"
value, err := urlTransformer(initialValue)
Expand All @@ -73,7 +58,7 @@ func TestURLTransformerPrefix(t *testing.T) {
func TestURLTransformerSlash(t *testing.T) {
t.Parallel()

urlTransformer := download.URLTranformer([]string{"https://releases.hashicorp.com/", "http://localhost"})
urlTransformer := download.NewURLTransformer("https://releases.hashicorp.com/", "http://localhost")

value, err := urlTransformer("https://releases.hashicorp.com/terraform/1.7.0/terraform_1.7.0_darwin_amd64.zip")
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions versionmanager/retriever/atmos/atmosretriever.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ func (r AtmosRetriever) InstallRelease(ctx context.Context, versionStr string, t
return err
}

urlTranformer := download.URLTranformer(r.conf.Atmos.GetRewriteRule())
assetURLs, err = download.ApplyURLTranformer(urlTranformer, assetURLs...)
assetURLs, err = download.ApplyURLTransformer(r.conf.Atmos.GetRewriteRule(), assetURLs...)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion versionmanager/retriever/html/htmlretriever.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func BuildAssetURLs(baseAssetURL string, assetNames ...string) ([]string, error)
return url.JoinPath(baseAssetURL, assetName)
}

return download.ApplyURLTranformer(joinTransformer, assetNames...)
return download.ApplyURLTransformer(joinTransformer, assetNames...)
}

func ListReleases(ctx context.Context, baseURL string, remoteConf map[string]string, options []download.RequestOption) ([]string, error) {
Expand Down
3 changes: 1 addition & 2 deletions versionmanager/retriever/terraform/terraformretriever.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ func (r TerraformRetriever) InstallRelease(ctx context.Context, version string,
return config.ErrInstallMode
}

urlTranformer := download.URLTranformer(r.conf.Tf.GetRewriteRule())
assetURLs, err := download.ApplyURLTranformer(urlTranformer, downloadURL, downloadSumsURL, downloadSumsSigURL)
assetURLs, err := download.ApplyURLTransformer(r.conf.Tf.GetRewriteRule(), downloadURL, downloadSumsURL, downloadSumsSigURL)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions versionmanager/retriever/terragrunt/terragruntretriever.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ func (r TerragruntRetriever) InstallRelease(ctx context.Context, versionStr stri
return err
}

urlTranformer := download.URLTranformer(r.conf.Tg.GetRewriteRule())
assetURLs, err = download.ApplyURLTranformer(urlTranformer, assetURLs...)
assetURLs, err = download.ApplyURLTransformer(r.conf.Tg.GetRewriteRule(), assetURLs...)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions versionmanager/retriever/tofu/tofuretriever.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,15 @@ func (r TofuRetriever) InstallRelease(ctx context.Context, versionStr string, ta
return err2
}

assetURLs, err = download.ApplyURLTranformer(builder.Build, assetNames...)
assetURLs, err = download.ApplyURLTransformer(builder.Build, assetNames...)
default:
return config.ErrInstallMode
}
if err != nil {
return err
}

urlTranformer := download.URLTranformer(r.conf.Tofu.GetRewriteRule())
assetURLs, err = download.ApplyURLTranformer(urlTranformer, assetURLs...)
assetURLs, err = download.ApplyURLTransformer(r.conf.Tofu.GetRewriteRule(), assetURLs...)
if err != nil {
return err
}
Expand Down

0 comments on commit 507a8ff

Please sign in to comment.