diff options
author | Ben Morrison <ben@gbmor.dev> | 2020-05-11 02:34:31 -0400 |
---|---|---|
committer | Ben Morrison <ben@gbmor.dev> | 2020-05-11 02:36:35 -0400 |
commit | a729136a6a7e36a7b717ee5df615339cb4bde654 (patch) | |
tree | 13cd8ce2de5bfa615cae8b6eaa9c749b156cb0a1 | |
parent | 65986b345f9e8ecf7e75f58fc1b2936a397e9520 (diff) | |
download | api-a729136a6a7e36a7b717ee5df615339cb4bde654.tar.gz |
user list query finished
-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 } |