Skip to content

Commit

Permalink
Merge pull request #7 from bfv/6-add-check-for-new-versions
Browse files Browse the repository at this point in the history
#6 add check for new versions
  • Loading branch information
bfv authored Aug 19, 2022
2 parents f886877 + e2123fe commit 3e5c57a
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 53 deletions.
99 changes: 50 additions & 49 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,50 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"github.com/bfv/pasoe-cli/logic"
"github.com/spf13/cobra"
)

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "Displays the version number",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
displayVersion()
},
}

func init() {
rootCmd.AddCommand(versionCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// versionCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// versionCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

func displayVersion() {
logic.DisplayVersion()
}
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"github.com/bfv/pasoe-cli/logic"
"github.com/spf13/cobra"
)

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Aliases: []string{"about"},
Short: "Displays the version number",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
displayVersion()
},
}

func init() {
rootCmd.AddCommand(versionCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// versionCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// versionCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

func displayVersion() {
logic.DisplayVersion()
}
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 3e5c57a

Please sign in to comment.