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"
|
|
|
|
"github.com/MichaelMure/git-bug/util/interrupt"
|
|
|
|
)
|
|
|
|
|
2020-06-21 08:51:48 +03:00
|
|
|
var (
|
|
|
|
userLsOutputFormat string
|
|
|
|
)
|
|
|
|
|
|
|
|
func runUserLs(_ *cobra.Command, _ []string) error {
|
2019-02-24 16:17:52 +03:00
|
|
|
backend, err := cache.NewRepoCache(repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer backend.Close()
|
|
|
|
interrupt.RegisterCleaner(backend.Close)
|
|
|
|
|
2020-06-24 05:22:32 +03:00
|
|
|
ids := backend.AllIdentityIds()
|
|
|
|
var users []*cache.IdentityExcerpt
|
|
|
|
for _, id := range ids {
|
|
|
|
user, err := backend.ResolveIdentityExcerpt(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
users = append(users, user)
|
|
|
|
}
|
|
|
|
|
2020-06-21 08:51:48 +03:00
|
|
|
switch userLsOutputFormat {
|
|
|
|
case "json":
|
2020-06-24 05:22:32 +03:00
|
|
|
return userLsJsonFormatter(users)
|
2020-06-21 08:51:48 +03:00
|
|
|
case "default":
|
2020-06-24 05:22:32 +03:00
|
|
|
return userLsDefaultFormatter(users)
|
2020-06-21 08:51:48 +03:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown format %s", userLsOutputFormat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 05:22:32 +03:00
|
|
|
func userLsDefaultFormatter(users []*cache.IdentityExcerpt) error {
|
|
|
|
for _, user := range users {
|
2019-02-24 16:17:52 +03:00
|
|
|
fmt.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-24 05:22:32 +03:00
|
|
|
func userLsJsonFormatter(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-21 08:51:48 +03:00
|
|
|
fmt.Printf("%s\n", jsonObject)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-24 16:17:52 +03:00
|
|
|
var userLsCmd = &cobra.Command{
|
|
|
|
Use: "ls",
|
2019-02-24 16:46:08 +03:00
|
|
|
Short: "List identities.",
|
2019-02-24 16:17:52 +03:00
|
|
|
PreRunE: loadRepo,
|
|
|
|
RunE: runUserLs,
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
userCmd.AddCommand(userLsCmd)
|
|
|
|
userLsCmd.Flags().SortFlags = false
|
2020-06-21 08:51:48 +03:00
|
|
|
userLsCmd.Flags().StringVarP(&userLsOutputFormat, "format", "f", "default",
|
2020-06-24 15:52:48 +03:00
|
|
|
"Select the output formatting style. Valid values are [default,json]")
|
2019-02-24 16:17:52 +03:00
|
|
|
}
|