Skip to content

Commit

Permalink
feat: check for tag version on github to let the user know they shoul…
Browse files Browse the repository at this point in the history
…d update to the new version
  • Loading branch information
Slug-Boi committed Jan 13, 2025
1 parent 2794a90 commit 79dc578
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cmd

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

Expand All @@ -14,6 +16,9 @@ import (

// Variables lives in here in case of possible future check of updates on running the CLI
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
// func RootCmd() *cobra.Command {
Expand Down Expand Up @@ -67,13 +72,19 @@ var rootCmd = &cobra.Command{
screen.MoveTopLeft()
sel_auth := tui.Entry()
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 {
fmt.Println(message)
return
}
goto tui
case 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 {
fmt.Println(args[0])
return
Expand Down Expand Up @@ -106,6 +117,10 @@ var rootCmd = &cobra.Command{
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)
// prints the commit message to the console if the print flag is set
if pflag {
Expand All @@ -120,6 +135,9 @@ var rootCmd = &cobra.Command{
// 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.
func Execute() {
// check for update
check_update()

// author file check
author_file := utils.CheckAuthorFile()
// define users
Expand All @@ -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() {
//rootCmD := RootCmd()
rootCmd.Flags().BoolP("print-output", "o", false, "Prints the commit message to the console")
Expand Down

0 comments on commit 79dc578

Please sign in to comment.