diff --git a/src/cmd/users.go b/src/cmd/users.go index f8ec00b..6973f65 100644 --- a/src/cmd/users.go +++ b/src/cmd/users.go @@ -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) } - fmt.Println("\033[32mAuthors added:\033[0m") + 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") diff --git a/src/cmd/utils/author_file_utils.go b/src/cmd/utils/author_file_utils.go index d928b06..5534e51 100644 --- a/src/cmd/utils/author_file_utils.go +++ b/src/cmd/utils/author_file_utils.go @@ -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) { diff --git a/src/cmd/utils/commit.go b/src/cmd/utils/commit.go index 72ffcf4..1605d75 100644 --- a/src/cmd/utils/commit.go +++ b/src/cmd/utils/commit.go @@ -48,7 +48,7 @@ func Commit(message string, authors []string) string { } 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") + println(committer, "was unknown. User either not defined or name typed wrong") } } @@ -130,11 +130,11 @@ func group_selection(group []User, excludeMode []string) []string { 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 var cmd *exec.Cmd - //TODO: Make the hash ammend work with rebase but its more complicated than orignally thought. + //TODO: Make the hash ammend work with rebase but its more complicated than orignally thought. // git log --format=%B -n1 if hash == "" { @@ -167,7 +167,7 @@ func GitCommitAppender(authors string, hash string, flags []string, t,p,n bool) if err != nil { return fmt.Errorf("Could not create tempfile: %s", err.Error()), "" } - defer os.Remove(file.Name()) + defer os.Remove(file.Name()) // Write the old commit message to the file _, err = file.WriteString(old_commit + "\n" + authors) @@ -182,7 +182,7 @@ func GitCommitAppender(authors string, hash string, flags []string, t,p,n bool) } else { edited_commit = old_commit + "\n" + authors } - + input = append(input, "--amend", "-m", edited_commit) if p { diff --git a/src/cmd/utils/user_util.go b/src/cmd/utils/user_util.go index c853799..ac0d5f5 100644 --- a/src/cmd/utils/user_util.go +++ b/src/cmd/utils/user_util.go @@ -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 }