feat: check for tag version on github to let the user know they should update to the new version

This commit is contained in:
Slug-Boi
2025-01-13 15:57:59 +01:00
parent 2794a9038b
commit 79dc578b56
+40
View File
@@ -1,7 +1,9 @@
package cmd package cmd
import ( import (
"encoding/json"
"fmt" "fmt"
"net/http"
"os" "os"
"strings" "strings"
@@ -14,6 +16,9 @@ import (
// Variables lives in here in case of possible future check of updates on running the CLI // Variables lives in here in case of possible future check of updates on running the CLI
var Coco_Version string var Coco_Version string
var update bool
// github_tag struct to hold the tag name from the github api response
// rootCmd represents the base command when called without any subcommands // rootCmd represents the base command when called without any subcommands
// func RootCmd() *cobra.Command { // func RootCmd() *cobra.Command {
@@ -67,6 +72,9 @@ var rootCmd = &cobra.Command{
screen.MoveTopLeft() screen.MoveTopLeft()
sel_auth := tui.Entry() sel_auth := tui.Entry()
message = utils.Commit(args[0], sel_auth) message = utils.Commit(args[0], sel_auth)
if update {
fmt.Print("--* A new version of cocommit is available. Please update to the latest version *-- \n\n")
}
if tflag { if tflag {
fmt.Println(message) fmt.Println(message)
return return
@@ -74,6 +82,9 @@ var rootCmd = &cobra.Command{
goto tui goto tui
case 1: case 1:
if len(args) == 1 { if len(args) == 1 {
if update {
fmt.Print("--* A new version of cocommit is available. Please update to the latest version *-- \n\n")
}
if tflag { if tflag {
fmt.Println(args[0]) fmt.Println(args[0])
return return
@@ -106,6 +117,10 @@ var rootCmd = &cobra.Command{
return return
} }
// if update {
// fmt.Print("--* A new version of cocommit is available. Please update to the latest version *-- \n\n")
// }
utils.GitWrapper(message, git_flags) utils.GitWrapper(message, git_flags)
// prints the commit message to the console if the print flag is set // prints the commit message to the console if the print flag is set
if pflag { if pflag {
@@ -120,6 +135,9 @@ var rootCmd = &cobra.Command{
// Execute adds all child commands to the root command and sets flags appropriately. // Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd. // This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() { func Execute() {
// check for update
check_update()
// author file check // author file check
author_file := utils.CheckAuthorFile() author_file := utils.CheckAuthorFile()
// define users // define users
@@ -131,6 +149,28 @@ func Execute() {
} }
} }
// function to check for updates (check tag version from repo with the current version)
func check_update() {
var tag github_release
tags, err := http.Get("https://api.github.com/repos/Slug-Boi/cocommit/releases/latest")
if err != nil {
fmt.Println("Could not fetch tags from github API")
return
}
defer tags.Body.Close()
err = json.NewDecoder(tags.Body).Decode(&tag)
if err != nil {
fmt.Println("Error decoding json response from github API")
return
}
// NOTE: maybe change to a split and parse method idk if this can cause issues
if tag.TagName != Coco_Version {
update = true
}
}
func init() { func init() {
//rootCmD := RootCmd() //rootCmD := RootCmd()
rootCmd.Flags().BoolP("print-output", "o", false, "Prints the commit message to the console") rootCmd.Flags().BoolP("print-output", "o", false, "Prints the commit message to the console")