bridge_auth_show.go

 1package commands
 2
 3import (
 4	"fmt"
 5	"time"
 6
 7	"github.com/spf13/cobra"
 8
 9	"github.com/MichaelMure/git-bug/bridge/core/auth"
10	"github.com/MichaelMure/git-bug/cache"
11	"github.com/MichaelMure/git-bug/util/colors"
12	"github.com/MichaelMure/git-bug/util/interrupt"
13)
14
15func runBridgeAuthShow(cmd *cobra.Command, args []string) error {
16	backend, err := cache.NewRepoCache(repo)
17	if err != nil {
18		return err
19	}
20	defer backend.Close()
21	interrupt.RegisterCleaner(backend.Close)
22
23	cred, err := auth.LoadWithPrefix(repo, args[0])
24	if err != nil {
25		return err
26	}
27
28	var userFmt string
29
30	switch cred.UserId() {
31	case auth.DefaultUserId:
32		userFmt = colors.Red("default user")
33	default:
34		user, err := backend.ResolveIdentity(cred.UserId())
35		if err != nil {
36			return err
37		}
38		userFmt = user.DisplayName()
39
40		defaultUser, _ := backend.GetUserIdentity()
41		if cred.UserId() == defaultUser.Id() {
42			userFmt = colors.Red(userFmt)
43		}
44	}
45
46	fmt.Printf("Id: %s\n", cred.ID())
47	fmt.Printf("Target: %s\n", cred.Target())
48	fmt.Printf("Kind: %s\n", cred.Kind())
49	fmt.Printf("User: %s\n", userFmt)
50	fmt.Printf("Creation: %s\n", cred.CreateTime().Format(time.RFC822))
51
52	switch cred := cred.(type) {
53	case *auth.Token:
54		fmt.Printf("Value: %s\n", cred.Value)
55	}
56
57	return nil
58}
59
60var bridgeAuthShowCmd = &cobra.Command{
61	Use:     "show",
62	Short:   "Display an authentication credential.",
63	PreRunE: loadRepo,
64	RunE:    runBridgeAuthShow,
65	Args:    cobra.ExactArgs(1),
66}
67
68func init() {
69	bridgeAuthCmd.AddCommand(bridgeAuthShowCmd)
70}