-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from NiloCK/versionChecks
Version checks - wip
- Loading branch information
Showing
8 changed files
with
234 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
name: Go Test | ||
|
||
on: [push, pull_request] | ||
|
||
jobs: | ||
test: | ||
name: Run Go Tests | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
with: | ||
fetch-depth: 0 | ||
with-tags: true | ||
- name: Set up Go | ||
uses: actions/setup-go@v2 | ||
with: | ||
go-version: '1.20' | ||
- name: Test | ||
run: go test ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Launch Package", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "auto", | ||
"program": "./main.go" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package utils | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
const version = "v0.0.8" | ||
const ReleaseURL = "https://github.com/NiloCK/tuido/releases/latest" | ||
|
||
// Version returns the currently running version of the application. | ||
func Version() string { | ||
return version | ||
} | ||
|
||
func getLatestRedirectURL() (string, error) { | ||
|
||
client := &http.Client{ | ||
CheckRedirect: func(req *http.Request, via []*http.Request) error { | ||
return http.ErrUseLastResponse // Stop after the first redirect | ||
}, | ||
} | ||
|
||
resp, err := client.Get(ReleaseURL) | ||
if err != nil { | ||
// If the error is due to stopping redirects, extract the Location header | ||
var urlErr *url.Error | ||
if errors.As(err, &urlErr) && urlErr.Err == http.ErrUseLastResponse { | ||
if location, err := resp.Location(); err == nil { | ||
return location.String(), nil | ||
} | ||
} | ||
return "", err | ||
} | ||
defer resp.Body.Close() | ||
|
||
loc, err := resp.Location() | ||
if err != nil { | ||
return loc.String(), nil | ||
} | ||
|
||
split := strings.Split(loc.String(), "/") | ||
version := split[len(split)-1] | ||
if version != "" { | ||
return version, nil | ||
} | ||
|
||
return "", errors.New("no redirect") | ||
} | ||
|
||
func LatestVersion() string { | ||
// lookup latest version from github | ||
latest, err := getLatestRedirectURL() | ||
|
||
if err != nil { | ||
return fmt.Sprintf("Error getting latest version: %s", err) | ||
} | ||
|
||
return latest | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package utils_test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/go-git/go-git/v5" | ||
"github.com/go-git/go-git/v5/plumbing" | ||
"github.com/nilock/tuido/utils" | ||
) | ||
|
||
func TestVersion(t *testing.T) { | ||
// get latest tag | ||
|
||
// print working directory | ||
wd, err := os.Getwd() | ||
if err != nil { | ||
t.Fatalf("Error getting working directory: %s", err) | ||
} | ||
|
||
fmt.Println("PWD:", wd) | ||
|
||
gitRepo, err := git.PlainOpen("..") | ||
if err != nil { | ||
t.Fatalf("Error opening git repo: %s", err) | ||
} | ||
|
||
tagRefs, err := gitRepo.Tags() | ||
tags := []string{} | ||
|
||
tagRefs.ForEach(func(tag *plumbing.Reference) error { | ||
tags = append(tags, tag.Name().Short()) | ||
return nil | ||
}) | ||
|
||
want := tags[len(tags)-1] | ||
|
||
if got := utils.Version(); got != want { | ||
t.Errorf("Version() = %v, want %v", got, want) | ||
} | ||
} |