-
Notifications
You must be signed in to change notification settings - Fork 2
/
grpc.go
46 lines (42 loc) · 956 Bytes
/
grpc.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
package cmd
import (
"fmt"
"github.com/jrockway/jsso2/pkg/client"
"github.com/spf13/cobra"
)
var (
clientset *client.Set
noClose bool
)
func AddClientset(c *cobra.Command) {
if c.PreRun != nil || c.PreRunE != nil {
panic("command already has a pre-run function")
}
c.PreRunE = func(cmd *cobra.Command, args []string) error {
if clientset != nil {
// Skip dialing for the tests.
return nil
}
credentials := &client.Credentials{
Bearer: bearer,
Root: root,
Token: session,
}
set, err := client.Dial(cmd.Context(), address, credentials)
if err != nil {
return fmt.Errorf("connect: %v", err)
}
clientset = set
return nil
}
if c.PostRun != nil || c.PostRunE != nil {
panic("command already has a post-run function")
}
c.PostRun = func(cmd *cobra.Command, args []string) {
if !noClose {
if err := clientset.Close(); err != nil {
cmd.PrintErrf("while closing client: %v\n", err)
}
}
}
}