about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2020-05-11 02:34:31 -0400
committerBen Morrison <ben@gbmor.dev>2020-05-11 02:36:35 -0400
commita729136a6a7e36a7b717ee5df615339cb4bde654 (patch)
tree13cd8ce2de5bfa615cae8b6eaa9c749b156cb0a1
parent65986b345f9e8ecf7e75f58fc1b2936a397e9520 (diff)
downloadapi-a729136a6a7e36a7b717ee5df615339cb4bde654.tar.gz
user list query finished
-rw-r--r--cache.go2
-rw-r--r--users.go34
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
 }