diff options
author | Michael Muré <batolettre@gmail.com> | 2020-06-28 18:26:29 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2020-06-28 18:26:29 +0200 |
commit | 26bd1dd11010b4d86cebe2510ad7085a6b316334 (patch) | |
tree | f1fe939311c75bd615071e96f3d37822cccd77a7 /commands/user_ls.go | |
parent | c0dbc149d5c0c3610476ba14a800c9ba803a2c2c (diff) | |
download | git-bug-26bd1dd11010b4d86cebe2510ad7085a6b316334.tar.gz |
commands: refactor to avoid globals
Diffstat (limited to 'commands/user_ls.go')
-rw-r--r-- | commands/user_ls.go | 62 |
1 files changed, 35 insertions, 27 deletions
diff --git a/commands/user_ls.go b/commands/user_ls.go index b3fb32e6..5087ebd4 100644 --- a/commands/user_ls.go +++ b/commands/user_ls.go @@ -11,12 +11,34 @@ import ( "github.com/MichaelMure/git-bug/util/interrupt" ) -var ( - userLsOutputFormat string -) +type userLsOptions struct { + format string +} + +func newUserLsCommand() *cobra.Command { + env := newEnv() + options := userLsOptions{} + + cmd := &cobra.Command{ + Use: "ls", + Short: "List identities.", + PreRunE: loadRepo(env), + RunE: 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]") -func runUserLs(_ *cobra.Command, _ []string) error { - backend, err := cache.NewRepoCache(repo) + return cmd +} + +func runUserLs(env *Env, opts userLsOptions) error { + backend, err := cache.NewRepoCache(env.repo) if err != nil { return err } @@ -33,19 +55,19 @@ func runUserLs(_ *cobra.Command, _ []string) error { 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 +76,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]") -} |