aboutsummaryrefslogtreecommitdiffstats
path: root/commands/user_ls.go
diff options
context:
space:
mode:
Diffstat (limited to 'commands/user_ls.go')
-rw-r--r--commands/user_ls.go69
1 files changed, 35 insertions, 34 deletions
diff --git a/commands/user_ls.go b/commands/user_ls.go
index b3fb32e6..05d343d6 100644
--- a/commands/user_ls.go
+++ b/commands/user_ls.go
@@ -8,44 +8,59 @@ import (
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/util/colors"
- "github.com/MichaelMure/git-bug/util/interrupt"
)
-var (
- userLsOutputFormat string
-)
+type userLsOptions struct {
+ format string
+}
-func runUserLs(_ *cobra.Command, _ []string) error {
- backend, err := cache.NewRepoCache(repo)
- if err != nil {
- return err
+func newUserLsCommand() *cobra.Command {
+ env := newEnv()
+ options := userLsOptions{}
+
+ cmd := &cobra.Command{
+ Use: "ls",
+ Short: "List identities.",
+ PreRunE: loadBackend(env),
+ PostRunE: closeBackend(env),
+ RunE: func(cmd *cobra.Command, args []string) error {
+ return runUserLs(env, options)
+ },
}
- defer backend.Close()
- interrupt.RegisterCleaner(backend.Close)
- ids := backend.AllIdentityIds()
+ 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 := backend.ResolveIdentityExcerpt(id)
+ user, err := env.backend.ResolveIdentityExcerpt(id)
if err != nil {
return err
}
users = append(users, user)
}
- switch userLsOutputFormat {
+ switch opts.format {
case "json":
- return userLsJsonFormatter(users)
+ return userLsJsonFormatter(env, users)
case "default":
- return userLsDefaultFormatter(users)
+ return userLsDefaultFormatter(env, users)
default:
- return fmt.Errorf("unknown format %s", userLsOutputFormat)
+ return fmt.Errorf("unknown format %s", opts.format)
}
}
-func userLsDefaultFormatter(users []*cache.IdentityExcerpt) error {
+func userLsDefaultFormatter(env *Env, users []*cache.IdentityExcerpt) error {
for _, user := range users {
- fmt.Printf("%s %s\n",
+ env.out.Printf("%s %s\n",
colors.Cyan(user.Id.Human()),
user.DisplayName(),
)
@@ -54,27 +69,13 @@ func userLsDefaultFormatter(users []*cache.IdentityExcerpt) error {
return nil
}
-func userLsJsonFormatter(users []*cache.IdentityExcerpt) error {
+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, "", " ")
- fmt.Printf("%s\n", jsonObject)
+ env.out.Printf("%s\n", jsonObject)
return nil
}
-
-var userLsCmd = &cobra.Command{
- Use: "ls",
- Short: "List identities.",
- PreRunE: loadRepo,
- RunE: runUserLs,
-}
-
-func init() {
- userCmd.AddCommand(userLsCmd)
- userLsCmd.Flags().SortFlags = false
- userLsCmd.Flags().StringVarP(&userLsOutputFormat, "format", "f", "default",
- "Select the output formatting style. Valid values are [default,json]")
-}