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