Skip to content

Commit 79dc578

Browse files
committed
feat: check for tag version on github to let the user know they should update to the new version
1 parent 2794a90 commit 79dc578

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/cmd/root.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package cmd
22

33
import (
4+
"encoding/json"
45
"fmt"
6+
"net/http"
57
"os"
68
"strings"
79

@@ -14,6 +16,9 @@ import (
1416

1517
// Variables lives in here in case of possible future check of updates on running the CLI
1618
var Coco_Version string
19+
var update bool
20+
21+
// github_tag struct to hold the tag name from the github api response
1722

1823
// rootCmd represents the base command when called without any subcommands
1924
// func RootCmd() *cobra.Command {
@@ -67,13 +72,19 @@ var rootCmd = &cobra.Command{
6772
screen.MoveTopLeft()
6873
sel_auth := tui.Entry()
6974
message = utils.Commit(args[0], sel_auth)
75+
if update {
76+
fmt.Print("--* A new version of cocommit is available. Please update to the latest version *-- \n\n")
77+
}
7078
if tflag {
7179
fmt.Println(message)
7280
return
7381
}
7482
goto tui
7583
case 1:
7684
if len(args) == 1 {
85+
if update {
86+
fmt.Print("--* A new version of cocommit is available. Please update to the latest version *-- \n\n")
87+
}
7788
if tflag {
7889
fmt.Println(args[0])
7990
return
@@ -106,6 +117,10 @@ var rootCmd = &cobra.Command{
106117
return
107118
}
108119

120+
// if update {
121+
// fmt.Print("--* A new version of cocommit is available. Please update to the latest version *-- \n\n")
122+
// }
123+
109124
utils.GitWrapper(message, git_flags)
110125
// prints the commit message to the console if the print flag is set
111126
if pflag {
@@ -120,6 +135,9 @@ var rootCmd = &cobra.Command{
120135
// Execute adds all child commands to the root command and sets flags appropriately.
121136
// This is called by main.main(). It only needs to happen once to the rootCmd.
122137
func Execute() {
138+
// check for update
139+
check_update()
140+
123141
// author file check
124142
author_file := utils.CheckAuthorFile()
125143
// define users
@@ -131,6 +149,28 @@ func Execute() {
131149
}
132150
}
133151

152+
// function to check for updates (check tag version from repo with the current version)
153+
func check_update() {
154+
var tag github_release
155+
tags, err := http.Get("https://api.github.com/repos/Slug-Boi/cocommit/releases/latest")
156+
if err != nil {
157+
fmt.Println("Could not fetch tags from github API")
158+
return
159+
}
160+
defer tags.Body.Close()
161+
162+
err = json.NewDecoder(tags.Body).Decode(&tag)
163+
if err != nil {
164+
fmt.Println("Error decoding json response from github API")
165+
return
166+
}
167+
168+
// NOTE: maybe change to a split and parse method idk if this can cause issues
169+
if tag.TagName != Coco_Version {
170+
update = true
171+
}
172+
}
173+
134174
func init() {
135175
//rootCmD := RootCmd()
136176
rootCmd.Flags().BoolP("print-output", "o", false, "Prints the commit message to the console")

0 commit comments

Comments
 (0)