1package commands
2
3import (
4 "fmt"
5 "sort"
6 "strings"
7 "time"
8
9 "github.com/spf13/cobra"
10
11 "github.com/MichaelMure/git-bug/bridge/core/auth"
12 "github.com/MichaelMure/git-bug/cache"
13 "github.com/MichaelMure/git-bug/util/interrupt"
14)
15
16func runBridgeAuthShow(cmd *cobra.Command, args []string) error {
17 backend, err := cache.NewRepoCache(repo)
18 if err != nil {
19 return err
20 }
21 defer backend.Close()
22 interrupt.RegisterCleaner(backend.Close)
23
24 cred, err := auth.LoadWithPrefix(repo, args[0])
25 if err != nil {
26 return err
27 }
28
29 fmt.Printf("Id: %s\n", cred.ID())
30 fmt.Printf("Target: %s\n", cred.Target())
31 fmt.Printf("Kind: %s\n", cred.Kind())
32 fmt.Printf("Creation: %s\n", cred.CreateTime().Format(time.RFC822))
33
34 switch cred := cred.(type) {
35 case *auth.Token:
36 fmt.Printf("Value: %s\n", cred.Value)
37 }
38
39 fmt.Println("Metadata:")
40
41 meta := make([]string, 0, len(cred.Metadata()))
42 for key, value := range cred.Metadata() {
43 meta = append(meta, fmt.Sprintf(" %s --> %s\n", key, value))
44 }
45 sort.Strings(meta)
46
47 fmt.Print(strings.Join(meta, ""))
48
49 return nil
50}
51
52var bridgeAuthShowCmd = &cobra.Command{
53 Use: "show",
54 Short: "Display an authentication credential.",
55 PreRunE: loadRepo,
56 RunE: runBridgeAuthShow,
57 Args: cobra.ExactArgs(1),
58}
59
60func init() {
61 bridgeAuthCmd.AddCommand(bridgeAuthShowCmd)
62}