From 367c91d843b47f07e61ff31e94cd4aa329f797ec Mon Sep 17 00:00:00 2001 From: Arya Kiran Date: Wed, 18 Jan 2023 23:28:56 +0530 Subject: [PATCH] use regex for online-check --- main.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 1fbb277..3241ac5 100644 --- a/main.go +++ b/main.go @@ -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