Skip to content

Commit

Permalink
#6 added release check to version command
Browse files Browse the repository at this point in the history
  • Loading branch information
bfv committed Aug 19, 2022
1 parent 067ba64 commit e2123fe
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 4 deletions.
7 changes: 7 additions & 0 deletions logic/colors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package logic

var (
red = string([]byte{27, 91, 51, 49, 109})
yellow = string([]byte{27, 91, 51, 51, 109})
reset = string([]byte{27, 91, 48, 109})
)
65 changes: 65 additions & 0 deletions logic/github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package logic

import (
"encoding/json"
"fmt"
"net/http"
"os"
"time"

"github.com/bfv/pasoe-cli/model"
)

func GetLatestRelease() (model.GitRelease, error) {

releases, err := GetReleases()
if err != nil {
fmt.Println("err3")
printError(err)
os.Exit(1)
}

return releases[0], nil
}

func GetReleases() (model.GitReleases, error) {

res, _ := doGithubRequest("GET", "releases")
defer res.Body.Close()

releases := model.GitReleases{}
err := json.NewDecoder(res.Body).Decode(&releases)
if err != nil {
fmt.Println("err3")
printError(err)
os.Exit(1)
}

return releases, nil
}

func doGithubRequest(verb string, path string) (*http.Response, error) {

client := &http.Client{
Timeout: time.Second * 5,
}

url := fmt.Sprintf("https://api.github.com/repos/bfv/pasoe-cli/%v", path)
req, err := http.NewRequest(verb, url, nil)
if err != nil {
fmt.Println("err1")
printError(err)
os.Exit(1)
}

req.Header.Add("Accept", "application/vnd.github+json")

res, err := client.Do(req)
if err != nil {
fmt.Println("err2")
printError(err)
os.Exit(1)
}

return res, nil
}
14 changes: 14 additions & 0 deletions logic/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,25 @@ package logic
import (
_ "embed"
"fmt"
"runtime"
"strings"
)

//go:embed version.txt
var vStr string

func DisplayVersion() {
thisVersion := strings.Split(strings.Split(vStr, "\n")[0], " ")[1]
fmt.Printf("\n%v\n", vStr)
latest, _ := GetLatestRelease()
if thisVersion != latest.Name {
fmt.Printf("%vupdate available: %v as of %v%v\n", red, latest.Name, latest.ReleasedAt, reset)
if runtime.GOOS == "linux" {
fmt.Printf(" %v%v%v\n", yellow, latest.LinuxArchiveURL, reset)
} else {
fmt.Printf(" %v%v%v\n", yellow, latest.WindowsArchiveURL, reset)
}
}

fmt.Println()
}
10 changes: 10 additions & 0 deletions model/github-model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package model

type GitRelease struct {
Name string `json:"name"`
ReleasedAt string `json:"published_at"`
LinuxArchiveURL string `json:"tarball_url"`
WindowsArchiveURL string `json:"zipball_url"`
}

type GitReleases []GitRelease
2 changes: 1 addition & 1 deletion scripts/create-release.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ sed -i "s/${version}/%1/" logic/version.txt

rem 2. commit the file
git add logic/version.txt
git commit -m "bumped version to %1"
git commit -m "release version %1"

rem 3. create, commit an annotated tag
git tag -a %1 -m "release: %1"
Expand Down
6 changes: 3 additions & 3 deletions version-template.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pasoe-cli ${version}
by Bronco Oostermeyer 2022
Published under GPL3
https://github.com/bfv/pasoe-cli
Bronco Oostermeyer 2022
Published under GPL3
https://github.com/bfv/pasoe-cli

0 comments on commit e2123fe

Please sign in to comment.