1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-14 04:35:57 +01:00

Implement galenectl list-tokens.

This commit is contained in:
Juliusz Chroboczek 2024-10-30 15:44:53 +01:00
parent 991c0e1fc5
commit f8f7b539f1

View file

@ -25,6 +25,7 @@ import (
"golang.org/x/term" "golang.org/x/term"
"github.com/jech/galene/group" "github.com/jech/galene/group"
"github.com/jech/galene/token"
) )
type configuration struct { type configuration struct {
@ -57,6 +58,10 @@ var commands = map[string]command{
command: deletePasswordCmd, command: deletePasswordCmd,
description: "delete a user's password", description: "delete a user's password",
}, },
"list-groups": {
command: listGroupsCmd,
description: "list groups",
},
"create-group": { "create-group": {
command: createGroupCmd, command: createGroupCmd,
description: "create a group", description: "create a group",
@ -81,9 +86,9 @@ var commands = map[string]command{
command: updateUserCmd, command: updateUserCmd,
description: "change a user's permissions", description: "change a user's permissions",
}, },
"list-groups": { "list-tokens": {
command: listGroupsCmd, command: listTokensCmd,
description: "list groups", description: "list tokens",
}, },
"create-token": { "create-token": {
command: createTokenCmd, command: createTokenCmd,
@ -923,6 +928,90 @@ func listGroupsCmd(cmdname string, args []string) {
} }
} }
func listTokensCmd(cmdname string, args []string) {
var groupname string
var long bool
cmd := flag.NewFlagSet(cmdname, flag.ExitOnError)
setUsage(cmd, cmdname, "%v [option...] %v [option...]\n",
os.Args[0], cmdname,
)
cmd.StringVar(&groupname, "group", "", "group `name`")
cmd.BoolVar(&long, "l", false, "display token fields")
cmd.Parse(args)
if cmd.NArg() != 0 {
cmd.Usage()
os.Exit(1)
}
if groupname == "" {
fmt.Fprintf(cmd.Output(),
"Option \"-group\" is required\n")
os.Exit(1)
}
u, err := url.JoinPath(
serverURL, "/galene-api/v0/.groups/", groupname, ".tokens/",
)
if err != nil {
log.Fatalf("Build URL: %v", err)
}
var tokens []string
_, err = getJSON(u, &tokens)
if err != nil {
log.Fatalf("Get tokens: %v", err)
}
sort.Slice(tokens, func(i, j int) bool {
return tokens[i] < tokens[j]
})
now := time.Now()
for _, t := range tokens {
if !long {
fmt.Println(t)
} else {
uu, err := url.JoinPath(u, t)
if err != nil {
fmt.Printf("%-12s (ERROR=%v)\n", t, err)
continue
}
var tt token.Stateful
_, err = getJSON(uu, &tt)
if err != nil {
fmt.Printf("%-12s (ERROR=%v)\n", t, err)
continue
}
var username string
if tt.Username != nil {
username = *tt.Username
}
var exp string
if tt.Expires == nil {
exp = "(no expiration date)"
} else if tt.Expires.Before(now) {
exp = "(expired)"
} else {
exp = tt.Expires.Format(time.DateTime)
}
var perms []byte
for _, p := range tt.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]
})
fmt.Printf("%-11s %-12s %-4s %-20s\n", t,
username, perms, exp,
)
}
}
}
func createTokenCmd(cmdname string, args []string) { func createTokenCmd(cmdname string, args []string) {
var groupname, username, permissions string var groupname, username, permissions string
cmd := flag.NewFlagSet(cmdname, flag.ExitOnError) cmd := flag.NewFlagSet(cmdname, flag.ExitOnError)