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)
13
14func newBridgeAuthShow() *cobra.Command {
15 env := newEnv()
16
17 cmd := &cobra.Command{
18 Use: "show",
19 Short: "Display an authentication credential.",
20 PreRunE: loadBackend(env),
21 RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
22 return runBridgeAuthShow(env, args)
23 }),
24 Args: cobra.ExactArgs(1),
25 }
26
27 return cmd
28}
29
30func runBridgeAuthShow(env *Env, args []string) error {
31 cred, err := auth.LoadWithPrefix(env.repo, args[0])
32 if err != nil {
33 return err
34 }
35
36 env.out.Printf("Id: %s\n", cred.ID())
37 env.out.Printf("Target: %s\n", cred.Target())
38 env.out.Printf("Kind: %s\n", cred.Kind())
39 env.out.Printf("Creation: %s\n", cred.CreateTime().Format(time.RFC822))
40
41 switch cred := cred.(type) {
42 case *auth.Token:
43 env.out.Printf("Value: %s\n", cred.Value)
44 }
45
46 env.out.Println("Metadata:")
47
48 meta := make([]string, 0, len(cred.Metadata()))
49 for key, value := range cred.Metadata() {
50 meta = append(meta, fmt.Sprintf(" %s --> %s\n", key, value))
51 }
52 sort.Strings(meta)
53
54 env.out.Print(strings.Join(meta, ""))
55
56 return nil
57}