1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-22 00:25:58 +01:00

Add pattern support to galenectl.

This commit is contained in:
Juliusz Chroboczek 2024-10-30 17:48:49 +01:00
parent 8de77fe417
commit ed61b208a9

View file

@ -15,6 +15,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"path"
"path/filepath" "path/filepath"
"sort" "sort"
"strings" "strings"
@ -704,22 +705,31 @@ func formatPermissions(permissions group.Permissions) string {
return formatRawPermissions(permissions.Permissions(nil)) return formatRawPermissions(permissions.Permissions(nil))
} }
func match(patterns []string, value string) (bool, error) {
for _, p := range patterns {
found, err := path.Match(p, value)
if err != nil {
return false, err
}
if found {
return true, nil
}
}
return false, nil
}
func listUsersCmd(cmdname string, args []string) { func listUsersCmd(cmdname string, args []string) {
var groupname string var groupname string
var long bool var long bool
cmd := flag.NewFlagSet(cmdname, flag.ExitOnError) cmd := flag.NewFlagSet(cmdname, flag.ExitOnError)
setUsage(cmd, cmdname, setUsage(cmd, cmdname,
"%v [option...] %v [option...]\n", "%v [option...] %v [option...] [pattern...]\n",
os.Args[0], cmdname, os.Args[0], cmdname,
) )
cmd.StringVar(&groupname, "group", "", "group `name`") cmd.StringVar(&groupname, "group", "", "group `name`")
cmd.BoolVar(&long, "l", false, "display permissions") cmd.BoolVar(&long, "l", false, "display permissions")
cmd.Parse(args) cmd.Parse(args)
patterns := cmd.Args()
if cmd.NArg() != 0 {
cmd.Usage()
os.Exit(1)
}
if groupname == "" { if groupname == "" {
fmt.Fprintf(cmd.Output(), fmt.Fprintf(cmd.Output(),
@ -741,6 +751,15 @@ func listUsersCmd(cmdname string, args []string) {
return users[i] < users[j] return users[i] < users[j]
}) })
for _, user := range users { for _, user := range users {
if len(patterns) > 0 {
found, err := match(patterns, user)
if err != nil {
log.Fatalf("Match: %v", err)
}
if !found {
continue
}
}
if !long { if !long {
fmt.Println(user) fmt.Println(user)
} else { } else {
@ -930,10 +949,11 @@ func deleteUserCmd(cmdname string, args []string) {
func listGroupsCmd(cmdname string, args []string) { func listGroupsCmd(cmdname string, args []string) {
cmd := flag.NewFlagSet(cmdname, flag.ExitOnError) cmd := flag.NewFlagSet(cmdname, flag.ExitOnError)
setUsage(cmd, cmdname, setUsage(cmd, cmdname,
"%v [option...] %v\n", "%v [option...] %v [pattern...]\n",
os.Args[0], cmdname, os.Args[0], cmdname,
) )
cmd.Parse(args) cmd.Parse(args)
patterns := cmd.Args()
u, err := url.JoinPath(serverURL, "/galene-api/v0/.groups/") u, err := url.JoinPath(serverURL, "/galene-api/v0/.groups/")
if err != nil { if err != nil {
@ -949,6 +969,15 @@ func listGroupsCmd(cmdname string, args []string) {
return groups[i] < groups[j] return groups[i] < groups[j]
}) })
for _, g := range groups { for _, g := range groups {
if len(patterns) > 0 {
found, err := match(patterns, g)
if err != nil {
log.Fatalf("Match: %v", err)
}
if !found {
continue
}
}
fmt.Println(g) fmt.Println(g)
} }
} }