about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2020-05-09 02:13:29 -0400
committerBen Morrison <ben@gbmor.dev>2020-05-09 02:13:29 -0400
commit1eab8d9a920942749547bcba80f9ad9e1c7cc276 (patch)
treed1b7f76dabf0847621a66e321b3a669220f3987c
parentd928c73de4c75f3113a4ddb3418789d950097fcb (diff)
downloadapi-1eab8d9a920942749547bcba80f9ad9e1c7cc276.tar.gz
usercount query is done
-rw-r--r--cache.go2
-rw-r--r--usercount.go29
2 files changed, 26 insertions, 5 deletions
diff --git a/cache.go b/cache.go
index d7839b8..a0d56eb 100644
--- a/cache.go
+++ b/cache.go
@@ -78,6 +78,8 @@ func (cache *cacheWrapper) bap(requestPath string) {
 		bytes, err = osVersionQuery(format)
 	case "uptime":
 		bytes, err = uptimeQuery(format)
+	case "usercount":
+		bytes, err = userCountQuery(format)
 	}
 
 	if err != nil {
diff --git a/usercount.go b/usercount.go
index 11a2674..0afc462 100644
--- a/usercount.go
+++ b/usercount.go
@@ -1,10 +1,29 @@
 package main
 
-import "net/http"
+import (
+	"fmt"
+	"os/exec"
+	"strings"
+)
 
-// UserCount handles the /<format>/usercount endpoint.
-// Responds with the number of registered users on the system.
-func UserCount(w http.ResponseWriter, r *http.Request, format string) error {
+// Just returns the number of directories in /home
+// The assumption being, it's the number of human users.
+func userCountQuery(format string) ([]byte, error) {
+	ls, err := exec.Command("ls", "/home").Output()
+	if err != nil {
+		return nil, fmt.Errorf("Couldn't execute ls: %w", err)
+	}
 
-	return nil
+	split := strings.Split(string(ls), " ")
+
+	if format == "plain" {
+		return []byte(fmt.Sprintf("%v users", len(split))), nil
+	}
+
+	out := fmt.Sprintf(`
+{
+	"userCount": "%v"
+}`, len(split))
+
+	return []byte(out), nil
 }