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