mirror of
https://github.com/Slug-Boi/cocommit.git
synced 2026-05-13 12:45:47 +00:00
feat: redo using tui and cli input modes. Also add clearer feedback to import
TODO: create tests
This commit is contained in:
+21
-8
@@ -2,10 +2,8 @@ package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"maps"
|
||||
"os"
|
||||
"os/exec"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
@@ -33,13 +31,19 @@ func UsersCmd() *cobra.Command {
|
||||
|
||||
s, _ := cmd.Flags().GetBool("share")
|
||||
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)
|
||||
os.Exit(0)
|
||||
} else if s && len(args) >= 1 {
|
||||
var users []utils.User
|
||||
for _, name := range args {
|
||||
users = append(users, utils.Users[name])
|
||||
users := utils.CLIAuthorInput(args)
|
||||
if len(users) == 0 {
|
||||
fmt.Println("\033[31mNo authors selected exiting\033[31m")
|
||||
os.Exit(0)
|
||||
}
|
||||
encoded := utils.SerealizeUsers(users)
|
||||
fmt.Print(encoded)
|
||||
@@ -48,16 +52,25 @@ func UsersCmd() *cobra.Command {
|
||||
|
||||
i, _ := cmd.Flags().GetBool("import")
|
||||
if i && len(args) == 1 {
|
||||
added_users := utils.UnserealizeUsers(args[0])
|
||||
added_users, not_added := utils.UnserealizeUsers(args[0])
|
||||
if len(added_users) == 0 {
|
||||
fmt.Println("\033[33mNo authors added (authors probably already existed or corrupted \"share code\")\033[0m")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if len(added_users) != 0 {
|
||||
fmt.Println("\033[32mAuthors added:\033[0m")
|
||||
}
|
||||
for _, usr := range added_users {
|
||||
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)
|
||||
} else {
|
||||
fmt.Println("\033[33mNo \"share code\", please run the flag with a valid \"share code\"\033[0m")
|
||||
|
||||
@@ -136,12 +136,13 @@ func CreateAuthor(user User) {
|
||||
Define_users(Find_authorfile())
|
||||
}
|
||||
|
||||
func CreateMultipleAuthors(users []User) []string {
|
||||
func CreateMultipleAuthors(users []User) ([]string,[]string) {
|
||||
if len(users) == 0 {
|
||||
return []string{}
|
||||
return []string{}, []string{}
|
||||
}
|
||||
|
||||
var added_users []string
|
||||
var not_added []string
|
||||
|
||||
for _, usr := range users {
|
||||
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())
|
||||
|
||||
return added_users
|
||||
return added_users, not_added
|
||||
}
|
||||
|
||||
func DeleteOneAuthor(author string) {
|
||||
|
||||
@@ -136,7 +136,12 @@ func TempAddUser(username, email string) {
|
||||
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)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@@ -147,13 +152,59 @@ func SerealizeUsers(users []User) string {
|
||||
return encoded
|
||||
}
|
||||
|
||||
func UnserealizeUsers(encoded string) []string {
|
||||
func UnserealizeUsers(encoded string) ([]string, []string) {
|
||||
users := []User{}
|
||||
|
||||
raw, _ := base64.StdEncoding.DecodeString(encoded)
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user