Skip to content

Commit b4be9fb

Browse files
authored
Merge pull request #59 from Slug-Boi/feat_update_check
2 parents a15067a + 9c98cd1 commit b4be9fb

File tree

6 files changed

+135
-54
lines changed

6 files changed

+135
-54
lines changed

src/cmd/cz.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ This will require the user to have commitizen installed on their system.`,
5555
}
5656
utils.GitWrapper(message, git_flags)
5757

58+
if update {
59+
update_msg()
60+
}
61+
5862
if pflag {
5963
fmt.Println(message)
6064
}

src/cmd/root.go

Lines changed: 80 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
package cmd
22

33
import (
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
1619
var 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.
122125
func 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+
134179
func init() {
135180
//rootCmD := RootCmd()
136181
rootCmd.Flags().BoolP("print-output", "o", false, "Prints the commit message to the console")

src/cmd/update.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ var updateCmd = &cobra.Command{
3030
Long: `This command will try to update the cocommit cli tool by either running the update script or by running the go get Command if the -g flag is set.`,
3131
Run: func(cmd *cobra.Command, args []string) {
3232
gflag, _ := cmd.Flags().GetBool("go-get")
33+
cflag, _ := cmd.Flags().GetBool("check")
34+
35+
if cflag {
36+
fmt.Println("Checking if Cocommit is up to date")
37+
if update {
38+
update_msg()
39+
} else {
40+
fmt.Println("Cocommit is up to date")
41+
}
42+
os.Exit(0)
43+
}
3344

3445
// check version of the cli tool
3546
Github, err := http.Get("https://api.github.com/repos/Slug-Boi/cocommit/releases/latest")
@@ -137,12 +148,12 @@ func updateScript() {
137148
}
138149
err = unzipper("./", r)
139150
if err != nil {
140-
fmt.Println("Error unzipping file")
151+
panic("Error unzipping file - " + err.Error())
141152
}
142153

143154
swapper(exec_path)
144155

145-
fmt.Println("Cocommit cli tool updated successfully")
156+
fmt.Println(update_style.Render("Cocommit cli tool updated successfully"))
146157
}
147158

148159
func swapper(exec_path string) {
@@ -203,10 +214,18 @@ func unzipper(dst string, r io.Reader) error {
203214
// the target location where the dir/file should be created
204215
target := filepath.Join(dst, header.Name)
205216

206-
// ensure the target path is within the destination directory
207-
if !strings.HasPrefix(target, filepath.Clean(dst)+string(os.PathSeparator)) {
208-
return fmt.Errorf("illegal file path: %s", target)
209-
}
217+
// ensure the target path is within the destination directory
218+
cleanTarget, err := filepath.Abs(target)
219+
if err != nil {
220+
return fmt.Errorf("failed to get absolute path: %v", err)
221+
}
222+
cleanDst, err := filepath.Abs(dst)
223+
if err != nil {
224+
return fmt.Errorf("failed to get absolute path: %v", err)
225+
}
226+
if !strings.HasPrefix(cleanTarget, cleanDst+string(os.PathSeparator)) {
227+
return fmt.Errorf("illegal file path: %s\nExpected: %s", cleanTarget, cleanDst+string(os.PathSeparator))
228+
}
210229

211230
// check the file type
212231
switch header.Typeflag {
@@ -241,4 +260,5 @@ func unzipper(dst string, r io.Reader) error {
241260
func init() {
242261
rootCmd.AddCommand(updateCmd)
243262
updateCmd.Flags().BoolP("go-get", "g", false, "Use the go get command to update the cocommit cli tool")
263+
updateCmd.Flags().BoolP("check", "c", false, "Check if the cocommit cli tool is up to date")
244264
}

src/cmd/users.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ func UsersCmd() *cobra.Command {
2222
Short: "Displays all users from the author file located at:\n" + authorfile,
2323
Long: `Displays all users from the author file located at:` + "\n" + authorfile,
2424
Run: func(cmd *cobra.Command, args []string) {
25+
if update {
26+
update_msg()
27+
}
28+
2529
//TODO: make this print a bit prettier (sort it and maybe use a table)
2630
// check if the no pretty print flag is set
2731
np, _ := cmd.Flags().GetBool("np")

src/cmd/utils/author_file_utils.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os"
88
"regexp"
9+
"strings"
910
)
1011

1112
// Author file utils is a package that contains functions that are used to read
@@ -26,6 +27,7 @@ func Find_authorfile() string {
2627
}
2728

2829
func CheckAuthorFile() string {
30+
var cocommit_folder string
2931
authorfile := Find_authorfile()
3032
if _, err := os.Stat(authorfile); os.IsNotExist(err) {
3133
println("Author file not found at: ", authorfile)
@@ -36,18 +38,15 @@ func CheckAuthorFile() string {
3638
println("Error reading response")
3739
}
3840
if response == "y" {
39-
if authorfile == "" {
40-
fmt.Println("author_file environment variable not set using default location:")
41-
config, err := os.UserConfigDir()
42-
if err != nil {
43-
fmt.Println("Error getting user config directory")
44-
os.Exit(1)
45-
}
46-
authorfile = config + "/cocommit/authors"
47-
fmt.Println(authorfile)
48-
}
41+
parts := strings.Split(authorfile, "/")
42+
cocommit_folder = strings.Join(parts[:len(parts)-1], "/")
4943

5044
// create the author file
45+
err := os.Mkdir(cocommit_folder, 0766)
46+
if err != nil {
47+
fmt.Println("Error creating directory: ", err, cocommit_folder)
48+
os.Exit(1)
49+
}
5150
file, err := os.Create(authorfile)
5251
if err != nil {
5352
fmt.Println("Error creating file: ", err)

src/cmd/utils/user_util.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package utils
22

33
import (
44
"bufio"
5+
"fmt"
56
"os"
67
"strings"
78
)
@@ -47,11 +48,19 @@ func Define_users(author_file string) {
4748
info := strings.Split(input_str, "|")
4849
if len(info) < 4 {
4950
if len(info) > 0 {
50-
println("Error: User ", info[0], " is missing information")
51+
if info[0] == "" {
52+
info[0] = "(empty string)"
53+
}
54+
fmt.Println("Error: User", info[0], "is missing information")
55+
} else {
56+
fmt.Println("Error: Some user is missing information")
57+
}
58+
fmt.Println("Please check the author file for proper syntax")
59+
if input_str == "" {
60+
fmt.Println("empty line found in author file")
5161
} else {
52-
println("Error: Some user is missing information")
62+
fmt.Println("author file input:", input_str)
5363
}
54-
println("Please check the author file for proper syntax")
5564
os.Exit(1)
5665
}
5766
usr := User{Username: info[2], Email: info[3], Names: info[0] + "/" + info[1]}

0 commit comments

Comments
 (0)