From a729136a6a7e36a7b717ee5df615339cb4bde654 Mon Sep 17 00:00:00 2001 From: Ben Morrison Date: Mon, 11 May 2020 02:34:31 -0400 Subject: user list query finished --- cache.go | 2 ++ users.go | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/cache.go b/cache.go index fde11ee..120e93e 100644 --- a/cache.go +++ b/cache.go @@ -87,6 +87,8 @@ func (cache *cacheWrapper) bap(requestPath string) error { bytes, err = uptimeQuery(format) case "usercount": bytes, err = userCountQuery(format) + case "users": + bytes, err = usersQuery(format) case "pkgs": bytes, err = pkgsQuery(format) default: diff --git a/users.go b/users.go index 7e1f40a..a40c177 100644 --- a/users.go +++ b/users.go @@ -1,10 +1,38 @@ package main -import "net/http" +import ( + "fmt" + "os/exec" + "strings" +) // Users handles the //users endpoint. // Responds with information on the system's users. -func Users(w http.ResponseWriter, r *http.Request, format string) error { +func usersQuery(format string) ([]byte, error) { + ls, err := exec.Command("/bin/ls", "/home").Output() + if err != nil { + return nil, fmt.Errorf("Users Query: %w", err) + } - return nil + users := strings.Fields(string(ls)) + out := `{ + "users": [ +` + for i, e := range users { + if strings.HasPrefix(e, ".") || strings.HasPrefix(e, "_") { + continue + } + + out = fmt.Sprintf("%s\t\t\"%s\"", out, e) + + if i < len(users)-1 { + out = fmt.Sprintf("%s,\n", out) + } else { + out = fmt.Sprintf("%s\n", out) + } + } + + out = fmt.Sprintf("%s\t]\n}\n", out) + + return []byte(out), nil } -- cgit 1.4.1-2-gfad0