bridge_auth_show.go

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