use regex for online-check

This commit is contained in:
Arya Kiran 2023-01-18 23:28:56 +05:30
parent 7a3ed3d955
commit 367c91d843
Signed by: arya
GPG Key ID: 842D12BDA50DF120
1 changed files with 10 additions and 2 deletions

12
main.go
View File

@ -9,6 +9,7 @@ import (
"os/exec"
"runtime"
"strings"
"regexp"
)
type Userstruct struct {
@ -56,12 +57,19 @@ func contains(strs []string, str string) bool {
func userdata(username, usersonline string) Userinfo {
filename := "/home/" + username + "/meta-info.env"
_, error := os.Stat(filename)
regex := "(^| )" + username + "($| )"
log.Info(usersonline)
isonline, err := regexp.MatchString(string(regex), string(usersonline))
log.Info(isonline)
if err != nil {
log.Error(err)
}
if error != nil {
if os.IsNotExist(error) {
log.Error(username + " does not have a meta-info.env")
var user Userinfo
user.Name = username
if strings.Contains(usersonline, " "+username) == true {
if isonline == true {
user.Online = true
} else {
user.Online = false
@ -79,7 +87,7 @@ func userdata(username, usersonline string) Userinfo {
user.Desc = viper.GetString("DESCRIPTION")
user.Email = viper.GetString("EMAIL")
user.Loc = viper.GetString("LOCATION")
if strings.Contains(usersonline, " "+username) == true {
if isonline == true {
user.Online = true
} else {
user.Online = false