user_show.go

  1package usercmd
  2
  3import (
  4	"errors"
  5	"fmt"
  6	"strings"
  7
  8	"github.com/spf13/cobra"
  9
 10	"github.com/MichaelMure/git-bug/cache"
 11	"github.com/MichaelMure/git-bug/commands/completion"
 12	"github.com/MichaelMure/git-bug/commands/execenv"
 13)
 14
 15type userShowOptions struct {
 16	fields string
 17}
 18
 19func newUserShowCommand(env *execenv.Env) *cobra.Command {
 20	options := userShowOptions{}
 21
 22	cmd := &cobra.Command{
 23		Use:     "user show [USER_ID]",
 24		Short:   "Display a user identity",
 25		PreRunE: execenv.LoadBackendEnsureUser(env),
 26		RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
 27			return runUserShow(env, options, args)
 28		}),
 29		ValidArgsFunction: completion.User(env),
 30	}
 31
 32	flags := cmd.Flags()
 33	flags.SortFlags = false
 34
 35	fields := []string{"email", "humanId", "id", "lastModification", "lastModificationLamports", "login", "metadata", "name"}
 36	flags.StringVarP(&options.fields, "field", "f", "",
 37		"Select field to display. Valid values are ["+strings.Join(fields, ",")+"]")
 38	cmd.RegisterFlagCompletionFunc("field", completion.From(fields))
 39
 40	return cmd
 41}
 42
 43func runUserShow(env *execenv.Env, opts userShowOptions, args []string) error {
 44	if len(args) > 1 {
 45		return errors.New("only one identity can be displayed at a time")
 46	}
 47
 48	var id *cache.IdentityCache
 49	var err error
 50	if len(args) == 1 {
 51		id, err = env.Backend.Identities().ResolvePrefix(args[0])
 52	} else {
 53		id, err = env.Backend.GetUserIdentity()
 54	}
 55
 56	if err != nil {
 57		return err
 58	}
 59
 60	if opts.fields != "" {
 61		switch opts.fields {
 62		case "email":
 63			env.Out.Printf("%s\n", id.Email())
 64		case "login":
 65			env.Out.Printf("%s\n", id.Login())
 66		case "humanId":
 67			env.Out.Printf("%s\n", id.Id().Human())
 68		case "id":
 69			env.Out.Printf("%s\n", id.Id())
 70		case "lastModification":
 71			env.Out.Printf("%s\n", id.LastModification().
 72				Time().Format("Mon Jan 2 15:04:05 2006 +0200"))
 73		case "lastModificationLamport":
 74			for name, t := range id.LastModificationLamports() {
 75				env.Out.Printf("%s\n%d\n", name, t)
 76			}
 77		case "metadata":
 78			for key, value := range id.ImmutableMetadata() {
 79				env.Out.Printf("%s\n%s\n", key, value)
 80			}
 81		case "name":
 82			env.Out.Printf("%s\n", id.Name())
 83
 84		default:
 85			return fmt.Errorf("\nUnsupported field: %s\n", opts.fields)
 86		}
 87
 88		return nil
 89	}
 90
 91	env.Out.Printf("Id: %s\n", id.Id())
 92	env.Out.Printf("Name: %s\n", id.Name())
 93	env.Out.Printf("Email: %s\n", id.Email())
 94	env.Out.Printf("Login: %s\n", id.Login())
 95	env.Out.Printf("Last modification: %s\n", id.LastModification().Time().Format("Mon Jan 2 15:04:05 2006 +0200"))
 96	env.Out.Printf("Last moditication (lamport):\n")
 97	for name, t := range id.LastModificationLamports() {
 98		env.Out.Printf("\t%s: %d", name, t)
 99	}
100	env.Out.Println("Metadata:")
101	for key, value := range id.ImmutableMetadata() {
102		env.Out.Printf("    %s --> %s\n", key, value)
103	}
104	// env.Out.Printf("Protected: %v\n", id.IsProtected())
105
106	return nil
107}