Skip to content

Commit

Permalink
Merge pull request #132 from Revolyssup/getRelease
Browse files Browse the repository at this point in the history
Moved getlatestreleasetag function to meshkit
  • Loading branch information
tangledbytes authored Nov 25, 2021
2 parents 362b73f + 226e533 commit 2f5d54b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/nats-io/nats.go v1.9.1
github.com/onsi/ginkgo v1.14.1 // indirect
github.com/onsi/gomega v1.10.2 // indirect
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.1.3
github.com/spf13/viper v1.8.1
Expand Down
8 changes: 6 additions & 2 deletions utils/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ var (
ErrRemoteFileNotFoundCode = "11052"
ErrReadingRemoteFileCode = "11053"
ErrReadingLocalFileCode = "11054"

ErrInvalidProtocol = errors.New(ErrInvalidProtocolCode, errors.Alert, []string{"invalid protocol: only http, https and file are valid protocols"}, []string{}, []string{"Network protocol is incorrect"}, []string{"Make sure to specify the right network protocol"})
ErrGettingLatestReleaseTagCode = "11055"
ErrInvalidProtocol = errors.New(ErrInvalidProtocolCode, errors.Alert, []string{"invalid protocol: only http, https and file are valid protocols"}, []string{}, []string{"Network protocol is incorrect"}, []string{"Make sure to specify the right network protocol"})
)

func ErrUnmarshal(err error) error {
Expand Down Expand Up @@ -67,3 +67,7 @@ func ErrReadingRemoteFile(err error) error {
func ErrReadingLocalFile(err error) error {
return errors.New(ErrReadingLocalFileCode, errors.Alert, []string{"error reading local file"}, []string{err.Error()}, []string{"File doesnt exist in the location", "File name is incorrect"}, []string{"Make sure to input the right file name and location"})
}

func ErrGettingLatestReleaseTag(err error) error {
return errors.New(ErrGettingLatestReleaseTagCode, errors.Alert, []string{"Could not fetch latest stable release from github"}, []string{err.Error()}, []string{"Failed to make GET request to github", "Invalid response recieved on github.com/<org>/<repo>/releases/stable"}, []string{"Make sure Github is reachable", "Make sure a valid response is available on github.com/<org>/<repo>/releases/stable"})
}
38 changes: 38 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ package utils
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/user"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"

log "github.com/sirupsen/logrus"
)

// unmarshal returns parses the JSON config data and stores the value in the reference to result
Expand Down Expand Up @@ -182,3 +186,37 @@ func ReadLocalFile(location string) (string, error) {

return string(data), nil
}

// Gets the latest stable release tag from github for a given org name and repo name(in that org)
func GetLatestReleaseTag(org string, repo string) (string, error) {
var url string = "https://github.com/" + org + "/" + repo + "/releases/latest"
resp, err := http.Get(url)
if err != nil {
return "", ErrGettingLatestReleaseTag(err)
}
defer safeClose(resp.Body)

if resp.StatusCode != http.StatusOK {
return "", ErrGettingLatestReleaseTag(err)
}

body, err := io.ReadAll(resp.Body)
if err != nil {
return "", ErrGettingLatestReleaseTag(err)
}
re := regexp.MustCompile("/releases/tag/(.*?)\"")
releases := re.FindAllString(string(body), -1)
if len(releases) == 0 {
return "", ErrGettingLatestReleaseTag(errors.New("no release found in this repository"))
}
latest := strings.ReplaceAll(releases[0], "/releases/tag/", "")
latest = strings.ReplaceAll(latest, "\"", "")
return latest, nil
}

// SafeClose is a helper function help to close the io
func safeClose(co io.Closer) {
if cerr := co.Close(); cerr != nil {
log.Error(cerr)
}
}

0 comments on commit 2f5d54b

Please sign in to comment.