Skip to content

Commit 106957c

Browse files
committed
refactor: Fix terrible code and flag handling in root
WIP this will be passed down to the cz command most likely as well
1 parent 79dc578 commit 106957c

File tree

1 file changed

+51
-46
lines changed

1 file changed

+51
-46
lines changed

src/cmd/root.go

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/Slug-Boi/cocommit/src/cmd/tui"
1111
"github.com/Slug-Boi/cocommit/src/cmd/utils"
1212
"github.com/inancgumus/screen"
13+
"github.com/charmbracelet/lipgloss"
1314

1415
"github.com/spf13/cobra"
1516
)
@@ -18,6 +19,10 @@ import (
1819
var Coco_Version string
1920
var update bool
2021

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+
2126
// github_tag struct to hold the tag name from the github api response
2227

2328
// rootCmd represents the base command when called without any subcommands
@@ -38,6 +43,12 @@ var rootCmd = &cobra.Command{
3843
Run: func(cmd *cobra.Command, args []string) {
3944
var message string
4045

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+
4152
// check if the print flag is set
4253
pflag, _ := cmd.Flags().GetBool("print-output")
4354
tflag, _ := cmd.Flags().GetBool("test_print")
@@ -48,6 +59,9 @@ var rootCmd = &cobra.Command{
4859

4960
if vflag {
5061
fmt.Println("Cocommit version:", Coco_Version)
62+
if update {
63+
update_msg()
64+
}
5165
os.Exit(0)
5266
}
5367

@@ -58,69 +72,43 @@ var rootCmd = &cobra.Command{
5872
}
5973

6074
if aflag {
61-
tui.Entry()
62-
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
6384
}
6485
// run execute commands again as root run will not call this part
6586
// redundant check for now but will be useful later when we add tui
66-
wrap_around:
6787
switch len(args) {
6888
case 0:
6989
// launch the tui
70-
args = append(args, tui.Entry_CM())
71-
screen.Clear()
72-
screen.MoveTopLeft()
73-
sel_auth := tui.Entry()
74-
message = utils.Commit(args[0], sel_auth)
75-
if update {
76-
fmt.Print("--* A new version of cocommit is available. Please update to the latest version *-- \n\n")
77-
}
78-
if tflag {
79-
fmt.Println(message)
80-
return
81-
}
82-
goto tui
90+
args = call_tui(args)
8391
case 1:
8492
if len(args) == 1 {
85-
if update {
86-
fmt.Print("--* A new version of cocommit is available. Please update to the latest version *-- \n\n")
87-
}
88-
if tflag {
89-
fmt.Println(args[0])
90-
return
91-
}
92-
93-
utils.GitWrapper(args[0], git_flags)
94-
if pflag {
95-
fmt.Println(args[0])
96-
}
97-
98-
if gpflag {
99-
utils.GitPush()
100-
}
101-
os.Exit(0)
93+
message = args[0]
10294
}
10395
}
10496

105-
// check if user included -m tag and remove. Wrap around for safety's sake
106-
if args[0] == "-m" {
107-
args = args[1:]
108-
goto wrap_around
109-
}
110-
97+
skip:
11198
// builds the commit message with the selected authors
112-
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+
}
113106

114-
tui:
115107
if tflag {
116108
fmt.Println(message)
117109
return
118110
}
119111

120-
// if update {
121-
// fmt.Print("--* A new version of cocommit is available. Please update to the latest version *-- \n\n")
122-
// }
123-
124112
utils.GitWrapper(message, git_flags)
125113
// prints the commit message to the console if the print flag is set
126114
if pflag {
@@ -149,6 +137,23 @@ func Execute() {
149137
}
150138
}
151139

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+
152157
// function to check for updates (check tag version from repo with the current version)
153158
func check_update() {
154159
var tag github_release
@@ -166,7 +171,7 @@ func check_update() {
166171
}
167172

168173
// NOTE: maybe change to a split and parse method idk if this can cause issues
169-
if tag.TagName != Coco_Version {
174+
if tag.TagName != Coco_Version && Coco_Version != "" {
170175
update = true
171176
}
172177
}

0 commit comments

Comments
 (0)