11package cmd
22
33import (
4+ "encoding/json"
45 "fmt"
6+ "net/http"
57 "os"
68 "strings"
79
810 "github.com/Slug-Boi/cocommit/src/cmd/tui"
911 "github.com/Slug-Boi/cocommit/src/cmd/utils"
1012 "github.com/inancgumus/screen"
13+ "github.com/charmbracelet/lipgloss"
1114
1215 "github.com/spf13/cobra"
1316)
1417
1518// Variables lives in here in case of possible future check of updates on running the CLI
1619var Coco_Version string
20+ var update bool
21+
22+ // print styling for the output for the CLI
23+ var update_style = lipgloss .NewStyle ().Bold (true ).Foreground (lipgloss .Color ("#1aff00" ))
24+ var msg_style = lipgloss .NewStyle ().BorderStyle (lipgloss .RoundedBorder ()).BorderForeground (lipgloss .Color ("170" ))
25+
26+ // github_tag struct to hold the tag name from the github api response
1727
1828// rootCmd represents the base command when called without any subcommands
1929// func RootCmd() *cobra.Command {
@@ -33,6 +43,12 @@ var rootCmd = &cobra.Command{
3343 Run : func (cmd * cobra.Command , args []string ) {
3444 var message string
3545
46+ // check if user included -m tag and remove. Wrap around for safety's sake
47+ if len (args ) > 0 && args [0 ] == "-m" {
48+ // maybe change to a walk in case it pops up later?
49+ args = args [1 :]
50+ }
51+
3652 // check if the print flag is set
3753 pflag , _ := cmd .Flags ().GetBool ("print-output" )
3854 tflag , _ := cmd .Flags ().GetBool ("test_print" )
@@ -43,6 +59,9 @@ var rootCmd = &cobra.Command{
4359
4460 if vflag {
4561 fmt .Println ("Cocommit version:" , Coco_Version )
62+ if update {
63+ update_msg ()
64+ }
4665 os .Exit (0 )
4766 }
4867
@@ -53,54 +72,38 @@ var rootCmd = &cobra.Command{
5372 }
5473
5574 if aflag {
56- tui .Entry ()
57- os .Exit (0 )
75+ sel_auth := tui .Entry ()
76+ if len (args ) == 0 {
77+ if update {
78+ update_msg ()
79+ }
80+ os .Exit (0 )
81+ }
82+ args = append (args , sel_auth ... )
83+ goto skip
5884 }
5985 // run execute commands again as root run will not call this part
6086 // redundant check for now but will be useful later when we add tui
61- wrap_around:
6287 switch len (args ) {
6388 case 0 :
6489 // launch the tui
65- args = append (args , tui .Entry_CM ())
66- screen .Clear ()
67- screen .MoveTopLeft ()
68- sel_auth := tui .Entry ()
69- message = utils .Commit (args [0 ], sel_auth )
70- if tflag {
71- fmt .Println (message )
72- return
73- }
74- goto tui
90+ args = call_tui (args )
7591 case 1 :
7692 if len (args ) == 1 {
77- if tflag {
78- fmt .Println (args [0 ])
79- return
80- }
81-
82- utils .GitWrapper (args [0 ], git_flags )
83- if pflag {
84- fmt .Println (args [0 ])
85- }
86-
87- if gpflag {
88- utils .GitPush ()
89- }
90- os .Exit (0 )
93+ message = args [0 ]
9194 }
9295 }
9396
94- // check if user included -m tag and remove. Wrap around for safety's sake
95- if args [0 ] == "-m" {
96- args = args [1 :]
97- goto wrap_around
98- }
99-
97+ skip:
10098 // builds the commit message with the selected authors
101- message = utils .Commit (args [0 ], args [1 :])
99+ if len (args ) > 1 {
100+ message = utils .Commit (args [0 ], args [1 :])
101+ }
102+
103+ if update {
104+ update_msg ()
105+ }
102106
103- tui:
104107 if tflag {
105108 fmt .Println (message )
106109 return
@@ -120,6 +123,9 @@ var rootCmd = &cobra.Command{
120123// Execute adds all child commands to the root command and sets flags appropriately.
121124// This is called by main.main(). It only needs to happen once to the rootCmd.
122125func Execute () {
126+ // check for update
127+ check_update ()
128+
123129 // author file check
124130 author_file := utils .CheckAuthorFile ()
125131 // define users
@@ -131,6 +137,45 @@ func Execute() {
131137 }
132138}
133139
140+ func call_tui (args []string ) []string {
141+ // append commit message to args
142+ args = append (args , tui .Entry_CM ())
143+
144+ // clear the screen
145+ screen .Clear ()
146+ screen .MoveTopLeft ()
147+
148+ // run the tui and append authors to args
149+ args = append (args , tui .Entry ()... )
150+ return args
151+ }
152+
153+ func update_msg () {
154+ fmt .Print (update_style .Render ("--* A new version of cocommit is available. Please update to the latest version *--" )+ "\n \n " )
155+ }
156+
157+ // function to check for updates (check tag version from repo with the current version)
158+ func check_update () {
159+ var tag github_release
160+ tags , err := http .Get ("https://api.github.com/repos/Slug-Boi/cocommit/releases/latest" )
161+ if err != nil {
162+ fmt .Println ("Could not fetch tags from github API" )
163+ return
164+ }
165+ defer tags .Body .Close ()
166+
167+ err = json .NewDecoder (tags .Body ).Decode (& tag )
168+ if err != nil {
169+ fmt .Println ("Error decoding json response from github API" )
170+ return
171+ }
172+
173+ // NOTE: maybe change to a split and parse method idk if this can cause issues
174+ if tag .TagName != Coco_Version && Coco_Version != "" {
175+ update = true
176+ }
177+ }
178+
134179func init () {
135180 //rootCmD := RootCmd()
136181 rootCmd .Flags ().BoolP ("print-output" , "o" , false , "Prints the commit message to the console" )
0 commit comments