refactor: update check author file function and create new flag to enter author list tui mode

This commit is contained in:
Slug-Boi
2024-10-28 19:29:03 +01:00
parent c4ac188918
commit ff9b648c59
4 changed files with 40 additions and 9 deletions
+1 -1
View File
@@ -1 +1 @@
Syntax: name_short|Name|Username|email (opt: |ex) (opt: ;;group1|group2|group3...) !!Remember to set the env var authors_file!!
Syntax: name_short|Name|Username|email (opt: |ex) (opt: ;;group1|group2|group3...)
+7
View File
@@ -31,6 +31,12 @@ var rootCmD = &cobra.Command{
// check if the print flag is set
pflag, _ := cmd.Flags().GetBool("print")
tflag, _ := cmd.Flags().GetBool("test_print")
aflag, _ := cmd.Flags().GetBool("authors")
if aflag {
tui.Entry()
os.Exit(0)
}
// 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
wrap_around:
@@ -96,4 +102,5 @@ func init() {
rootCmD.Flags().BoolP("print", "p", false, "Prints the commit message to the console")
rootCmD.Flags().BoolP("test_print", "t", false, "Prints the commit message to the console without running the git commit command")
rootCmD.Flags().BoolP("message", "m", false, "Does nothing but allows for -m to be used in the command")
rootCmD.Flags().BoolP("authors", "a", false, "Runs the author list TUI")
}
+2 -1
View File
@@ -5,10 +5,11 @@ package tui
import (
"fmt"
"github.com/Slug-Boi/cocommit/src_code/go_src/cmd/utils"
"os"
"strings"
"github.com/Slug-Boi/cocommit/src_code/go_src/cmd/utils"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
+28 -5
View File
@@ -36,8 +36,31 @@ func CheckAuthorFile() string {
println("Error reading response")
}
if response == "y" {
//TODO: Tui response to create author file
//createAuthorFile(authorfile)
if authorfile == "" {
fmt.Println("author_file environment variable not set using default location:")
config, err := os.UserConfigDir()
if err != nil {
fmt.Println("Error getting user config directory")
os.Exit(1)
}
authorfile = config + "/cocommit/authors"
fmt.Println(authorfile)
}
// create the author file
file, err := os.Create(authorfile)
if err != nil {
fmt.Println("Error creating file: ", err)
os.Exit(1)
}
defer file.Close()
// write the header to the file
file.WriteString("Syntax: name_short|Name|Username|email (opt: |ex) (opt: ;;group1|group2|group3...)\n")
fmt.Println("Author file created. To add authors please launch the TUI with -a and press 'C'")
} else {
os.Exit(1)
}
@@ -59,7 +82,7 @@ func DeleteOneAuthor(author string) {
defer file.Close()
// create regex to capture author line
regexp, err := regexp.Compile(fmt.Sprintf("^(.+\\|%s\\|.+|%s\\|.+\\|.+)$",author,author))
regexp, err := regexp.Compile(fmt.Sprintf("^(.+\\|%s\\|.+|%s\\|.+\\|.+)$", author, author))
if err != nil {
fmt.Println("Error compiling regex: ", err)
return
@@ -85,10 +108,10 @@ func DeleteOneAuthor(author string) {
}
// remove the last newline character
buf.Truncate(buf.Len()-1)
buf.Truncate(buf.Len() - 1)
file.Truncate(0)
file.Seek(0,0)
file.Seek(0, 0)
buf.WriteTo(file)
RemoveUser(author)