mirror of
https://github.com/Slug-Boi/cocommit.git
synced 2026-05-13 12:45:47 +00:00
feat: add share code utility to program using 2 new flags on the users cli command
still need to add it to the TUI utility as a command
This commit is contained in:
@@ -1,8 +1,11 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"maps"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -28,6 +31,39 @@ func UsersCmd() *cobra.Command {
|
|||||||
update_msg()
|
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)
|
//TODO: make this print a bit prettier (sort it and maybe use a table)
|
||||||
// check if the no pretty print flag is set
|
// check if the no pretty print flag is set
|
||||||
np, _ := cmd.Flags().GetBool("np")
|
np, _ := cmd.Flags().GetBool("np")
|
||||||
@@ -63,4 +99,6 @@ func init() {
|
|||||||
usersCmd := UsersCmd()
|
usersCmd := UsersCmd()
|
||||||
rootCmd.AddCommand(usersCmd)
|
rootCmd.AddCommand(usersCmd)
|
||||||
usersCmd.Flags().BoolP("np", "n", false, "No pretty print of the users")
|
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)")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -136,6 +136,58 @@ func CreateAuthor(user User) {
|
|||||||
Define_users(Find_authorfile())
|
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) {
|
func DeleteOneAuthor(author string) {
|
||||||
// check that users aren't empty
|
// check that users aren't empty
|
||||||
if len(Users) < 1 {
|
if len(Users) < 1 {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -17,7 +18,7 @@ type User struct {
|
|||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Ex bool `json:"ex"`
|
Ex bool `json:"ex"`
|
||||||
Groups []string `json:"groups"`
|
Groups []string `json:"groups"`
|
||||||
From_git bool
|
From_git bool `json:"from_git,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Author struct {
|
type Author struct {
|
||||||
@@ -134,3 +135,25 @@ func TempAddUser(username, email string) {
|
|||||||
|
|
||||||
Users[username] = usr
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user