feat: redo using tui and cli input modes. Also add clearer feedback to import

TODO: create tests
This commit is contained in:
Slug-Boi
2026-04-28 14:59:33 +02:00
parent 329304c240
commit d748020b33
4 changed files with 88 additions and 21 deletions
+22 -9
View File
@@ -2,10 +2,8 @@ package cmd
import ( import (
"fmt" "fmt"
"maps"
"os" "os"
"os/exec" "os/exec"
"slices"
"sort" "sort"
"strings" "strings"
@@ -33,13 +31,19 @@ func UsersCmd() *cobra.Command {
s, _ := cmd.Flags().GetBool("share") s, _ := cmd.Flags().GetBool("share")
if s && len(args) == 0 { if s && len(args) == 0 {
encoded := utils.SerealizeUsers(slices.Collect(maps.Values(utils.Users))) args = append(args, tui.Entry()...)
if len(args) == 0 {
fmt.Println("\033[31mNo authors selected exiting\033[31m")
os.Exit(0)
}
encoded := utils.SerealizeUsers(args)
fmt.Print(encoded) fmt.Print(encoded)
os.Exit(0) os.Exit(0)
} else if s && len(args) >= 1 { } else if s && len(args) >= 1 {
var users []utils.User users := utils.CLIAuthorInput(args)
for _, name := range args { if len(users) == 0 {
users = append(users, utils.Users[name]) fmt.Println("\033[31mNo authors selected exiting\033[31m")
os.Exit(0)
} }
encoded := utils.SerealizeUsers(users) encoded := utils.SerealizeUsers(users)
fmt.Print(encoded) fmt.Print(encoded)
@@ -48,16 +52,25 @@ func UsersCmd() *cobra.Command {
i, _ := cmd.Flags().GetBool("import") i, _ := cmd.Flags().GetBool("import")
if i && len(args) == 1 { if i && len(args) == 1 {
added_users := utils.UnserealizeUsers(args[0]) added_users, not_added := utils.UnserealizeUsers(args[0])
if len(added_users) == 0 { if len(added_users) == 0 {
fmt.Println("\033[33mNo authors added (authors probably already existed or corrupted \"share code\")\033[0m") fmt.Println("\033[33mNo authors added (authors probably already existed or corrupted \"share code\")\033[0m")
os.Exit(0)
} }
fmt.Println("\033[32mAuthors added:\033[0m") if len(added_users) != 0 {
fmt.Println("\033[32mAuthors added:\033[0m")
}
for _, usr := range added_users { for _, usr := range added_users {
fmt.Println("\033[32m+\033[0m ", usr) fmt.Println("\033[32m+\033[0m ", usr)
} }
if len(not_added) != 0 {
fmt.Println("\033[33mAlready existing authors (not added):\033[0m")
}
for _, usr := range not_added {
fmt.Println("\033[33m~\033[0m ", usr)
}
os.Exit(0) os.Exit(0)
} else { } else {
fmt.Println("\033[33mNo \"share code\", please run the flag with a valid \"share code\"\033[0m") fmt.Println("\033[33mNo \"share code\", please run the flag with a valid \"share code\"\033[0m")
+6 -3
View File
@@ -136,12 +136,13 @@ func CreateAuthor(user User) {
Define_users(Find_authorfile()) Define_users(Find_authorfile())
} }
func CreateMultipleAuthors(users []User) []string { func CreateMultipleAuthors(users []User) ([]string,[]string) {
if len(users) == 0 { if len(users) == 0 {
return []string{} return []string{}, []string{}
} }
var added_users []string var added_users []string
var not_added []string
for _, usr := range users { for _, usr := range users {
if _, ok := Users[usr.Shortname]; !ok { if _, ok := Users[usr.Shortname]; !ok {
@@ -162,6 +163,8 @@ func CreateMultipleAuthors(users []User) []string {
} }
} }
} }
} else {
not_added = append(not_added, (usr.Username + " - " + usr.Email + "\n"))
} }
} }
@@ -185,7 +188,7 @@ func CreateMultipleAuthors(users []User) []string {
Define_users(Find_authorfile()) Define_users(Find_authorfile())
return added_users return added_users, not_added
} }
func DeleteOneAuthor(author string) { func DeleteOneAuthor(author string) {
+2 -2
View File
@@ -48,7 +48,7 @@ func Commit(message string, authors []string) string {
} else if committer[0] == '^' { // Negations } else if committer[0] == '^' { // Negations
excludeMode = append(excludeMode, Users[committer[1:]].Username) excludeMode = append(excludeMode, Users[committer[1:]].Username)
} else { } else {
println(committer, " was unknown. User either not defined or name typed wrong") println(committer, "was unknown. User either not defined or name typed wrong")
} }
} }
@@ -130,7 +130,7 @@ func group_selection(group []User, excludeMode []string) []string {
return excludeMode return excludeMode
} }
func GitCommitAppender(authors string, hash string, flags []string, t,p,n bool) (error, string) { func GitCommitAppender(authors string, hash string, flags []string, t, p, n bool) (error, string) {
// Get old commit message // Get old commit message
var cmd *exec.Cmd var cmd *exec.Cmd
+55 -4
View File
@@ -136,7 +136,12 @@ func TempAddUser(username, email string) {
Users[username] = usr Users[username] = usr
} }
func SerealizeUsers(users []User) string { func SerealizeUsers(authors []string) string {
var users []User
for _, name := range authors {
users = append(users, Users[name])
}
bytes, err := json.Marshal(users) bytes, err := json.Marshal(users)
if err != nil { if err != nil {
panic(err) panic(err)
@@ -147,13 +152,59 @@ func SerealizeUsers(users []User) string {
return encoded return encoded
} }
func UnserealizeUsers(encoded string) []string { func UnserealizeUsers(encoded string) ([]string, []string) {
users := []User{} users := []User{}
raw, _ := base64.StdEncoding.DecodeString(encoded) raw, _ := base64.StdEncoding.DecodeString(encoded)
json.Unmarshal(raw, &users) json.Unmarshal(raw, &users)
added_users := CreateMultipleAuthors(users) added_users, not_added := CreateMultipleAuthors(users)
return added_users return added_users, not_added
}
func CLIAuthorInput(authors []string) []string {
var selected []string
excludeMode := []string{}
// write the commit message to the string builder
fst := authors[0]
if fst == "all" || fst == "All" {
selected = add_x_users_string_slice(excludeMode, selected)
return selected
} else if Groups[fst] != nil {
excludeMode = group_selection(Groups[fst], excludeMode)
selected = add_x_users_string_slice(excludeMode, selected)
return selected
}
for _, committer := range authors {
if _, ok := Users[committer]; ok {
selected = append(selected, committer)
} 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")
}
}
if len(excludeMode) > 0 {
selected = add_x_users_string_slice(excludeMode, selected)
}
return selected
}
func add_x_users_string_slice(excludeMode, selected []string) []string {
if len(DefExclude) > 0 {
excludeMode = append(excludeMode, DefExclude...)
}
for key, user := range Users {
if !slices.Contains(excludeMode, user.Username) {
selected = append(selected, key)
excludeMode = append(excludeMode, user.Username)
}
}
return selected
} }