diff options
-rw-r--r-- | cache.go | 2 | ||||
-rw-r--r-- | 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 /<format>/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 } |