Merge pull request #30 from Slug-Boi/feat_commitizen

feat: added commitizen support
This commit is contained in:
Theis
2024-10-31 22:02:42 +01:00
committed by GitHub
6 changed files with 149 additions and 49 deletions
+5 -7
View File
@@ -32,15 +32,13 @@ func teardown() {
os.Setenv("author_file", envVar) os.Setenv("author_file", envVar)
} }
//Skip cobra cmd tests on CI causes problems apparenly idk why // Skip cobra cmd tests on CI causes problems apparenly idk why
// test will be run locally before releasing a new version // test will be run locally before releasing a new version
func skipCI(t *testing.T) { func skipCI(t *testing.T) {
if os.Getenv("CI") != "" { if os.Getenv("CI") != "" {
t.Skip("Skipping testing in CI environment") t.Skip("Skipping testing in CI environment")
} }
} }
func StdoutReader() (chan string, *os.File, *os.File, *os.File) { func StdoutReader() (chan string, *os.File, *os.File, *os.File) {
old := os.Stdout old := os.Stdout
@@ -111,7 +109,7 @@ func Test_CommitCmd(t *testing.T) {
outC <- buf.String() outC <- buf.String()
}() }()
cmd := rootCmD cmd := rootCmd
cmd.SetArgs([]string{"-t", "Test commit message"}) cmd.SetArgs([]string{"-t", "Test commit message"})
cmd.Execute() cmd.Execute()
@@ -142,7 +140,7 @@ func Test_CommitCmdWithM(t *testing.T) {
outC <- buf.String() outC <- buf.String()
}() }()
cmd := rootCmD cmd := rootCmd
cmd.SetArgs([]string{"-m", "-t", "Test commit message"}) cmd.SetArgs([]string{"-m", "-t", "Test commit message"})
cmd.Execute() cmd.Execute()
@@ -157,6 +155,6 @@ func Test_CommitCmdWithM(t *testing.T) {
t.Errorf("Expected to find 'Test commit message' in output but got %s", outStr) t.Errorf("Expected to find 'Test commit message' in output but got %s", outStr)
} }
} }
// root CMD TEST END // root CMD TEST END
+60
View File
@@ -0,0 +1,60 @@
package cmd
import (
"fmt"
"github.com/Slug-Boi/cocommit/src_code/go_src/cmd/tui"
"github.com/Slug-Boi/cocommit/src_code/go_src/cmd/utils"
"github.com/inancgumus/screen"
"github.com/spf13/cobra"
)
// czCmd represents the cz command
var czCmd = &cobra.Command{
Use: "cz",
Short: "Allows for commitizen commit messages",
Long: `This command will allow the user to use commitizen to craft the commit message
after which the user will be able to add co-authors to the commit message. This will require
the user to have commitizen installed on their system.`,
Run: func(cmd *cobra.Command, args []string) {
var message string
var authors []string
// check if the print flag is set
pflag, _ := cmd.Flags().GetBool("print")
cflag, _ := cmd.Flags().GetBool("cli")
// run execute commands again as root run will not call this part
message = utils.Cz_Call()
if cflag {
// call the cli style syntax
authors = args
goto skip_tui
}
// for good measure clear the screen
screen.Clear()
screen.MoveTopLeft()
// call tui
authors = tui.Entry()
skip_tui:
// build the commit message
message = utils.Commit(message, authors)
// commit the message
utils.GitWrapper(message)
if pflag {
fmt.Println(message)
}
},
}
func init() {
rootCmd.AddCommand(czCmd)
czCmd.Flags().BoolP("print", "p", false, "Print the commit message")
czCmd.Flags().BoolP("cli", "c", false, "[co-author1] [co-author2] ...")
}
+6 -6
View File
@@ -13,7 +13,7 @@ import (
// rootCmd represents the base command when called without any subcommands // rootCmd represents the base command when called without any subcommands
// func RootCmd() *cobra.Command { // func RootCmd() *cobra.Command {
var rootCmD = &cobra.Command{ var rootCmd = &cobra.Command{
Use: `cocommit <commit message> <co-author1> [co-author2] ... || Use: `cocommit <commit message> <co-author1> [co-author2] ... ||
cocommit <commit message> <co-author1:email> [co-author2:email] ... || cocommit <commit message> <co-author1:email> [co-author2:email] ... ||
cocommit <commit message> all || cocommit <commit message> all ||
@@ -91,7 +91,7 @@ func Execute() {
// define users // define users
utils.Define_users(author_file) utils.Define_users(author_file)
err := rootCmD.Execute() err := rootCmd.Execute()
if err != nil { if err != nil {
os.Exit(1) os.Exit(1)
} }
@@ -99,8 +99,8 @@ func Execute() {
func init() { func init() {
//rootCmD := RootCmd() //rootCmD := RootCmd()
rootCmD.Flags().BoolP("print", "p", false, "Prints the commit message to the console") 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("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("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") rootCmd.Flags().BoolP("authors", "a", false, "Runs the author list TUI")
} }
@@ -85,7 +85,7 @@ func (m model_cm) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.KeyMsg: case tea.KeyMsg:
switch { switch {
case key.Matches(msg, m.keys.EndWithMes) && msg.Alt == true: case key.Matches(msg, m.keys.EndWithMes):
return m, tea.Quit return m, tea.Quit
case key.Matches(msg, m.keys.NewLine): case key.Matches(msg, m.keys.NewLine):
m.textarea.SetValue(m.textarea.Value() + "\n") m.textarea.SetValue(m.textarea.Value() + "\n")
+5 -4
View File
@@ -1,14 +1,15 @@
package cmd package cmd
import ( import (
"github.com/Slug-Boi/cocommit/src_code/go_src/cmd/tui"
"github.com/Slug-Boi/cocommit/src_code/go_src/cmd/utils"
"os" "os"
"os/exec" "os/exec"
"slices" "slices"
"sort" "sort"
"strings" "strings"
"github.com/Slug-Boi/cocommit/src_code/go_src/cmd/tui"
"github.com/Slug-Boi/cocommit/src_code/go_src/cmd/utils"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@@ -49,11 +50,11 @@ func UsersCmd() *cobra.Command {
bat.Stderr = os.Stderr bat.Stderr = os.Stderr
bat.Run() bat.Run()
}, },
} }
} }
func init() { func init() {
usersCmd := UsersCmd() usersCmd := UsersCmd()
rootCmD.AddCommand(usersCmd) rootCmd.AddCommand(usersCmd)
usersCmd.Flags().BoolP("np", "n", false, "No pretty print of the users") usersCmd.Flags().BoolP("np", "n", false, "No pretty print of the users")
} }
+41
View File
@@ -0,0 +1,41 @@
package utils
import (
"fmt"
"io"
"os"
"os/exec"
"strings"
)
func Cz_Call() string {
// create commitizen command
cmd := exec.Command("cz", "commit", "--dry-run", "--write-message-to-file", "msg")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
// if the user exits the commitizen command, exit the program
if strings.Contains(err.Error(), "exit status 8") {
os.Exit(0)
}
panic(fmt.Sprint(err))
}
file, err := os.OpenFile("msg", os.O_RDONLY, 0644)
defer os.Remove("msg")
defer file.Close()
if err != nil {
panic(fmt.Sprint(err))
}
msg, err := io.ReadAll(file)
if err != nil {
panic(fmt.Sprint(err))
}
return string(msg)
}