about summary refs log tree commit diff stats
path: root/usercount.go
blob: 0afc462b35e3a9b33ff73e36176049fcb9727f21 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package main

import (
	"fmt"
	"os/exec"
	"strings"
)

// 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)
	}

	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
}