diff --git a/src/cmd/root.go b/src/cmd/root.go index acec4bd..cf07393 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -1,7 +1,9 @@ package cmd import ( + "encoding/json" "fmt" + "net/http" "os" "strings" @@ -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 { @@ -67,6 +72,9 @@ 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 @@ -74,6 +82,9 @@ var rootCmd = &cobra.Command{ 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 @@ -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 { @@ -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 @@ -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")