Browse Source

add list users subcommand

related issue: #37
pull/70/head
Linus789 3 years ago
parent
commit
1c40689e9d
  1. 24
      README.md
  2. 70
      list.go

24
README.md

@ -33,18 +33,18 @@ Unofficial packages are available on: [Debian](https://packages.debian.org/buste
### Commands
update Update the index
search [<regexp...>] Search available apps
show <appid...> Show detailed info about apps
install [<appid...>] Install or upgrade apps
uninstall <appid...> Uninstall an app
download <appid...> Download an app
devices List connected devices
list (categories) List all known values of a kind
repo Manage repositories
clean Clean index and/or cache
defaults Reset to the default settings
version Print version information
update Update the index
search [<regexp...>] Search available apps
show <appid...> Show detailed info about apps
install [<appid...>] Install or upgrade apps
uninstall <appid...> Uninstall an app
download <appid...> Download an app
devices List connected devices
list (categories/users) List all known values of a kind
repo Manage repositories
clean Clean index and/or cache
defaults Reset to the default settings
version Print version information
An appid is just an app's unique package name. A specific version of an app can

70
list.go

@ -4,13 +4,16 @@
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"sort"
"strings"
)
var cmdList = &Command{
UsageLine: "list (categories)",
UsageLine: "list (categories/users)",
Short: "List all known values of a kind",
}
@ -34,6 +37,10 @@ func runList(args []string) error {
values[c] = struct{}{}
}
}
case "users":
if err := listUsers(); err != nil {
return err
}
default:
return fmt.Errorf("invalid argument")
}
@ -47,3 +54,64 @@ func runList(args []string) error {
}
return nil
}
var userIdNameRegex = regexp.MustCompile(`UserInfo{(\d+):([^:}]*):[^}]*}`)
func listUsers() error {
device, err := oneDevice()
if err != nil {
return err
}
cmd := device.AdbShell("pm", "list", "users")
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
uidHeader := "UID"
nameHeader := "Name"
runningHeader := "Running"
scanner := bufio.NewScanner(stdout)
uids := make([]string, 0)
names := make([]string, 0)
running := make([]bool, 0)
maxUidLen := len(uidHeader)
maxNameLen := len(nameHeader)
for scanner.Scan() {
text := scanner.Text()
m := userIdNameRegex.FindStringSubmatch(text)
if m == nil {
continue
}
uid := m[1]
uids = append(uids, uid)
if uidLen := len(uid); uidLen > maxUidLen {
maxUidLen = uidLen
}
name := m[2]
names = append(names, name)
if nameLen := len(name); nameLen > maxNameLen {
maxNameLen = nameLen
}
currentRunning := false
if strings.HasSuffix(strings.TrimSpace(text), "running") {
currentRunning = true
}
running = append(running, currentRunning)
}
if len(uids) == 0 {
return nil
}
fmt.Printf("%-*s %-*s %s\n", maxUidLen, uidHeader, maxNameLen, nameHeader, runningHeader)
for i, uid := range uids {
name := names[i]
runningStr := ""
if running[i] {
runningStr = "Yes"
}
fmt.Printf("%*s %-*s %s\n", maxUidLen, uid, maxNameLen, name, runningStr)
}
return nil
}

Loading…
Cancel
Save