add error handling for reading configuration #1

Merged
arya merged 9 commits from odyseusz/publapi:master into master 2024-11-04 20:20:30 +05:30
Showing only changes of commit e21e98bf52 - Show all commits

View File

@ -9,9 +9,10 @@ import (
"strconv"
"strings"
log "log/slog"
"github.com/ProjectSegfault/publapi/utils"
"github.com/gofiber/fiber/v2"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
@ -53,20 +54,24 @@ 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)
}
func userdata(username, usersonline, ops string) Userinfo {
regex := "(^| )" + username + "($| )"
isonline, err := regexp.MatchString(string(regex), string(usersonline))
if err != nil {
log.Error(err)
UserError("couldn't get online status for user", username, err)
}
isop, operr := regexp.MatchString(string(regex), string(ops))
if operr != nil {
log.Error(err)
UserError("couldn't get op status for user", username, operr)
}
cmd := "/usr/bin/stat -c %W /home/" + username
crd, crerr := exec.Command("bash", "-c", cmd).Output()
if crerr != nil {
log.Error(crerr)
UserError("couldn't stat user", username, crerr)
}
crdstr := string(crd)
crdstr = strings.TrimSuffix(crdstr, "\n")
@ -78,12 +83,12 @@ func userdata(username, usersonline, ops string) Userinfo {
var user Userinfo
user.Name = username
user.Created, _ = strconv.Atoi(crdstr)
if isonline == true {
if isonline {
user.Online = true
} else {
user.Online = false
}
if isop == true {
if isop {
user.Op = true
} else {
user.Op = false
@ -93,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", 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,
}
@ -122,12 +127,12 @@ func userdata(username, usersonline, ops string) Userinfo {
user.Matrix = viper.GetString("matrix")
user.Fediverse = viper.GetString("fediverse")
user.Loc = viper.GetString("location")
if isop == true {
if isop {
user.Op = true
} else {
user.Op = false
}
if isonline == true {
if isonline {
user.Online = true
} else {
user.Online = false
@ -145,7 +150,7 @@ func UsersPage(c *fiber.Ctx) error {
// Get the number of users online
usersonline, err := exec.Command("bash", "-c", "/usr/bin/users").Output()
if err != nil {
log.Error(err)
log.Error("error", log.Any("error", err))
return c.SendStatus(fiber.StatusInternalServerError)
}
usersonlinestr := string(usersonline)
@ -162,14 +167,14 @@ func UsersPage(c *fiber.Ctx) error {
// Get OPs
ops, opserr := exec.Command("bash", "-c", "/usr/bin/members sudo").Output()
if opserr != nil {
log.Error(err)
log.Error("couldn't get ops", "error", log.Any("err", err))
return c.SendStatus(fiber.StatusInternalServerError)
}
opstr := string(ops)
// Get all users
users, err2 := exec.Command("bash", "-c", "/usr/bin/ls /home").Output()
if err2 != nil {
log.Error(err2)
log.Error("couldn't get all users", "error", log.Any("err", err2))
return c.SendStatus(fiber.StatusInternalServerError)
}
userstr := string(users)