-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump-groups.go
47 lines (43 loc) · 1.44 KB
/
dump-groups.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"context"
"encoding/json"
"fmt"
"os/signal"
"syscall"
"github.com/uselagoon/lagoon-opensearch-sync/internal/keycloak"
)
// DumpGroupsCmd represents the `dump-groups` command.
type DumpGroupsCmd struct {
KeycloakClientID string `kong:"default='lagoon-opensearch-sync',env='KEYCLOAK_CLIENT_ID',help='Keycloak OAuth2 Client ID'"`
KeycloakClientSecret string `kong:"required,env='KEYCLOAK_CLIENT_SECRET',help='Keycloak OAuth2 Client Secret'"`
KeycloakBaseURL string `kong:"required,env='KEYCLOAK_BASE_URL',help='Keycloak Base URL'"`
Raw bool `kong:"help='Dump the raw JSON recevied from the backend service.'"`
}
// Run the dump-groups command.
func (cmd *DumpGroupsCmd) Run() error {
// get main process context, which cancels on SIGTERM
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM)
defer stop()
// init the keycloak client
k, err := keycloak.NewClientCredentialsClient(ctx, cmd.KeycloakBaseURL,
cmd.KeycloakClientID, cmd.KeycloakClientSecret)
if err != nil {
return fmt.Errorf("couldn't init keycloak client: %v", err)
}
if cmd.Raw {
data, err := k.RawGroups(ctx)
fmt.Println(string(data))
return err
}
groups, err := k.Groups(ctx)
if err != nil {
return fmt.Errorf("couldn't get keycloak groups: %v", err)
}
j, err := json.Marshal(groups)
if err != nil {
return fmt.Errorf("couldn't marshal groups: %v", err)
}
_, err = fmt.Println(string(j))
return err
}