feat: allow for json files to be opened instead of the weird self made csv format

This commit is contained in:
Slug-Boi
2025-03-29 22:12:52 +01:00
parent f6a1316d4b
commit ec2f361583
4 changed files with 47 additions and 63 deletions
+43 -57
View File
@@ -1,84 +1,75 @@
package utils
import (
"bufio"
"fmt"
"os"
"strings"
"slices"
"encoding/json"
)
// This util file is used to handle users and their information
type User struct {
Username string
Email string
Names string
Shortname string `json:"shortname"`
Longname string `json:"longname"`
Username string `json:"username"`
Email string `json:"email"`
Ex bool `json:"ex"`
Groups []string `json:"groups"`
}
type Author struct {
Authors map[string]User
}
var Users = map[string]User{}
var DefExclude = []string{}
var Groups = map[string][]User{}
func ContainsUser(users []User, user User) bool {
return slices.ContainsFunc(users, func(u User) bool {
return u.Shortname == user.Shortname &&
u.Longname == user.Longname &&
u.Username == user.Username &&
u.Email == user.Email &&
u.Ex == user.Ex &&
slices.Equal(u.Groups, user.Groups)
})
}
func Define_users(author_file string) {
// wipe the users map
Users = map[string]User{}
DefExclude = []string{}
Groups = map[string][]User{}
file, err := os.Open(author_file)
var auth Author
data, err := os.ReadFile(author_file)
if err != nil {
print("File not found")
fmt.Println("Error reading author file: ", err)
os.Exit(2)
}
err = json.Unmarshal(data, &auth)
if err != nil {
fmt.Println("Error unmarshalling json: ", err)
os.Exit(2)
}
defer file.Close()
scanner := bufio.NewScanner(file)
// eat a single input
scanner.Scan()
// reads the input of authors file and formats accordingly
for scanner.Scan() {
input_str := scanner.Text()
group_info := []string{}
if strings.Contains(input_str, ";;") {
input := strings.Split(input_str, ";;")
input_str = input[0]
group_info = append(group_info, strings.Split(input[1], "|")...)
}
info := strings.Split(input_str, "|")
if len(info) < 4 {
if len(info) > 0 {
if info[0] == "" {
info[0] = "(empty string)"
}
fmt.Println("Error: User", info[0], "is missing information")
} else {
fmt.Println("Error: Some user is missing information")
}
fmt.Println("Please check the author file for proper syntax")
if input_str == "" {
fmt.Println("empty line found in author file")
} else {
fmt.Println("author file input:", input_str)
}
os.Exit(1)
}
usr := User{Username: info[2], Email: info[3], Names: info[0] + "/" + info[1]}
Users[info[0]] = usr
Users[info[1]] = usr
// Adds users with the ex tag to the defExclude list
if len(info) == 5 {
if info[4] == "ex" {
DefExclude = append(DefExclude, info[2])
}
for _, usr := range auth.Authors {
Users[usr.Shortname] = usr
Users[usr.Longname] = usr
if usr.Ex {
DefExclude = append(DefExclude, usr.Shortname)
}
group_info := usr.Groups
if len(group_info) > 0 {
// Group assignment
for _, group := range group_info {
if Groups[group] == nil {
Groups[group] = []User{usr}
} else {
//TODO: Try and find a cleaner way of doing this
usr_lst := Groups[group]
usr_lst = append(usr_lst, usr)
Groups[group] = usr_lst
@@ -86,17 +77,12 @@ func Define_users(author_file string) {
}
}
}
if err := scanner.Err(); err != nil {
os.Exit(2)
}
}
func RemoveUser(short string) {
usr := Users[short]
split := strings.Split(usr.Names, "/")
delete(Users, split[0])
delete(Users, split[1])
delete(Users, usr.Shortname)
delete(Users, usr.Longname)
}
func TempAddUser(username, email string) {