diff --git a/src/cmd/users.go b/src/cmd/users.go index 8654e29..f8ec00b 100644 --- a/src/cmd/users.go +++ b/src/cmd/users.go @@ -1,8 +1,11 @@ package cmd import ( + "fmt" + "maps" "os" "os/exec" + "slices" "sort" "strings" @@ -28,6 +31,39 @@ func UsersCmd() *cobra.Command { update_msg() } + s, _ := cmd.Flags().GetBool("share") + if s && len(args) == 0 { + encoded := utils.SerealizeUsers(slices.Collect(maps.Values(utils.Users))) + 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]) + } + encoded := utils.SerealizeUsers(users) + fmt.Print(encoded) + os.Exit(0) + } + + i, _ := cmd.Flags().GetBool("import") + if i && len(args) == 1 { + added_users := 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") + for _, usr := range added_users { + fmt.Println("\033[32m+\033[0m ", usr) + } + os.Exit(0) + } else { + fmt.Println("\033[33mNo \"share code\", please run the flag with a valid \"share code\"\033[0m") + os.Exit(0) + } + //TODO: make this print a bit prettier (sort it and maybe use a table) // check if the no pretty print flag is set np, _ := cmd.Flags().GetBool("np") @@ -45,7 +81,7 @@ func UsersCmd() *cobra.Command { println(strings.Join(user_sb, "")) os.Exit(0) } - bat_check := exec.Command("bat","--version") + bat_check := exec.Command("bat", "--version") out, _ := bat_check.CombinedOutput() if string(out) == "" { tui.Entry_US(authorfile) @@ -63,4 +99,6 @@ func init() { usersCmd := UsersCmd() rootCmd.AddCommand(usersCmd) usersCmd.Flags().BoolP("np", "n", false, "No pretty print of the users") + usersCmd.Flags().BoolP("share", "s", false, "Shares one or more users as a \"share code\" (encoded json)") + usersCmd.Flags().BoolP("import", "i", false, "Imports users from \"share code\" (encoded json)") } diff --git a/src/cmd/utils/author_file_utils.go b/src/cmd/utils/author_file_utils.go index dd61248..d928b06 100644 --- a/src/cmd/utils/author_file_utils.go +++ b/src/cmd/utils/author_file_utils.go @@ -136,6 +136,58 @@ func CreateAuthor(user User) { Define_users(Find_authorfile()) } +func CreateMultipleAuthors(users []User) []string { + if len(users) == 0 { + return []string{} + } + + var added_users []string + + for _, usr := range users { + if _, ok := Users[usr.Shortname]; !ok { + added_users = append(added_users, (usr.Username + " - " + usr.Email + "\n")) + Users[usr.Shortname] = usr + Users[usr.Longname] = usr + Authors.Authors[usr.Longname] = usr + + group_info := usr.Groups + if len(group_info) > 0 { + for _, group := range group_info { + if Groups[group] == nil { + Groups[group] = []User{usr} + } else { + usr_lst := Groups[group] + usr_lst = append(usr_lst, usr) + Groups[group] = usr_lst + } + } + } + } + } + + data, err := json.MarshalIndent(Authors, "", " ") + if err != nil { + panic(fmt.Sprintf("Error marshalling json: %v", err)) + } + + author_file := Find_authorfile() + f, err := os.OpenFile(author_file, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) + if err != nil { + panic(err) + } + defer f.Close() + + // write the data to the file + f.Truncate(0) + f.Seek(0, 0) + f.Write(data) + f.Close() + + Define_users(Find_authorfile()) + + return added_users +} + func DeleteOneAuthor(author string) { // check that users aren't empty if len(Users) < 1 { diff --git a/src/cmd/utils/user_util.go b/src/cmd/utils/user_util.go index 323c3d6..c853799 100644 --- a/src/cmd/utils/user_util.go +++ b/src/cmd/utils/user_util.go @@ -5,6 +5,7 @@ import ( "os" "slices" + "encoding/base64" "encoding/json" ) @@ -17,7 +18,7 @@ type User struct { Email string `json:"email"` Ex bool `json:"ex"` Groups []string `json:"groups"` - From_git bool + From_git bool `json:"from_git,omitempty"` } type Author struct { @@ -134,3 +135,25 @@ func TempAddUser(username, email string) { Users[username] = usr } + +func SerealizeUsers(users []User) string { + bytes, err := json.Marshal(users) + if err != nil { + panic(err) + } + + encoded := base64.StdEncoding.EncodeToString(bytes) + + return encoded +} + +func UnserealizeUsers(encoded string) []string { + users := []User{} + + raw, _ := base64.StdEncoding.DecodeString(encoded) + json.Unmarshal(raw, &users) + + added_users := CreateMultipleAuthors(users) + + return added_users +}