/
globalSecretGet.go
54 lines (43 loc) · 1.2 KB
/
globalSecretGet.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
48
49
50
51
52
53
54
package secret
import (
"context"
"fmt"
"github.com/clintjedwards/gofer/internal/cli/cl"
proto "github.com/clintjedwards/gofer/proto/go"
"github.com/spf13/cobra"
"google.golang.org/grpc/metadata"
)
var cmdGlobalSecretGet = &cobra.Command{
Use: "get <key>",
Short: "Read a secret from the global secret store",
Example: `$ gofer global secret get my_key`,
RunE: globalSecretGet,
Args: cobra.ExactArgs(1),
}
func init() {
CmdGlobalSecret.AddCommand(cmdGlobalSecretGet)
}
func globalSecretGet(_ *cobra.Command, args []string) error {
// We don't use the formatter here because we may want to redirect the object we get into
// a file or similar situation.
cl.State.Fmt.Finish()
key := args[0]
conn, err := cl.State.Connect()
if err != nil {
fmt.Println(err)
return err
}
client := proto.NewGoferClient(conn)
md := metadata.Pairs("Authorization", "Bearer "+cl.State.Config.Token)
ctx := metadata.NewOutgoingContext(context.Background(), md)
resp, err := client.GetGlobalSecret(ctx, &proto.GetGlobalSecretRequest{
Key: key,
IncludeSecret: true,
})
if err != nil {
fmt.Printf("could not read object: %v\n", err)
return err
}
fmt.Printf(resp.Secret)
return nil
}