generated from matoszz/mitbgo-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
apitoken_create.go
84 lines (65 loc) · 2.26 KB
/
apitoken_create.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package datumapitokens
import (
"context"
"encoding/json"
"time"
"github.com/samber/lo"
"github.com/spf13/cobra"
"github.com/spf13/viper"
datum "github.com/datumforge/datum/cmd/cli/cmd"
"github.com/datumforge/datum/pkg/datumclient"
)
var apiTokenCreateCmd = &cobra.Command{
Use: "create",
Short: "Create a new datum api token",
RunE: func(cmd *cobra.Command, args []string) error {
return createAPIToken(cmd.Context())
},
}
func init() {
apiTokenCmd.AddCommand(apiTokenCreateCmd)
apiTokenCreateCmd.Flags().StringP("name", "n", "", "name of the api token token")
datum.ViperBindFlag("apitoken.create.name", apiTokenCreateCmd.Flags().Lookup("name"))
apiTokenCreateCmd.Flags().StringP("description", "d", "", "description of the api token")
datum.ViperBindFlag("apitoken.create.description", apiTokenCreateCmd.Flags().Lookup("description"))
apiTokenCreateCmd.Flags().DurationP("expiration", "e", 0, "duration of the api token to be valid, leave empty to never expire")
datum.ViperBindFlag("apitoken.create.expiration", apiTokenCreateCmd.Flags().Lookup("expiration"))
apiTokenCreateCmd.Flags().StringSlice("scopes", []string{"read", "write"}, "scopes to associate with the api token")
datum.ViperBindFlag("apitoken.create.scopes", apiTokenCreateCmd.Flags().Lookup("scopes"))
}
func createAPIToken(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)
var s []byte
name := viper.GetString("apitoken.create.name")
if name == "" {
return datum.NewRequiredFieldMissingError("token name")
}
input := datumclient.CreateAPITokenInput{
Name: name,
Scopes: viper.GetStringSlice("apitoken.create.scopes"),
}
description := viper.GetString("apitoken.create.description")
if description != "" {
input.Description = &description
}
expiration := viper.GetDuration("apitoken.create.expiration")
if expiration != 0 {
input.ExpiresAt = lo.ToPtr(time.Now().Add(expiration))
}
o, err := cli.Client.CreateAPIToken(ctx, input, cli.Interceptor)
if err != nil {
return err
}
s, err = json.Marshal(o)
if err != nil {
return err
}
return datum.JSONPrint(s)
}