generated from matoszz/mitbgo-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
apitoken_get.go
69 lines (55 loc) · 1.42 KB
/
apitoken_get.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package datumapitokens
import (
"context"
"encoding/json"
"github.com/spf13/cobra"
"github.com/spf13/viper"
datum "github.com/datumforge/datum/cmd/cli/cmd"
"github.com/datumforge/datum/pkg/datumclient"
)
var apiTokenGetCmd = &cobra.Command{
Use: "get",
Short: "Get or list api tokens",
RunE: func(cmd *cobra.Command, args []string) error {
return apiTokens(cmd.Context())
},
}
func init() {
apiTokenCmd.AddCommand(apiTokenGetCmd)
apiTokenGetCmd.Flags().StringP("id", "i", "", "api token id to query")
datum.ViperBindFlag("apitoken.get.id", apiTokenGetCmd.Flags().Lookup("id"))
}
func apiTokens(ctx context.Context) error {
// setup datum http client
cli, err := datum.GetGraphClient(ctx)
if err != nil {
return err
}
// save session cookies on function exit
client, _ := cli.Client.(*datumclient.Client)
defer datum.StoreSessionCookies(client)
// filter options
tokenID := viper.GetString("apitoken.get.id")
var s []byte
// if an api token ID is provided, filter on that api token, otherwise get all
if tokenID == "" {
tokens, err := cli.Client.GetAllAPITokens(ctx, cli.Interceptor)
if err != nil {
return err
}
s, err = json.Marshal(tokens)
if err != nil {
return err
}
} else {
token, err := cli.Client.GetAPITokenByID(ctx, tokenID, cli.Interceptor)
if err != nil {
return err
}
s, err = json.Marshal(token)
if err != nil {
return err
}
}
return datum.JSONPrint(s)
}