1package commands
2
3import (
4 "fmt"
5 "strings"
6
7 "github.com/spf13/cobra"
8
9 "github.com/MichaelMure/git-bug/bridge/core"
10 "github.com/MichaelMure/git-bug/cache"
11 "github.com/MichaelMure/git-bug/util/colors"
12 "github.com/MichaelMure/git-bug/util/interrupt"
13 "github.com/MichaelMure/git-bug/util/text"
14)
15
16var (
17 bridgeTokenLocal bool
18 bridgeTokenGlobal bool
19)
20
21func runTokenBridge(cmd *cobra.Command, args []string) error {
22 backend, err := cache.NewRepoCache(repo)
23 if err != nil {
24 return err
25 }
26 defer backend.Close()
27 interrupt.RegisterCleaner(backend.Close)
28
29 tokens, err := core.ListTokens(backend)
30 if err != nil {
31 return err
32 }
33
34 for token, global := range tokens {
35 // TODO: filter tokens using flags
36 getTokenFn := core.GetToken
37 if global {
38 getTokenFn = core.GetGlobalToken
39 }
40
41 token, err := getTokenFn(repo, token)
42 if err != nil {
43 return err
44 }
45 printToken(token)
46 }
47
48 return nil
49}
50
51func printToken(token *core.Token) {
52 idFmt := text.LeftPadMaxLine(token.HumanId(), 7, 0)
53 valueFmt := text.LeftPadMaxLine(token.Value, 8, 0)
54 targetFmt := text.LeftPadMaxLine(token.Target, 8, 0)
55 scopesFmt := text.LeftPadMaxLine(strings.Join(token.Scopes, ","), 20, 0)
56
57 fmt.Printf("%s %s %s %s %s\n",
58 idFmt,
59 valueFmt,
60 colors.Magenta(targetFmt),
61 colors.Yellow(token.Kind()),
62 scopesFmt,
63 )
64}
65
66var bridgeTokenCmd = &cobra.Command{
67 Use: "token",
68 Short: "List all stored tokens.",
69 PreRunE: loadRepo,
70 RunE: runTokenBridge,
71 Args: cobra.NoArgs,
72}
73
74func init() {
75 bridgeCmd.AddCommand(bridgeTokenCmd)
76 bridgeTokenCmd.Flags().BoolVarP(&bridgeTokenLocal, "local", "l", false, "")
77 bridgeTokenCmd.Flags().BoolVarP(&bridgeTokenGlobal, "global", "g", false, "")
78 bridgeTokenCmd.Flags().SortFlags = false
79}