2019-02-24 16:17:52 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2020-06-21 08:51:48 +03:00
|
|
|
"encoding/json"
|
2019-02-24 16:17:52 +03:00
|
|
|
"fmt"
|
2020-06-24 05:22:32 +03:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2019-02-24 16:17:52 +03:00
|
|
|
"github.com/MichaelMure/git-bug/cache"
|
|
|
|
"github.com/MichaelMure/git-bug/util/colors"
|
|
|
|
)
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
type userLsOptions struct {
|
|
|
|
format string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newUserLsCommand() *cobra.Command {
|
|
|
|
env := newEnv()
|
|
|
|
options := userLsOptions{}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2021-05-09 12:33:20 +03:00
|
|
|
Use: "ls",
|
|
|
|
Short: "List identities.",
|
|
|
|
PreRunE: loadBackend(env),
|
|
|
|
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
|
2020-06-28 19:26:29 +03:00
|
|
|
return runUserLs(env, options)
|
2021-05-09 12:33:20 +03:00
|
|
|
}),
|
2020-06-28 19:26:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.SortFlags = false
|
|
|
|
|
|
|
|
flags.StringVarP(&options.format, "format", "f", "default",
|
|
|
|
"Select the output formatting style. Valid values are [default,json]")
|
2021-01-24 16:05:40 +03:00
|
|
|
cmd.RegisterFlagCompletionFunc("format", completeFrom([]string{"default", "json"}))
|
2020-06-21 08:51:48 +03:00
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runUserLs(env *Env, opts userLsOptions) error {
|
2020-06-28 20:09:32 +03:00
|
|
|
ids := env.backend.AllIdentityIds()
|
2020-06-24 05:22:32 +03:00
|
|
|
var users []*cache.IdentityExcerpt
|
|
|
|
for _, id := range ids {
|
2020-06-28 20:09:32 +03:00
|
|
|
user, err := env.backend.ResolveIdentityExcerpt(id)
|
2020-06-24 05:22:32 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
users = append(users, user)
|
|
|
|
}
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
switch opts.format {
|
2020-06-21 08:51:48 +03:00
|
|
|
case "json":
|
2020-06-28 19:26:29 +03:00
|
|
|
return userLsJsonFormatter(env, users)
|
2020-06-21 08:51:48 +03:00
|
|
|
case "default":
|
2020-06-28 19:26:29 +03:00
|
|
|
return userLsDefaultFormatter(env, users)
|
2020-06-21 08:51:48 +03:00
|
|
|
default:
|
2020-06-28 19:26:29 +03:00
|
|
|
return fmt.Errorf("unknown format %s", opts.format)
|
2020-06-21 08:51:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
func userLsDefaultFormatter(env *Env, users []*cache.IdentityExcerpt) error {
|
2020-06-24 05:22:32 +03:00
|
|
|
for _, user := range users {
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("%s %s\n",
|
2020-06-24 05:22:32 +03:00
|
|
|
colors.Cyan(user.Id.Human()),
|
|
|
|
user.DisplayName(),
|
2019-02-24 16:17:52 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
func userLsJsonFormatter(env *Env, users []*cache.IdentityExcerpt) error {
|
2020-06-24 15:52:48 +03:00
|
|
|
jsonUsers := make([]JSONIdentity, len(users))
|
|
|
|
for i, user := range users {
|
|
|
|
jsonUsers[i] = NewJSONIdentityFromExcerpt(user)
|
2020-06-21 08:51:48 +03:00
|
|
|
}
|
|
|
|
|
2020-06-24 05:22:32 +03:00
|
|
|
jsonObject, _ := json.MarshalIndent(jsonUsers, "", " ")
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("%s\n", jsonObject)
|
2020-06-21 08:51:48 +03:00
|
|
|
return nil
|
|
|
|
}
|