user_ls.go

  1package commands
  2
  3import (
  4	"encoding/json"
  5	"errors"
  6	"fmt"
  7	"reflect"
  8
  9	"github.com/spf13/cobra"
 10
 11	"github.com/MichaelMure/git-bug/cache"
 12	identity2 "github.com/MichaelMure/git-bug/identity"
 13	"github.com/MichaelMure/git-bug/util/colors"
 14	"github.com/MichaelMure/git-bug/util/interrupt"
 15)
 16
 17var (
 18	userLsOutputFormat string
 19)
 20
 21func runUserLs(_ *cobra.Command, _ []string) error {
 22	backend, err := cache.NewRepoCache(repo)
 23	if err != nil {
 24		return err
 25	}
 26	defer backend.Close()
 27	interrupt.RegisterCleaner(backend.Close)
 28
 29	ids := backend.AllIdentityIds()
 30	var users []*cache.IdentityExcerpt
 31	for _, id := range ids {
 32		user, err := backend.ResolveIdentityExcerpt(id)
 33		if err != nil {
 34			return err
 35		}
 36		users = append(users, user)
 37	}
 38
 39	switch userLsOutputFormat {
 40	case "org-mode":
 41		return userLsOrgmodeFormatter(users)
 42	case "json":
 43		return userLsJsonFormatter(users)
 44	case "plain":
 45		return userLsPlainFormatter(users)
 46	case "default":
 47		return userLsDefaultFormatter(users)
 48	default:
 49		return fmt.Errorf("unknown format %s", userLsOutputFormat)
 50	}
 51}
 52
 53type JSONIdentity struct {
 54	Id      string `json:"id"`
 55	HumanId string `json:"human_id"`
 56	Name    string `json:"name"`
 57	Login   string `json:"login"`
 58}
 59
 60func NewJSONIdentity(id interface{}) (JSONIdentity, error) {
 61	switch id.(type) {
 62	case *cache.IdentityExcerpt:
 63		i := id.(*cache.IdentityExcerpt)
 64		return JSONIdentity{
 65			i.Id.String(),
 66			i.Id.Human(),
 67			i.Name,
 68			i.Login,
 69		}, nil
 70	case identity2.Interface:
 71		i := id.(identity2.Interface)
 72		return JSONIdentity{
 73			i.Id().String(),
 74			i.Id().Human(),
 75			i.Name(),
 76			i.Login(),
 77		}, nil
 78	case cache.LegacyAuthorExcerpt:
 79		i := id.(cache.LegacyAuthorExcerpt)
 80		return JSONIdentity{
 81			"",
 82			"",
 83			i.Name,
 84			i.Login,
 85		}, nil
 86	default:
 87		return JSONIdentity{}, errors.New(fmt.Sprintf("Inconvertible type, attempting to convert type %s to type %s.", reflect.TypeOf(id).String(), reflect.TypeOf(JSONIdentity{}).String()))
 88	}
 89}
 90
 91func userLsPlainFormatter(users []*cache.IdentityExcerpt) error {
 92	for _, user := range users {
 93		fmt.Printf("%s %s\n",
 94			user.Id.Human(),
 95			user.DisplayName(),
 96		)
 97	}
 98
 99	return nil
100}
101
102func userLsDefaultFormatter(users []*cache.IdentityExcerpt) error {
103	for _, user := range users {
104		fmt.Printf("%s %s\n",
105			colors.Cyan(user.Id.Human()),
106			user.DisplayName(),
107		)
108	}
109
110	return nil
111}
112
113func userLsJsonFormatter(users []*cache.IdentityExcerpt) error {
114	jsonUsers := []JSONIdentity{}
115	for _, user := range users {
116		jsonUser, err := NewJSONIdentity(user)
117		if err != nil {
118			return err
119		}
120		jsonUsers = append(jsonUsers, jsonUser)
121	}
122
123	jsonObject, _ := json.MarshalIndent(jsonUsers, "", "    ")
124	fmt.Printf("%s\n", jsonObject)
125	return nil
126}
127
128func userLsOrgmodeFormatter(users []*cache.IdentityExcerpt) error {
129	for _, user := range users {
130		fmt.Printf("* %s %s\n",
131			user.Id.Human(),
132			user.DisplayName(),
133		)
134	}
135
136	return nil
137}
138
139var userLsCmd = &cobra.Command{
140	Use:     "ls",
141	Short:   "List identities.",
142	PreRunE: loadRepo,
143	RunE:    runUserLs,
144}
145
146func init() {
147	userCmd.AddCommand(userLsCmd)
148	userLsCmd.Flags().SortFlags = false
149	userLsCmd.Flags().StringVarP(&userLsOutputFormat, "format", "f", "default",
150		"Select the output formatting style. Valid values are [default,plain,json,org-mode]")
151}