diff options
author | Michael Muré <batolettre@gmail.com> | 2022-11-22 14:53:15 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-22 14:53:15 +0100 |
commit | 70bd7377b6362127794f3a6198dd2c63863025fc (patch) | |
tree | e159372673104ade1f15ddc1a84aa9da93e93552 /commands/user_ls.go | |
parent | a3fa445a9c76631c4cd16f93e1c1c68a954adef7 (diff) | |
parent | acc9a6f3a6df2961c3ae44352216d915cb9b5315 (diff) | |
download | git-bug-70bd7377b6362127794f3a6198dd2c63863025fc.tar.gz |
Merge pull request #870 from MichaelMure/cli-reorg
commands: reorg into different packages
Diffstat (limited to 'commands/user_ls.go')
-rw-r--r-- | commands/user_ls.go | 81 |
1 files changed, 0 insertions, 81 deletions
diff --git a/commands/user_ls.go b/commands/user_ls.go deleted file mode 100644 index 341f0dc1..00000000 --- a/commands/user_ls.go +++ /dev/null @@ -1,81 +0,0 @@ -package commands - -import ( - "encoding/json" - "fmt" - - "github.com/spf13/cobra" - - "github.com/MichaelMure/git-bug/cache" - "github.com/MichaelMure/git-bug/util/colors" -) - -type userLsOptions struct { - format string -} - -func newUserLsCommand() *cobra.Command { - env := newEnv() - options := userLsOptions{} - - cmd := &cobra.Command{ - Use: "ls", - Short: "List identities.", - PreRunE: loadBackend(env), - RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error { - return runUserLs(env, options) - }), - } - - flags := cmd.Flags() - flags.SortFlags = false - - flags.StringVarP(&options.format, "format", "f", "default", - "Select the output formatting style. Valid values are [default,json]") - cmd.RegisterFlagCompletionFunc("format", completeFrom([]string{"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 := env.backend.ResolveIdentityExcerpt(id) - if err != nil { - return err - } - users = append(users, user) - } - - switch opts.format { - case "json": - return userLsJsonFormatter(env, users) - case "default": - return userLsDefaultFormatter(env, users) - default: - return fmt.Errorf("unknown format %s", opts.format) - } -} - -func userLsDefaultFormatter(env *Env, users []*cache.IdentityExcerpt) error { - for _, user := range users { - env.out.Printf("%s %s\n", - colors.Cyan(user.Id.Human()), - user.DisplayName(), - ) - } - - return nil -} - -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, "", " ") - env.out.Printf("%s\n", jsonObject) - return nil -} |