-
Notifications
You must be signed in to change notification settings - Fork 142
/
token.go
81 lines (62 loc) · 1.48 KB
/
token.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
package cli
import (
"context"
"fmt"
"log"
"os"
"github.com/spf13/cobra"
"github.com/hatchet-dev/hatchet/internal/config/loader"
)
var (
tokenTenantId string
tokenName string
)
var tokenCmd = &cobra.Command{
Use: "token",
Short: "command for generating tokens.",
}
var tokenCreateAPICmd = &cobra.Command{
Use: "create",
Short: "create a new API token.",
Run: func(cmd *cobra.Command, args []string) {
err := runCreateAPIToken()
if err != nil {
log.Printf("Fatal: could not run [token create] command: %v", err)
os.Exit(1)
}
},
}
func init() {
rootCmd.AddCommand(tokenCmd)
tokenCmd.AddCommand(tokenCreateAPICmd)
tokenCreateAPICmd.PersistentFlags().StringVar(
&tokenTenantId,
"tenant-id",
"",
"the tenant ID to associate with the token",
)
// require the tenant ID
tokenCreateAPICmd.MarkPersistentFlagRequired("tenant-id") // nolint: errcheck
tokenCreateAPICmd.PersistentFlags().StringVar(
&tokenName,
"name",
"default",
"the name of the token",
)
}
func runCreateAPIToken() error {
// read in the local config
configLoader := loader.NewConfigLoader(configDirectory)
cleanup, serverConf, err := configLoader.LoadServerConfig()
if err != nil {
return err
}
defer cleanup() // nolint:errcheck
defer serverConf.Disconnect() // nolint:errcheck
defaultTok, err := serverConf.Auth.JWTManager.GenerateTenantToken(context.Background(), tokenTenantId, tokenName)
if err != nil {
return err
}
fmt.Println(defaultTok)
return nil
}