mirror of
https://github.com/Slug-Boi/cocommit.git
synced 2026-05-13 12:45:47 +00:00
Merge pull request #32 from Slug-Boi/chore_src_folder
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
# Custom
|
||||
dist
|
||||
author_file
|
||||
|
||||
go.work
|
||||
go.sum
|
||||
|
||||
@@ -25,20 +25,20 @@ func main() {
|
||||
// at /src in the container
|
||||
source := client.Container().
|
||||
From("golang:1.23").
|
||||
WithDirectory("/src", client.Host().Directory(".", dagger.HostDirectoryOpts{
|
||||
WithDirectory("/src_d", client.Host().Directory(".", dagger.HostDirectoryOpts{
|
||||
Exclude: []string{},
|
||||
})).WithMountedCache("/src/dagger_dep_cache/go_dep", goCache)
|
||||
})).WithMountedCache("/src_d/dagger_dep_cache/go_dep", goCache)
|
||||
|
||||
geese := []string{"darwin", "linux", "windows"}
|
||||
goarch := "amd64"
|
||||
|
||||
// set the working directory in the container
|
||||
// install application dependencies
|
||||
runner := source.WithWorkdir("/src/src_code/go_src/").
|
||||
runner := source.WithWorkdir("/src_d/src/").
|
||||
WithExec([]string{"go", "mod", "tidy"})
|
||||
|
||||
// run application tests
|
||||
test := runner.WithWorkdir("/src/src_code/go_src").WithExec([]string{"go", "test", "./..."}).WithEnvVariable("CI", "true")
|
||||
test := runner.WithWorkdir("/src_d/src/").WithExec([]string{"go", "test", "./..."}).WithEnvVariable("CI", "true")
|
||||
|
||||
buildDir := test.Directory("/src/")
|
||||
|
||||
|
||||
+4
-4
@@ -25,17 +25,17 @@ func main() {
|
||||
// at /src in the container
|
||||
source := client.Container().
|
||||
From("golang:1.23").
|
||||
WithDirectory("/src", client.Host().Directory(".", dagger.HostDirectoryOpts{
|
||||
WithDirectory("/src_d", client.Host().Directory(".", dagger.HostDirectoryOpts{
|
||||
Exclude: []string{"build/"},
|
||||
})).WithMountedCache("/src/dagger_dep_cache/go_dep", goCache)
|
||||
})).WithMountedCache("/src_d/dagger_dep_cache/go_dep", goCache)
|
||||
|
||||
// set the working directory in the container
|
||||
// install application dependencies
|
||||
runner := source.WithWorkdir("/src/src_code/go_src/").
|
||||
runner := source.WithWorkdir("/src_d/src").
|
||||
WithExec([]string{"go", "mod", "tidy"}).WithEnvVariable("CI", "true")
|
||||
|
||||
// run application tests
|
||||
out, err := runner.WithWorkdir("/src/src_code/go_src/").WithExec([]string{"go", "test", "./..."}).
|
||||
out, err := runner.WithWorkdir("/src_d/src").WithExec([]string{"go", "test", "./..."}).
|
||||
Stderr(ctx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Slug-Boi/cocommit/src_code/go_src/cmd/utils"
|
||||
"github.com/Slug-Boi/cocommit/src/cmd/utils"
|
||||
)
|
||||
|
||||
const author_data = `syntax for the test file
|
||||
@@ -3,8 +3,8 @@ 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/Slug-Boi/cocommit/src/cmd/tui"
|
||||
"github.com/Slug-Boi/cocommit/src/cmd/utils"
|
||||
"github.com/inancgumus/screen"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
@@ -4,8 +4,8 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/Slug-Boi/cocommit/src_code/go_src/cmd/tui"
|
||||
"github.com/Slug-Boi/cocommit/src_code/go_src/cmd/utils"
|
||||
"github.com/Slug-Boi/cocommit/src/cmd/tui"
|
||||
"github.com/Slug-Boi/cocommit/src/cmd/utils"
|
||||
|
||||
"github.com/inancgumus/screen"
|
||||
"github.com/spf13/cobra"
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/Slug-Boi/cocommit/src_code/go_src/cmd/utils"
|
||||
"github.com/Slug-Boi/cocommit/src/cmd/utils"
|
||||
|
||||
"github.com/charmbracelet/bubbles/textinput"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
@@ -2,7 +2,7 @@ package tui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Slug-Boi/cocommit/src_code/go_src/cmd/utils"
|
||||
"github.com/Slug-Boi/cocommit/src/cmd/utils"
|
||||
"io"
|
||||
"os"
|
||||
"sort"
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/Slug-Boi/cocommit/src_code/go_src/cmd/utils"
|
||||
"github.com/Slug-Boi/cocommit/src/cmd/utils"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/x/exp/teatest"
|
||||
)
|
||||
@@ -7,8 +7,8 @@ import (
|
||||
"sort"
|
||||
"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/Slug-Boi/cocommit/src/cmd/tui"
|
||||
"github.com/Slug-Boi/cocommit/src/cmd/utils"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
@@ -1,7 +1,7 @@
|
||||
package utils_test
|
||||
|
||||
import (
|
||||
"github.com/Slug-Boi/cocommit/src_code/go_src/cmd/utils"
|
||||
"github.com/Slug-Boi/cocommit/src/cmd/utils"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
@@ -4,7 +4,7 @@ Copyright © 2024 NAME HERE <EMAIL ADDRESS>
|
||||
*/
|
||||
package main
|
||||
|
||||
import "github.com/Slug-Boi/cocommit/src_code/go_src/cmd"
|
||||
import "github.com/Slug-Boi/cocommit/src/cmd"
|
||||
|
||||
func main() {
|
||||
cmd.Execute()
|
||||
@@ -1,4 +0,0 @@
|
||||
Syntax for authorfile
|
||||
test|test|test|test|ex;;test
|
||||
tet|tessadsat|teta|asdadad|ex;;adsadas
|
||||
teadsajdma|asdasdasda|adsdadasd|addsadasd;;adsadsadadas
|
||||
@@ -1,309 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type user struct {
|
||||
username string
|
||||
email string
|
||||
names string
|
||||
}
|
||||
//TODO: Remove later once everything is up and running with the new version
|
||||
|
||||
// Map of all th users in the author file
|
||||
var users = make(map[string]user)
|
||||
|
||||
// String builder for building the commit message
|
||||
var sb strings.Builder
|
||||
|
||||
// Flag that can be toggled to include all users in a commit message (excluding defExclude)
|
||||
var all_flag = false
|
||||
|
||||
// DefaultExclude -> A list that contains users marked with ex meaning
|
||||
// they should not be included in all and negations
|
||||
var defExclude = []string{}
|
||||
|
||||
// Group map for adding people as a group
|
||||
var groups = make(map[string][]user)
|
||||
|
||||
func main() {
|
||||
|
||||
// Reads a shell env variable :: author_file
|
||||
var authors string
|
||||
envVar := os.Getenv("author_file")
|
||||
if envVar == "" {
|
||||
var err error
|
||||
authors, err = os.UserConfigDir()
|
||||
authors += "/cocommit/authors"
|
||||
if err != nil {
|
||||
println("Error: ", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
} else {
|
||||
authors = envVar
|
||||
}
|
||||
|
||||
file, err := os.Open(authors)
|
||||
if err != nil {
|
||||
authors, _ = os.UserConfigDir()
|
||||
authors += "/cocommit/authors"
|
||||
println("Authors file cannot be found. Please check the path to the file. \nEither set the author_file env variable or place the file in the default location. \nDefault location: " + authors)
|
||||
println("If you want to create a blank template file at the default location type y|yes or cancel with n|no")
|
||||
var input string
|
||||
fmt.Scanln(&input)
|
||||
if input == "y" || input == "yes" {
|
||||
create_author_file("yes")
|
||||
os.Exit(1)
|
||||
} else {
|
||||
println("Cancelled")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
|
||||
// eat a single input
|
||||
scanner.Scan()
|
||||
|
||||
// reads the input of authors file and formats accordingly
|
||||
for scanner.Scan() {
|
||||
input_str := scanner.Text()
|
||||
group_info := []string{}
|
||||
if strings.Contains(input_str, ";;") {
|
||||
input := strings.Split(input_str, ";;")
|
||||
input_str = input[0]
|
||||
group_info = append(group_info, strings.Split(input[1], "|")...)
|
||||
}
|
||||
info := strings.Split(input_str, "|")
|
||||
usr := user{username: info[2], email: info[3], names: info[0] + "/" + info[1]}
|
||||
users[info[0]] = usr
|
||||
users[info[1]] = usr
|
||||
// Adds users with the ex tag to the defExclude list
|
||||
if len(info) == 5 {
|
||||
if info[4] == "ex" {
|
||||
defExclude = append(defExclude, info[2])
|
||||
}
|
||||
} else if len(group_info) > 0 {
|
||||
// Group assignment
|
||||
for _, group := range group_info {
|
||||
if groups[group] == nil {
|
||||
groups[group] = []user{usr}
|
||||
} else {
|
||||
//TODO: Try and find a cleaner way of doing this
|
||||
usr_lst := groups[group]
|
||||
usr_lst = append(usr_lst, usr)
|
||||
groups[group] = usr_lst
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
check_err(scanner.Err())
|
||||
// Removes the call command for the program
|
||||
args := os.Args[1:]
|
||||
|
||||
// Checks if the user called the program with any inputs or with non commit args
|
||||
NoInput(args, users)
|
||||
|
||||
// This list is used when doing negations and for removing duplicate users during string building
|
||||
excludeMode := []string{}
|
||||
|
||||
// builds the commit message with the selected authors
|
||||
sb.WriteString(string(args[0]) + "\n")
|
||||
|
||||
// Regex that catches one off authors
|
||||
reg, _ := regexp.Compile("([^:]+):([^:]+)")
|
||||
|
||||
if args[1] == "all" || args[1] == "All" {
|
||||
all_flag = true
|
||||
goto skip_loop
|
||||
} else if groups[args[1]] != nil {
|
||||
// Selects everybody that isn't the group members and adds them to the defExclude
|
||||
excludeMode = group_selection(groups[args[1]], excludeMode)
|
||||
goto skip_loop
|
||||
}
|
||||
|
||||
// Loop that adds users
|
||||
for _, committer := range args[1:] {
|
||||
if _, ok := users[committer]; ok {
|
||||
sb_author(committer)
|
||||
} else if match := reg.MatchString(committer); match {
|
||||
str := strings.Split(committer, ":")
|
||||
|
||||
sb.WriteString("\nCo-authored-by: ")
|
||||
sb.WriteString(str[0])
|
||||
sb.WriteString(" <")
|
||||
sb.WriteString(str[1])
|
||||
sb.WriteRune('>')
|
||||
|
||||
} else if committer[0] == '^' { // Negations
|
||||
excludeMode = append(excludeMode, users[committer[1:]].username)
|
||||
|
||||
} else {
|
||||
println(committer, " was unknown. User either not defined or name typed wrong")
|
||||
}
|
||||
}
|
||||
|
||||
// Skip label for adding all
|
||||
skip_loop:
|
||||
|
||||
if len(excludeMode) > 0 || all_flag {
|
||||
// adds all users not in the excludeMode list
|
||||
add_x_users(excludeMode)
|
||||
}
|
||||
|
||||
// commit msg built
|
||||
commit := sb_build()
|
||||
|
||||
print(commit)
|
||||
|
||||
//NOTE: Uncomment for testing
|
||||
//print(commit)
|
||||
|
||||
// commit shell command
|
||||
cmd := exec.Command("git", "commit", "-m", commit)
|
||||
|
||||
// https://stackoverflow.com/questions/18159704/how-to-debug-exit-status-1-error-when-running-exec-command-in-golang
|
||||
|
||||
cmd_output, err := cmd.CombinedOutput()
|
||||
|
||||
if err != nil {
|
||||
println(fmt.Sprint(err) + " : " + string(cmd_output))
|
||||
} else {
|
||||
println(string(cmd_output))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func group_selection(group []user, excludeMode []string) []string {
|
||||
for _, user := range users {
|
||||
if !(slices.Contains(group, user)) {
|
||||
excludeMode = append(excludeMode, user.username)
|
||||
}
|
||||
}
|
||||
|
||||
return excludeMode
|
||||
}
|
||||
|
||||
func add_x_users(excludeMode []string) {
|
||||
if len(defExclude) > 0 {
|
||||
excludeMode = append(excludeMode, defExclude...)
|
||||
}
|
||||
for key, user := range users {
|
||||
if !slices.Contains(excludeMode, user.username) {
|
||||
sb_author(key)
|
||||
excludeMode = append(excludeMode, user.username)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func sb_build() string {
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func sb_author(committer string) {
|
||||
sb.WriteString("\nCo-authored-by: ")
|
||||
sb.WriteString(users[committer].username)
|
||||
sb.WriteString(" <")
|
||||
sb.WriteString(users[committer].email)
|
||||
sb.WriteRune('>')
|
||||
}
|
||||
|
||||
// TODO: move half this into another function and call before building users to improve performance
|
||||
func NoInput(args []string, users map[string]user) {
|
||||
if len(args) < 2 {
|
||||
// If you call binary with users prints users
|
||||
if len(args) == 1 && args[0] == "users" {
|
||||
println("List of users:\nFormat: <shortname>/<name> -> Username: <username> Email: <email>")
|
||||
seen_users := []user{}
|
||||
user_sb := []string{}
|
||||
for name, usr := range users {
|
||||
if !slices.Contains(seen_users, usr) {
|
||||
user_sb = append(user_sb, users[name].names+" ->"+" Username: "+usr.username+" Email: "+usr.email+"\n")
|
||||
seen_users = append(seen_users, usr)
|
||||
}
|
||||
}
|
||||
sort.Strings(user_sb)
|
||||
println(strings.Join(user_sb, ""))
|
||||
os.Exit(1)
|
||||
} else if len(args) == 1 && args[0] == "config" {
|
||||
create_author_file()
|
||||
}
|
||||
// if calling binary with nothing or only string
|
||||
command_options := []string{
|
||||
"cocommit <commit message> <co-author1> [co-author2] [co-author3]",
|
||||
"cocommit <commit message> <co-author1:email> [co-author2:email] [co-author3:email]",
|
||||
"cocommit <commit message> all",
|
||||
"cocommit <commit message> ^<co-author1> ^[co-author2]",
|
||||
"cocommit <commit message> <group>",
|
||||
"cocommit users",
|
||||
}
|
||||
println("Usage:")
|
||||
for _, v := range command_options {
|
||||
print(v)
|
||||
println(" ||")
|
||||
}
|
||||
println("Mixes of both")
|
||||
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func create_author_file(param ...string) {
|
||||
var input string
|
||||
authors, err := os.UserConfigDir()
|
||||
|
||||
if err != nil {
|
||||
println("Error: ", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if len(param) > 0 {
|
||||
input = "yes"
|
||||
goto skip
|
||||
}
|
||||
println("This command will create a blank template auhtor file in the default location. \nDefault location: " + authors + "\nConfirm by typing y|yes or cancel with n|no")
|
||||
fmt.Scanln(&input)
|
||||
if err != nil {
|
||||
println("Error: ", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
skip:
|
||||
if input == "y" || input == "yes" {
|
||||
// create folder cocommit in .config
|
||||
authors += "/cocommit"
|
||||
err := os.MkdirAll(authors, 0755)
|
||||
if err != nil {
|
||||
println("Error in dir creation: ", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
authors += "/authors"
|
||||
file, err := os.Create(authors)
|
||||
if err != nil {
|
||||
println("Error: ", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
defer file.Close()
|
||||
file.WriteString("name_short|Name|Username|email (opt: |ex) (opt: ;;group1 or ;;group1|group2|group3...)\n")
|
||||
println("File created successfully at: " + authors)
|
||||
os.Exit(1)
|
||||
} else {
|
||||
println("Cancelled")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func check_err(e error) {
|
||||
if e != nil {
|
||||
fmt.Println(e.Error())
|
||||
os.Exit(2)
|
||||
}
|
||||
}
|
||||
@@ -1,126 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_emptyInput(t *testing.T) {
|
||||
authors := make(map[string]user)
|
||||
authors["test"] = user{username: "test", email: "test"}
|
||||
if os.Getenv("BE_CRASHER") == "1" {
|
||||
NoInput([]string{}, authors)
|
||||
return
|
||||
}
|
||||
cmd := exec.Command(os.Args[0], "-test.run=Test_emptyInput")
|
||||
cmd.Env = append(os.Environ(), "BE_CRASHER=1")
|
||||
err := cmd.Run()
|
||||
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
|
||||
return
|
||||
}
|
||||
t.Fatalf("process ran with err %v, want exit status 1", err)
|
||||
}
|
||||
|
||||
func Test_usersInput(t *testing.T) {
|
||||
authors := make(map[string]user)
|
||||
authors["test"] = user{username: "test", email: "test"}
|
||||
if os.Getenv("BE_CRASHER") == "1" {
|
||||
NoInput([]string{"users"}, authors)
|
||||
return
|
||||
}
|
||||
cmd := exec.Command(os.Args[0], "-test.run=Test_usersInput")
|
||||
cmd.Env = append(os.Environ(), "BE_CRASHER=1")
|
||||
err := cmd.Run()
|
||||
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
|
||||
return
|
||||
}
|
||||
t.Fatalf("process ran with err %v, want exit status 1", err)
|
||||
}
|
||||
//TODO: Turn this into a fuzz test
|
||||
|
||||
func Test_commit_message(t *testing.T) {
|
||||
//authors := make(map[string]user)
|
||||
users["test"] = user{username: "test", email: "test"}
|
||||
sb_author("test")
|
||||
commit := sb_build()
|
||||
if commit != "\nCo-authored-by: test <test>" {
|
||||
t.Fatalf("String built incorrectly. Strings did not match: Created -> %s Expected -> Co-authored-by: test <test>",commit)
|
||||
}
|
||||
}
|
||||
//TODO: Turn this into a fuzz test
|
||||
func Test_add_all(t *testing.T) {
|
||||
for k := range users {
|
||||
delete(users, k)
|
||||
}
|
||||
sb.Reset()
|
||||
users["test1"] = user{username: "test1", email: "test1"}
|
||||
users["test2"] = user{username: "test2", email: "test2"}
|
||||
users["test3"] = user{username: "test3", email: "test3"}
|
||||
all_flag = true
|
||||
add_x_users([]string{})
|
||||
|
||||
commit := sb_build()
|
||||
if !strings.Contains(commit, "\nCo-authored-by: test1 <test1>") ||
|
||||
!strings.Contains(commit, "\nCo-authored-by: test2 <test2>") ||
|
||||
!strings.Contains(commit, "\nCo-authored-by: test3 <test3>") {
|
||||
t.Fatalf("String built incorrectly. Strings did not match: Created -> %s Expected -> Co-authored-by: test1 <test1>\nCo-authored-by: test2 <test2>\n\nCo-authored-by: test3 <test3>",commit)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_exclude_user(t *testing.T) {
|
||||
// Reusing users map from last test
|
||||
excludeMode := []string{"test1"}
|
||||
|
||||
sb.Reset()
|
||||
|
||||
add_x_users(excludeMode)
|
||||
|
||||
commit := sb_build()
|
||||
if strings.Contains(commit, "\nCo-authored-by: test1 <test1>") {
|
||||
t.Fatalf("String built incorrectly. Strings did not match: Created -> %s Expected -> Co-authored-by: test2 <test2>\n\nCo-authored-by: test3 <test3>",commit)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_exclude_by_default(t *testing.T) {
|
||||
// Reusing users from before
|
||||
defExclude = append(defExclude, users["test1"].username)
|
||||
|
||||
sb.Reset()
|
||||
|
||||
add_x_users([]string{})
|
||||
|
||||
commit := sb_build()
|
||||
|
||||
if strings.Contains(commit, "\nCo-authored-by: test1 <test1>") {
|
||||
t.Fatalf("String built incorrectly. Strings did not match: Created -> %s Expected -> Co-authored-by: test2 <test2>\n\nCo-authored-by: test3 <test3>",commit)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_commit_with_grouping(t *testing.T) {
|
||||
for k := range groups {
|
||||
delete(groups, k)
|
||||
}
|
||||
|
||||
defExclude = []string{}
|
||||
|
||||
groups["test1"] = []user{users["test1"]}
|
||||
|
||||
excludeMode := group_selection(groups["test1"], []string{})
|
||||
|
||||
sb.Reset()
|
||||
|
||||
add_x_users(excludeMode)
|
||||
|
||||
commit := sb_build()
|
||||
|
||||
if commit != "\nCo-authored-by: test1 <test1>" {
|
||||
t.Fatalf("String built incorrectly. Strings did not match: Created -> %s Expected -> Co-authored-by: test <test>",commit)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user