minor cleanups

This commit is contained in:
2024-09-29 12:29:59 +02:00
parent 2f4cbc6e1e
commit dc6fb02d5f
2 changed files with 11 additions and 12 deletions

View File

@@ -55,7 +55,7 @@ func (a ByAdminAndName) Less(i, j int) bool {
func (a ByAdminAndName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func UserError(message string, username string, err error) {
log.Error("error", message, log.Any("err", err), "username", username)
log.Error("message", message, log.Any("err", err), "username", username)
}
func userData(username, usersonline, ops string) UserInfo {
@@ -79,7 +79,7 @@ func userData(username, usersonline, ops string) UserInfo {
_, error := os.Stat(filename)
if error != nil {
if os.IsNotExist(error) {
log.Error(username + " does not have a meta-info.toml")
UserError("user doesn't have a meta-info.toml", username, error)
var user UserInfo
user.Name = username
user.Created, _ = strconv.Atoi(crdstr)
@@ -98,7 +98,7 @@ func userData(username, usersonline, ops string) UserInfo {
}
viper.SetConfigFile(filename)
if err := viper.ReadInConfig(); err != nil {
log.Error("message", "Couldn't read a users meta-info.toml file.", "error", log.Any("err", err), "user", username)
log.Error("message", "couldn't read a users meta-info.toml file.", "error", log.Any("err", err), "user", username)
user := UserInfo{
Name: username,
}
@@ -175,15 +175,14 @@ func UsersPage(c *fiber.Ctx) error {
log.Error("couldn't get all users", "error", log.Any("err", err2))
return c.SendStatus(fiber.StatusInternalServerError)
}
userstr := string(users)
userstr2 := strings.TrimSuffix(userstr, "\n")
usersarr := strings.Split(userstr2, "\n")
userstr := strings.TrimSuffix(string(users), "\n")
usersarr := strings.Split(userstr, "\n")
// Fill user info
var userInfoStruct []UserInfo
var userInfoArray []UserInfo
for i := 0; i < len(usersarr); i++ {
uname := string(usersarr[i])
userInfoStruct = append(
userInfoStruct,
userInfoArray = append(
userInfoArray,
userData(
uname,
strings.TrimSuffix(usersOnlineDedup, "\n"),
@@ -191,11 +190,11 @@ func UsersPage(c *fiber.Ctx) error {
),
)
}
sort.Sort(ByAdminAndName(userInfoStruct))
sort.Sort(ByAdminAndName(userInfoArray))
data := UserStruct{
Status: c.Response().StatusCode(),
Online: output,
Users: userInfoStruct,
Users: userInfoArray,
Total: int(strings.Count(userstr, "\n")),
}
return c.JSON(data)