git-bug/commands/bridge_auth.go

69 lines
1.3 KiB
Go
Raw Normal View History

2019-09-17 15:11:16 +03:00
package commands
import (
2020-02-05 00:05:34 +03:00
"sort"
"strings"
2019-09-17 15:11:16 +03:00
"github.com/spf13/cobra"
text "github.com/MichaelMure/go-term-text"
2019-09-17 15:11:16 +03:00
"github.com/MichaelMure/git-bug/bridge/core/auth"
"github.com/MichaelMure/git-bug/util/colors"
2019-09-17 15:11:16 +03:00
)
2020-06-28 19:26:29 +03:00
func newBridgeAuthCommand() *cobra.Command {
env := newEnv()
cmd := &cobra.Command{
Use: "auth",
Short: "List all known bridge authentication credentials.",
PreRunE: loadBackend(env),
PostRunE: closeBackend(env),
2020-06-28 19:26:29 +03:00
RunE: func(cmd *cobra.Command, args []string) error {
return runBridgeAuth(env)
},
Args: cobra.NoArgs,
}
cmd.AddCommand(newBridgeAuthAddTokenCommand())
cmd.AddCommand(newBridgeAuthRm())
cmd.AddCommand(newBridgeAuthShow())
return cmd
}
func runBridgeAuth(env *Env) error {
creds, err := auth.List(env.backend)
if err != nil {
return err
}
for _, cred := range creds {
targetFmt := text.LeftPadMaxLine(cred.Target(), 10, 0)
var value string
switch cred := cred.(type) {
case *auth.Token:
value = cred.Value
}
2020-02-05 00:05:34 +03:00
meta := make([]string, 0, len(cred.Metadata()))
for k, v := range cred.Metadata() {
meta = append(meta, k+":"+v)
}
2020-02-05 00:05:34 +03:00
sort.Strings(meta)
metaFmt := strings.Join(meta, ",")
2019-09-17 15:11:16 +03:00
2020-06-28 19:26:29 +03:00
env.out.Printf("%s %s %s %s %s\n",
colors.Cyan(cred.ID().Human()),
colors.Yellow(targetFmt),
colors.Magenta(cred.Kind()),
value,
2020-02-05 00:05:34 +03:00
metaFmt,
)
}
return nil
}