git-bug/commands/user_ls.go
2021-05-09 11:33:20 +02:00

81 lines
1.7 KiB
Go

package commands
import (
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/util/colors"
)
type userLsOptions struct {
format string
}
func newUserLsCommand() *cobra.Command {
env := newEnv()
options := userLsOptions{}
cmd := &cobra.Command{
Use: "ls",
Short: "List identities.",
PreRunE: loadBackend(env),
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
return runUserLs(env, options)
}),
}
flags := cmd.Flags()
flags.SortFlags = false
flags.StringVarP(&options.format, "format", "f", "default",
"Select the output formatting style. Valid values are [default,json]")
return cmd
}
func runUserLs(env *Env, opts userLsOptions) error {
ids := env.backend.AllIdentityIds()
var users []*cache.IdentityExcerpt
for _, id := range ids {
user, err := env.backend.ResolveIdentityExcerpt(id)
if err != nil {
return err
}
users = append(users, user)
}
switch opts.format {
case "json":
return userLsJsonFormatter(env, users)
case "default":
return userLsDefaultFormatter(env, users)
default:
return fmt.Errorf("unknown format %s", opts.format)
}
}
func userLsDefaultFormatter(env *Env, users []*cache.IdentityExcerpt) error {
for _, user := range users {
env.out.Printf("%s %s\n",
colors.Cyan(user.Id.Human()),
user.DisplayName(),
)
}
return nil
}
func userLsJsonFormatter(env *Env, users []*cache.IdentityExcerpt) error {
jsonUsers := make([]JSONIdentity, len(users))
for i, user := range users {
jsonUsers[i] = NewJSONIdentityFromExcerpt(user)
}
jsonObject, _ := json.MarshalIndent(jsonUsers, "", " ")
env.out.Printf("%s\n", jsonObject)
return nil
}