-
Notifications
You must be signed in to change notification settings - Fork 77
feat: Add file:// URL scheme support for local release lists #609
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,9 +21,12 @@ package download | |
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "net/url" | ||
| "os" | ||
| "strings" | ||
| ) | ||
|
|
||
| type RequestOption = func(*http.Request) | ||
|
|
@@ -44,10 +47,17 @@ func ApplyURLTransformer(urlTransformer URLTransformer, baseURLs ...string) ([]s | |
| return transformedURLs, nil | ||
| } | ||
|
|
||
| func Bytes(ctx context.Context, url string, display func(string), checker ResponseChecker, requestOptions ...RequestOption) ([]byte, error) { | ||
| display("Downloading " + url) | ||
| func Bytes(ctx context.Context, urlStr string, display func(string), checker ResponseChecker, requestOptions ...RequestOption) ([]byte, error) { | ||
| //Handle file:// URLs | ||
| //renaming urlstr to url to avoid shadowing the package name (net/url) | ||
| scheme, _, ok := strings.Cut(urlStr, "://") | ||
| if ok && scheme == "file" { | ||
| return bytesFromFile(urlStr) | ||
| } | ||
|
|
||
| display("Downloading " + urlStr) | ||
|
|
||
| request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody) | ||
| request, err := http.NewRequestWithContext(ctx, http.MethodGet, urlStr, http.NoBody) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
@@ -69,8 +79,8 @@ func Bytes(ctx context.Context, url string, display func(string), checker Respon | |
| return io.ReadAll(response.Body) | ||
| } | ||
|
|
||
| func JSON(ctx context.Context, url string, display func(string), checker ResponseChecker, requestOptions ...RequestOption) (any, error) { | ||
| data, err := Bytes(ctx, url, display, checker, requestOptions...) | ||
| func JSON(ctx context.Context, urlStr string, display func(string), checker ResponseChecker, requestOptions ...RequestOption) (any, error) { | ||
| data, err := Bytes(ctx, urlStr, display, checker, requestOptions...) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
@@ -110,6 +120,19 @@ func NoTransform(value string) (string, error) { | |
| return value, nil | ||
| } | ||
|
|
||
| func bytesFromFile(fileURL string) ([]byte, error) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you use |
||
| //parse file:// URL to get the path | ||
| //file://path/to/file -> /path/to/file | ||
| //file://./relative/path/to/file -> ./relative/path/to/file | ||
|
|
||
| filePath := strings.TrimPrefix(fileURL, "file://") | ||
| data, err := os.ReadFile(filePath) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to read file from %s: %w", fileURL, err) | ||
| } | ||
| return data, nil | ||
| } | ||
|
|
||
| func NoCheck(*http.Response) error { | ||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,7 @@ import ( | |
| "github.com/tofuutils/tenv/v4/pkg/winbin" | ||
| htmlretriever "github.com/tofuutils/tenv/v4/versionmanager/retriever/html" | ||
| releaseapi "github.com/tofuutils/tenv/v4/versionmanager/retriever/terraform/api" | ||
| "path/filepath" | ||
| ) | ||
|
|
||
| const ( | ||
|
|
@@ -140,9 +141,20 @@ func (r TerraformRetriever) ListVersions(ctx context.Context) ([]string, error) | |
| return nil, err | ||
| } | ||
|
|
||
| baseURL, err := url.JoinPath(r.conf.Tf.GetListURL(), cmdconst.TerraformName) | ||
| if err != nil { | ||
| return nil, err | ||
| listURL := r.conf.Tf.GetListURL() | ||
| var baseURL string | ||
| if strings.HasPrefix(listURL, "file://") { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here, |
||
| // For file:// URLs, use filepath joining | ||
| filePath := strings.TrimPrefix(listURL, "file://") | ||
| filePath = filepath.Join(filePath, cmdconst.TerraformName) | ||
| baseURL = "file://" + filePath | ||
| } else { | ||
| // For HTTP URLs, use url.JoinPath | ||
| var err error | ||
| baseURL, err = url.JoinPath(listURL, cmdconst.TerraformName) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| requestOptions := config.GetBasicAuthOption(r.conf.Getenv, envname.TfRemoteUser, envname.TfRemotePass) | ||
|
|
@@ -153,9 +165,21 @@ func (r TerraformRetriever) ListVersions(ctx context.Context) ([]string, error) | |
|
|
||
| return htmlretriever.ListReleases(ctx, baseURL, r.conf.Tf.Data, requestOptions) | ||
| case config.ModeAPI: | ||
| releasesURL, err := url.JoinPath(baseURL, indexJSON) | ||
| if err != nil { | ||
| return nil, err | ||
| // releasesURL, err := url.JoinPath(baseURL, indexJSON) | ||
| // if err != nil { | ||
| // return nil, err | ||
| // } | ||
| var releasesURL string | ||
| if strings.HasPrefix(baseURL, "file://") { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably better to store in a local var that it was a file scheme, instead of checking several times
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggested improvements :
|
||
| filePath := strings.TrimPrefix(baseURL, "file://") | ||
| filePath = filepath.Join(filePath, indexJSON) | ||
| releasesURL = "file://" + filePath | ||
| } else { | ||
| var err error | ||
| releasesURL, err = url.JoinPath(baseURL, indexJSON) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| r.conf.Displayer.Display(apimsg.MsgFetchAllReleases + releasesURL) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe better to use
CutPrefix