func extractDSCacheUtilKeyValues()

in internal/util/util.go [152:183]


func extractDSCacheUtilKeyValues(text []byte, keys []string) map[string]string {
	// Command output from dscacheutil should look like:
	//
	//   name: ec2-user
	//   password: ********
	//   uid: 501
	//   gid: 20
	//   dir: /Users/ec2-user
	//   shell: /bin/bash
	//   gecos: ec2-user
	//

	extracted := map[string]string{}

	lines := bytes.Split(text, []byte("\n")) // split on newline to separate uid and gid
	for _, kvLine := range lines {
		// kv splits the [key: value] lines into their components
		kv := bytes.SplitN(kvLine, []byte(": "), 2)

		if len(kv) < 2 {
			continue
		}

		for _, key := range keys {
			if bytes.EqualFold(kv[0], []byte(key)) {
				extracted[key] = string(kv[1])
			}
		}
	}

	return extracted
}