Skip to content

Commit b7172df

Browse files
committed
Rework permissions formatting in galenectl.
1 parent 7fe6a5d commit b7172df

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

galenectl/galenectl.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,29 @@ func parsePermissions(p string, expand bool) (any, error) {
681681
return pp.Permissions(nil), nil
682682
}
683683

684+
func formatRawPermissions(permissions []string) string {
685+
var perms []byte
686+
for _, p := range permissions {
687+
if len(p) > 0 {
688+
perms = append(perms, p[0])
689+
} else {
690+
perms = append(perms, '?')
691+
}
692+
}
693+
sort.Slice(perms, func(i, j int) bool {
694+
return perms[i] < perms[j]
695+
})
696+
return fmt.Sprintf("[%s]", perms)
697+
}
698+
699+
func formatPermissions(permissions group.Permissions) string {
700+
s := permissions.String()
701+
if len(s) > 0 && s[0] != '[' {
702+
return s
703+
}
704+
return formatRawPermissions(permissions.Permissions(nil))
705+
}
706+
684707
func listUsersCmd(cmdname string, args []string) {
685708
var groupname string
686709
var long bool
@@ -732,7 +755,9 @@ func listUsersCmd(cmdname string, args []string) {
732755
fmt.Printf("%-12s (ERROR=%v)\n", user, err)
733756
continue
734757
}
735-
fmt.Printf("%-12s %v\n", user, d.Permissions)
758+
fmt.Printf("%-12s %v\n",
759+
user, formatPermissions(d.Permissions),
760+
)
736761
}
737762
}
738763
}

galenectl/galenectl_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"testing"
56

67
"github.com/jech/galene/group"
@@ -38,3 +39,29 @@ func TestMakePassword(t *testing.T) {
3839
t.Errorf("Wildcard didn't match")
3940
}
4041
}
42+
43+
func TestFormatPermissions(t *testing.T) {
44+
tests := []struct{ j, v, p string }{
45+
{`"op"`, "op", "[mopt]"},
46+
{`"present"`, "present", "[mp]"},
47+
{`"observe"`, "observe", "[]"},
48+
{`"admin"`, "admin", "[a]"},
49+
{`["message", "present", "token"]`, "[mpt]", "[mpt]"},
50+
}
51+
for _, test := range tests {
52+
var p group.Permissions
53+
err := json.Unmarshal([]byte(test.j), &p)
54+
if err != nil {
55+
t.Errorf("Unmarshal %#v: %v", test.j, err)
56+
continue
57+
}
58+
v := formatPermissions(p)
59+
if v != test.v {
60+
t.Errorf("Expected %v, got %v", test.v, v)
61+
}
62+
pp := formatRawPermissions(p.Permissions(nil))
63+
if pp != test.p {
64+
t.Errorf("Expected %v, got %v", test.p, pp)
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)