Skip to content

Commit

Permalink
reimplement functions in go-mouff-update, use ghreposervice
Browse files Browse the repository at this point in the history
Signed-off-by: novahow <[email protected]>
  • Loading branch information
novahow committed Jun 10, 2024
1 parent cd37d1b commit 95e854a
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 5 deletions.
7 changes: 4 additions & 3 deletions flytectl/cmd/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,20 @@ func upgrade(u *updater.Updater) (string, error) {
}

func isUpgradeSupported(goos platformutil.Platform) (bool, error) {
latest, err := github.FlytectlReleaseConfig.GetLatestVersion()
latest, err := github.FlytectlReleaseConfig.Provider.GetLatestVersion()
if err != nil {
return false, err
}

if isGreater, err := util.IsVersionGreaterThan(latest, stdlibversion.Version); err != nil {
compatible_version := strings.TrimPrefix(latest, fmt.Sprintf("%s/", github.FlytectlReleaseConfig.ExecutableName))
if isGreater, err := util.IsVersionGreaterThan(compatible_version, stdlibversion.Version); err != nil {
return false, err
} else if !isGreater {
fmt.Println("You already have the latest version of Flytectl")
return false, nil
}

message, err := github.GetUpgradeMessage(latest, goos)
message, err := github.GetUpgradeMessage(compatible_version, goos)
if err != nil {
return false, err
}
Expand Down
3 changes: 1 addition & 2 deletions flytectl/pkg/github/githubutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
stdlibversion "github.com/flyteorg/flyte/flytestdlib/version"

"github.com/google/go-github/v42/github"
"github.com/mouuff/go-rocket-update/pkg/provider"
"github.com/mouuff/go-rocket-update/pkg/updater"
"golang.org/x/oauth2"
"golang.org/x/text/cases"
Expand All @@ -40,7 +39,7 @@ var Client GHRepoService

// FlytectlReleaseConfig represent the updater config for flytectl binary
var FlytectlReleaseConfig = &updater.Updater{
Provider: &provider.Github{
Provider: &GitHubProvider{
RepositoryURL: flytectlRepository,
ArchiveName: getFlytectlAssetName(),
},
Expand Down
170 changes: 170 additions & 0 deletions flytectl/pkg/github/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package github

import (
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"

go_github "github.com/google/go-github/v42/github"
"github.com/mouuff/go-rocket-update/pkg/provider"
)

// type GitHubProvider struct {
// provider.Github
// }

// Github provider finds a archive file in the repository's releases to provide files
type GitHubProvider struct {
RepositoryURL string // Repository URL, example github.com/mouuff/go-rocket-update
ArchiveName string // Archive name (the zip/tar.gz you upload for a release on github), example: binaries.zip

tmpDir string // temporary directory this is used internally
decompressProvider provider.Provider // provider used to decompress the downloaded archive
archivePath string // path to the downloaded archive (should be in tmpDir)
}

// githubTag struct used to unmarshal response from github
// https://api.github.com/repos/ownerName/projectName/tags
type githubTag struct {
Name string `json:"name"`
}

// githubRepositoryInfo is used to get the name of the project and the owner name
// from this fields we are able to get other links (such as the release and tags link)
type githubRepositoryInfo struct {
RepositoryOwner string
RepositoryName string
}

// getRepositoryInfo parses the github repository URL
func (c *GitHubProvider) repositoryInfo() (*githubRepositoryInfo, error) {
re := regexp.MustCompile(`github\.com/(.*?)/(.*?)$`)
submatches := re.FindAllStringSubmatch(c.RepositoryURL, 1)
if len(submatches) < 1 {
return nil, errors.New("Invalid github URL:" + c.RepositoryURL)

Check warning on line 50 in flytectl/pkg/github/provider.go

View check run for this annotation

Codecov / codecov/patch

flytectl/pkg/github/provider.go#L46-L50

Added lines #L46 - L50 were not covered by tests
}
return &githubRepositoryInfo{
RepositoryOwner: submatches[0][1],
RepositoryName: submatches[0][2],
}, nil

Check warning on line 55 in flytectl/pkg/github/provider.go

View check run for this annotation

Codecov / codecov/patch

flytectl/pkg/github/provider.go#L52-L55

Added lines #L52 - L55 were not covered by tests
}

// getArchiveURL get the archive URL for the github repository
// If no tag is provided then the latest version is selected
func (c *GitHubProvider) getArchiveURL(tag string) (string, error) {
if len(tag) == 0 {

Check warning on line 61 in flytectl/pkg/github/provider.go

View check run for this annotation

Codecov / codecov/patch

flytectl/pkg/github/provider.go#L60-L61

Added lines #L60 - L61 were not covered by tests
// Get latest version if no tag is provided
var err error
tag, err = c.GetLatestVersion()
if err != nil {
return "", err

Check warning on line 66 in flytectl/pkg/github/provider.go

View check run for this annotation

Codecov / codecov/patch

flytectl/pkg/github/provider.go#L63-L66

Added lines #L63 - L66 were not covered by tests
}
}

info, err := c.repositoryInfo()
if err != nil {
return "", err

Check warning on line 72 in flytectl/pkg/github/provider.go

View check run for this annotation

Codecov / codecov/patch

flytectl/pkg/github/provider.go#L70-L72

Added lines #L70 - L72 were not covered by tests
}
return fmt.Sprintf("https://github.com/%s/%s/releases/download/%s/%s",
info.RepositoryOwner,
info.RepositoryName,
tag,
c.ArchiveName,
), nil

Check warning on line 79 in flytectl/pkg/github/provider.go

View check run for this annotation

Codecov / codecov/patch

flytectl/pkg/github/provider.go#L74-L79

Added lines #L74 - L79 were not covered by tests
}

// Open opens the provider
func (c *GitHubProvider) Open() (err error) {
archiveURL, err := c.getArchiveURL("") // get archive url for latest version
if err != nil {
return

Check warning on line 86 in flytectl/pkg/github/provider.go

View check run for this annotation

Codecov / codecov/patch

flytectl/pkg/github/provider.go#L83-L86

Added lines #L83 - L86 were not covered by tests
}
resp, err := http.Get(archiveURL)
if err != nil {
return

Check warning on line 90 in flytectl/pkg/github/provider.go

View check run for this annotation

Codecov / codecov/patch

flytectl/pkg/github/provider.go#L88-L90

Added lines #L88 - L90 were not covered by tests
}
defer resp.Body.Close()

Check warning on line 92 in flytectl/pkg/github/provider.go

View check run for this annotation

Codecov / codecov/patch

flytectl/pkg/github/provider.go#L92

Added line #L92 was not covered by tests

c.tmpDir, err = os.MkdirTemp("", "rocket-update")
if err != nil {
return

Check warning on line 96 in flytectl/pkg/github/provider.go

View check run for this annotation

Codecov / codecov/patch

flytectl/pkg/github/provider.go#L94-L96

Added lines #L94 - L96 were not covered by tests
}

c.archivePath = filepath.Join(c.tmpDir, c.ArchiveName)
archiveFile, err := os.Create(c.archivePath)
if err != nil {
return

Check warning on line 102 in flytectl/pkg/github/provider.go

View check run for this annotation

Codecov / codecov/patch

flytectl/pkg/github/provider.go#L99-L102

Added lines #L99 - L102 were not covered by tests
}
_, err = io.Copy(archiveFile, resp.Body)
archiveFile.Close()
if err != nil {
return

Check warning on line 107 in flytectl/pkg/github/provider.go

View check run for this annotation

Codecov / codecov/patch

flytectl/pkg/github/provider.go#L104-L107

Added lines #L104 - L107 were not covered by tests
}
c.decompressProvider, err = provider.Decompress(c.archivePath)
if err != nil {
return nil

Check warning on line 111 in flytectl/pkg/github/provider.go

View check run for this annotation

Codecov / codecov/patch

flytectl/pkg/github/provider.go#L109-L111

Added lines #L109 - L111 were not covered by tests
}
return c.decompressProvider.Open()

Check warning on line 113 in flytectl/pkg/github/provider.go

View check run for this annotation

Codecov / codecov/patch

flytectl/pkg/github/provider.go#L113

Added line #L113 was not covered by tests
}

// Close closes the provider
func (c *GitHubProvider) Close() error {
if c.decompressProvider != nil {
c.decompressProvider.Close()
c.decompressProvider = nil

Check warning on line 120 in flytectl/pkg/github/provider.go

View check run for this annotation

Codecov / codecov/patch

flytectl/pkg/github/provider.go#L117-L120

Added lines #L117 - L120 were not covered by tests
}

if len(c.tmpDir) > 0 {
os.RemoveAll(c.tmpDir)
c.tmpDir = ""
c.archivePath = ""

Check warning on line 126 in flytectl/pkg/github/provider.go

View check run for this annotation

Codecov / codecov/patch

flytectl/pkg/github/provider.go#L123-L126

Added lines #L123 - L126 were not covered by tests
}
return nil

Check warning on line 128 in flytectl/pkg/github/provider.go

View check run for this annotation

Codecov / codecov/patch

flytectl/pkg/github/provider.go#L128

Added line #L128 was not covered by tests
}

// GetLatestVersion gets the latest version
func (c *GitHubProvider) GetLatestVersion() (string, error) {
tags, err := c.getReleases()
if err != nil {
return "", err

Check warning on line 135 in flytectl/pkg/github/provider.go

View check run for this annotation

Codecov / codecov/patch

flytectl/pkg/github/provider.go#L132-L135

Added lines #L132 - L135 were not covered by tests
}
latest_tag := tags[0].GetTagName()
return latest_tag, err

Check warning on line 138 in flytectl/pkg/github/provider.go

View check run for this annotation

Codecov / codecov/patch

flytectl/pkg/github/provider.go#L137-L138

Added lines #L137 - L138 were not covered by tests
}

func (c *GitHubProvider) getReleases() ([]*go_github.RepositoryRelease, error) {
g := GetGHRepoService()
releases, _, err := g.ListReleases(context.Background(), owner, flyte, &go_github.ListOptions{
PerPage: 100,
})
if err != nil {
return nil, err

Check warning on line 147 in flytectl/pkg/github/provider.go

View check run for this annotation

Codecov / codecov/patch

flytectl/pkg/github/provider.go#L141-L147

Added lines #L141 - L147 were not covered by tests
}
var filteredReleases []*go_github.RepositoryRelease
for _, release := range releases {
if strings.HasPrefix(release.GetTagName(), flytectl) {
filteredReleases = append(filteredReleases, release)

Check warning on line 152 in flytectl/pkg/github/provider.go

View check run for this annotation

Codecov / codecov/patch

flytectl/pkg/github/provider.go#L149-L152

Added lines #L149 - L152 were not covered by tests
}
}
return filteredReleases, err

Check warning on line 155 in flytectl/pkg/github/provider.go

View check run for this annotation

Codecov / codecov/patch

flytectl/pkg/github/provider.go#L155

Added line #L155 was not covered by tests
}

// Walk walks all the files provided
func (c *GitHubProvider) Walk(walkFn provider.WalkFunc) error {
if c.decompressProvider == nil {

Check warning on line 160 in flytectl/pkg/github/provider.go

View check run for this annotation

Codecov / codecov/patch

flytectl/pkg/github/provider.go#L159-L160

Added lines #L159 - L160 were not covered by tests
// TODO specify error
return provider.ErrNotOpenned

Check warning on line 162 in flytectl/pkg/github/provider.go

View check run for this annotation

Codecov / codecov/patch

flytectl/pkg/github/provider.go#L162

Added line #L162 was not covered by tests
}
return c.decompressProvider.Walk(walkFn)

Check warning on line 164 in flytectl/pkg/github/provider.go

View check run for this annotation

Codecov / codecov/patch

flytectl/pkg/github/provider.go#L164

Added line #L164 was not covered by tests
}

// Retrieve file relative to "provider" to destination
func (c *GitHubProvider) Retrieve(src string, dest string) error {
return c.decompressProvider.Retrieve(src, dest)

Check warning on line 169 in flytectl/pkg/github/provider.go

View check run for this annotation

Codecov / codecov/patch

flytectl/pkg/github/provider.go#L168-L169

Added lines #L168 - L169 were not covered by tests
}

0 comments on commit 95e854a

Please sign in to comment.