refactor: Fix terrible code and flag handling in root

WIP this will be passed down to the cz command most likely as well
This commit is contained in:
Slug-Boi
2025-01-14 13:49:54 +01:00
parent 79dc578b56
commit 106957c290
+49 -44
View File
@@ -10,6 +10,7 @@ import (
"github.com/Slug-Boi/cocommit/src/cmd/tui" "github.com/Slug-Boi/cocommit/src/cmd/tui"
"github.com/Slug-Boi/cocommit/src/cmd/utils" "github.com/Slug-Boi/cocommit/src/cmd/utils"
"github.com/inancgumus/screen" "github.com/inancgumus/screen"
"github.com/charmbracelet/lipgloss"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@@ -18,6 +19,10 @@ import (
var Coco_Version string var Coco_Version string
var update bool var update bool
// print styling for the output for the CLI
var update_style = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("#1aff00"))
var msg_style = lipgloss.NewStyle().BorderStyle(lipgloss.RoundedBorder()).BorderForeground(lipgloss.Color("170"))
// github_tag struct to hold the tag name from the github api response // 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
@@ -38,6 +43,12 @@ var rootCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
var message string var message string
// check if user included -m tag and remove. Wrap around for safety's sake
if len(args) > 0 && args[0] == "-m" {
// maybe change to a walk in case it pops up later?
args = args[1:]
}
// check if the print flag is set // check if the print flag is set
pflag, _ := cmd.Flags().GetBool("print-output") pflag, _ := cmd.Flags().GetBool("print-output")
tflag, _ := cmd.Flags().GetBool("test_print") tflag, _ := cmd.Flags().GetBool("test_print")
@@ -48,6 +59,9 @@ var rootCmd = &cobra.Command{
if vflag { if vflag {
fmt.Println("Cocommit version:", Coco_Version) fmt.Println("Cocommit version:", Coco_Version)
if update {
update_msg()
}
os.Exit(0) os.Exit(0)
} }
@@ -58,69 +72,43 @@ var rootCmd = &cobra.Command{
} }
if aflag { if aflag {
tui.Entry() sel_auth := tui.Entry()
if len(args) == 0 {
if update {
update_msg()
}
os.Exit(0) os.Exit(0)
} }
args = append(args, sel_auth...)
goto skip
}
// run execute commands again as root run will not call this part // run execute commands again as root run will not call this part
// redundant check for now but will be useful later when we add tui // redundant check for now but will be useful later when we add tui
wrap_around:
switch len(args) { switch len(args) {
case 0: case 0:
// launch the tui // launch the tui
args = append(args, tui.Entry_CM()) args = call_tui(args)
screen.Clear()
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: case 1:
if len(args) == 1 { if len(args) == 1 {
if update { message = args[0]
fmt.Print("--* A new version of cocommit is available. Please update to the latest version *-- \n\n")
}
if tflag {
fmt.Println(args[0])
return
}
utils.GitWrapper(args[0], git_flags)
if pflag {
fmt.Println(args[0])
}
if gpflag {
utils.GitPush()
}
os.Exit(0)
} }
} }
// check if user included -m tag and remove. Wrap around for safety's sake skip:
if args[0] == "-m" {
args = args[1:]
goto wrap_around
}
// builds the commit message with the selected authors // builds the commit message with the selected authors
if len(args) > 1 {
message = utils.Commit(args[0], args[1:]) message = utils.Commit(args[0], args[1:])
}
if update {
update_msg()
}
tui:
if tflag { if tflag {
fmt.Println(message) fmt.Println(message)
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 {
@@ -149,6 +137,23 @@ func Execute() {
} }
} }
func call_tui(args []string) []string {
// append commit message to args
args = append(args, tui.Entry_CM())
// clear the screen
screen.Clear()
screen.MoveTopLeft()
// run the tui and append authors to args
args = append(args, tui.Entry()...)
return args
}
func update_msg() {
fmt.Print(update_style.Render("--* A new version of cocommit is available. Please update to the latest version *--")+"\n\n")
}
// function to check for updates (check tag version from repo with the current version) // function to check for updates (check tag version from repo with the current version)
func check_update() { func check_update() {
var tag github_release var tag github_release
@@ -166,7 +171,7 @@ func check_update() {
} }
// NOTE: maybe change to a split and parse method idk if this can cause issues // NOTE: maybe change to a split and parse method idk if this can cause issues
if tag.TagName != Coco_Version { if tag.TagName != Coco_Version && Coco_Version != "" {
update = true update = true
} }
} }