-
Notifications
You must be signed in to change notification settings - Fork 66
feat(service-registry artifact): allow loading artifact from remote URL #1345
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
Changes from 1 commit
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 |
|---|---|---|
| @@ -1,8 +1,14 @@ | ||
| package cmdutil | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "io/ioutil" | ||
| "net/http" | ||
| "os" | ||
| "strconv" | ||
| "strings" | ||
| ) | ||
|
|
||
| func ConvertPageValueToInt32(s string) int32 { | ||
|
|
@@ -38,3 +44,54 @@ func StringSliceToListStringWithQuotes(validOptions []string) string { | |
| } | ||
| return listF | ||
| } | ||
|
|
||
| // IsURL accepts a string and determines if it is a URL | ||
| func IsURL(s string) bool { | ||
| return strings.HasPrefix(s, "http:/") || strings.HasPrefix(s, "https:/") | ||
| } | ||
|
|
||
| // GetContentFromFileURL loads file content from the provided URL | ||
| func GetContentFromFileURL(url string, ctx context.Context) (*os.File, error) { | ||
|
||
|
|
||
| req, err := http.NewRequestWithContext(ctx, "GET", url, nil) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| client := http.DefaultClient | ||
|
|
||
| resp, err := client.Do(req) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return nil, fmt.Errorf("error loading file: %s", http.StatusText(resp.StatusCode)) | ||
|
||
| } | ||
|
|
||
| respbody := resp.Body | ||
|
|
||
| defer resp.Body.Close() | ||
|
|
||
| tmpfile, err := ioutil.TempFile("", "rhoas_file-*") | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error initializing temporary file: %w", err) | ||
|
||
| } | ||
|
|
||
| defer func() { | ||
| _ = tmpfile.Close() | ||
| _ = os.Remove(tmpfile.Name()) | ||
wtrocki marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }() | ||
|
|
||
| _, err = io.Copy(tmpfile, respbody) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| specifiedFile, err := os.Open(tmpfile.Name()) | ||
wtrocki marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return specifiedFile, nil | ||
| } | ||
|
||
Uh oh!
There was an error while loading. Please reload this page.