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

Fix fetch of latest version #183

Merged
merged 1 commit into from
Jul 16, 2022
Merged
Changes from all 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
14 changes: 9 additions & 5 deletions pkg/utils/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"io"
"net/http"
"regexp"
"time"
Expand All @@ -14,7 +13,7 @@ import (

var versionPattern = regexp.MustCompile(`v[0-9]+\.[0-9]+\.[0-9]+`)

func fetchURL(ctx context.Context, url string, cookie *http.Cookie) ([]byte, error) {
func fetchLocation(ctx context.Context, url string, cookie *http.Cookie) (*string, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("new request: %w", err)
Expand All @@ -33,20 +32,25 @@ func fetchURL(ctx context.Context, url string, cookie *http.Cookie) ([]byte, err
if resp.StatusCode != 302 {
return nil, fmt.Errorf("HTTP error code : %d, url : %s", resp.StatusCode, url)
}
return io.ReadAll(resp.Body)
location, err := resp.Location()
if err != nil {
return nil, err
}
locationString := location.String()
return &locationString, nil
}

func FetchLatestVersion(ctx context.Context) (version string, err error) {
log.Logger.Debug("Fetch latest version from github")
body, err := fetchURL(
body, err := fetchLocation(
ctx,
"https://github.com/goodwithtech/dockle/releases/latest",
&http.Cookie{Name: "user_session", Value: "guard"},
)
if err != nil {
return "", err
}
if versionMatched := versionPattern.FindString(string(body)); versionMatched != "" {
if versionMatched := versionPattern.FindString(*body); versionMatched != "" {
return versionMatched, nil
}
return "", errors.New("not found version patterns")
Expand Down