git-bug/commands/bridge_auth_show.go

59 lines
1.2 KiB
Go
Raw Normal View History

2019-11-10 17:50:56 +03:00
package commands
import (
"fmt"
2020-02-05 00:05:34 +03:00
"sort"
"strings"
2019-11-10 17:50:56 +03:00
"time"
"github.com/spf13/cobra"
"github.com/MichaelMure/git-bug/bridge/core/auth"
2019-11-10 17:50:56 +03:00
)
2020-06-28 19:26:29 +03:00
func newBridgeAuthShow() *cobra.Command {
env := newEnv()
cmd := &cobra.Command{
Use: "show",
Short: "Display an authentication credential.",
PreRunE: loadBackend(env),
PostRunE: closeBackend(env),
2020-06-28 19:26:29 +03:00
RunE: func(cmd *cobra.Command, args []string) error {
return runBridgeAuthShow(env, args)
},
Args: cobra.ExactArgs(1),
}
return cmd
}
func runBridgeAuthShow(env *Env, args []string) error {
cred, err := auth.LoadWithPrefix(env.repo, args[0])
2019-11-10 17:50:56 +03:00
if err != nil {
return err
}
2020-06-28 19:26:29 +03:00
env.out.Printf("Id: %s\n", cred.ID())
env.out.Printf("Target: %s\n", cred.Target())
env.out.Printf("Kind: %s\n", cred.Kind())
env.out.Printf("Creation: %s\n", cred.CreateTime().Format(time.RFC822))
switch cred := cred.(type) {
case *auth.Token:
2020-06-28 19:26:29 +03:00
env.out.Printf("Value: %s\n", cred.Value)
}
2019-11-10 17:50:56 +03:00
2020-06-28 19:26:29 +03:00
env.out.Println("Metadata:")
2020-02-05 00:05:34 +03:00
meta := make([]string, 0, len(cred.Metadata()))
for key, value := range cred.Metadata() {
meta = append(meta, fmt.Sprintf(" %s --> %s\n", key, value))
}
sort.Strings(meta)
2020-06-28 19:26:29 +03:00
env.out.Print(strings.Join(meta, ""))
2020-02-05 00:05:34 +03:00
2019-11-10 17:50:56 +03:00
return nil
}