diff --git a/cmd/version.go b/cmd/version.go index d479002..80e56f4 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -1,49 +1,50 @@ -/* -Copyright © 2022 NAME HERE - -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 + +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() +} diff --git a/logic/colors.go b/logic/colors.go new file mode 100644 index 0000000..5ec576c --- /dev/null +++ b/logic/colors.go @@ -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}) +) diff --git a/logic/github.go b/logic/github.go new file mode 100644 index 0000000..44493a9 --- /dev/null +++ b/logic/github.go @@ -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 +} diff --git a/logic/version.go b/logic/version.go index 274086a..81f9ad2 100644 --- a/logic/version.go +++ b/logic/version.go @@ -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() } diff --git a/model/github-model.go b/model/github-model.go new file mode 100644 index 0000000..a028e0c --- /dev/null +++ b/model/github-model.go @@ -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 diff --git a/scripts/create-release.cmd b/scripts/create-release.cmd index 17a7e32..5de02b3 100644 --- a/scripts/create-release.cmd +++ b/scripts/create-release.cmd @@ -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" diff --git a/version-template.txt b/version-template.txt index 411a9fb..e2716d1 100644 --- a/version-template.txt +++ b/version-template.txt @@ -1,4 +1,4 @@ pasoe-cli ${version} -by Bronco Oostermeyer 2022 -Published under GPL3 -https://github.com/bfv/pasoe-cli \ No newline at end of file + Bronco Oostermeyer 2022 + Published under GPL3 + https://github.com/bfv/pasoe-cli \ No newline at end of file