Skip to content

Commit

Permalink
Rework permissions formatting in galenectl.
Browse files Browse the repository at this point in the history
  • Loading branch information
jech committed Oct 30, 2024
1 parent 7fe6a5d commit b7172df
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
27 changes: 26 additions & 1 deletion galenectl/galenectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,29 @@ func parsePermissions(p string, expand bool) (any, error) {
return pp.Permissions(nil), nil
}

func formatRawPermissions(permissions []string) string {
var perms []byte
for _, p := range permissions {
if len(p) > 0 {
perms = append(perms, p[0])
} else {
perms = append(perms, '?')
}
}
sort.Slice(perms, func(i, j int) bool {
return perms[i] < perms[j]
})
return fmt.Sprintf("[%s]", perms)
}

func formatPermissions(permissions group.Permissions) string {
s := permissions.String()
if len(s) > 0 && s[0] != '[' {
return s
}
return formatRawPermissions(permissions.Permissions(nil))
}

func listUsersCmd(cmdname string, args []string) {
var groupname string
var long bool
Expand Down Expand Up @@ -732,7 +755,9 @@ func listUsersCmd(cmdname string, args []string) {
fmt.Printf("%-12s (ERROR=%v)\n", user, err)
continue
}
fmt.Printf("%-12s %v\n", user, d.Permissions)
fmt.Printf("%-12s %v\n",
user, formatPermissions(d.Permissions),
)
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions galenectl/galenectl_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"testing"

"github.com/jech/galene/group"
Expand Down Expand Up @@ -38,3 +39,29 @@ func TestMakePassword(t *testing.T) {
t.Errorf("Wildcard didn't match")
}
}

func TestFormatPermissions(t *testing.T) {
tests := []struct{ j, v, p string }{
{`"op"`, "op", "[mopt]"},
{`"present"`, "present", "[mp]"},
{`"observe"`, "observe", "[]"},
{`"admin"`, "admin", "[a]"},
{`["message", "present", "token"]`, "[mpt]", "[mpt]"},
}
for _, test := range tests {
var p group.Permissions
err := json.Unmarshal([]byte(test.j), &p)
if err != nil {
t.Errorf("Unmarshal %#v: %v", test.j, err)
continue
}
v := formatPermissions(p)
if v != test.v {
t.Errorf("Expected %v, got %v", test.v, v)
}
pp := formatRawPermissions(p.Permissions(nil))
if pp != test.p {
t.Errorf("Expected %v, got %v", test.p, pp)
}
}
}

0 comments on commit b7172df

Please sign in to comment.