From 4da2c77179d26687d2a8135d924fe29e7302adb0 Mon Sep 17 00:00:00 2001 From: eleijonmarck Date: Fri, 15 Jul 2022 17:10:34 +0100 Subject: [PATCH] add users-manager command --- pkg/cmd/grafana-cli/commands/commands.go | 12 +++++ .../commands/duplicate_user_command.go | 54 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 pkg/cmd/grafana-cli/commands/duplicate_user_command.go diff --git a/pkg/cmd/grafana-cli/commands/commands.go b/pkg/cmd/grafana-cli/commands/commands.go index 631bdc5c744..cc06b223530 100644 --- a/pkg/cmd/grafana-cli/commands/commands.go +++ b/pkg/cmd/grafana-cli/commands/commands.go @@ -192,6 +192,18 @@ var adminCommands = []*cli.Command{ }, }, }, + { + Name: "users-manager", + Usage: "Runs different helpful user commands", + Subcommands: []*cli.Command{ + { + Name: "list-duplicates", + Usage: "list-duplicate-users returns a list of users with duplicate entries in the database. Safe to execute multiple times.", + Action: runDuplicateUsersCommand(), + }, + // TODO: reset password for user + }, + }, } var Commands = []*cli.Command{ diff --git a/pkg/cmd/grafana-cli/commands/duplicate_user_command.go b/pkg/cmd/grafana-cli/commands/duplicate_user_command.go new file mode 100644 index 00000000000..031f4a5448d --- /dev/null +++ b/pkg/cmd/grafana-cli/commands/duplicate_user_command.go @@ -0,0 +1,54 @@ +package commands + +import ( + "fmt" + + "github.com/fatih/color" + "github.com/grafana/grafana/pkg/bus" + "github.com/grafana/grafana/pkg/cmd/grafana-cli/logger" + "github.com/grafana/grafana/pkg/cmd/grafana-cli/utils" + "github.com/grafana/grafana/pkg/infra/tracing" + "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/services/sqlstore" + "github.com/grafana/grafana/pkg/services/sqlstore/migrations" + "github.com/urfave/cli/v2" +) + +// listRemoteCommand prints out all plugins in the remote repo with latest version supported on current platform. +// If there are no supported versions for plugin it is skipped. + +func runDuplicateUsersCommand() func(context *cli.Context) error { + return func(context *cli.Context) error { + cmd := &utils.ContextCommandLine{Context: context} + + cfg, err := initCfg(cmd) + if err != nil { + return fmt.Errorf("%v: %w", "failed to load configuration", err) + } + + tracer, err := tracing.ProvideService(cfg) + if err != nil { + return fmt.Errorf("%v: %w", "failed to initialize tracer service", err) + } + + bus := bus.ProvideBus(tracer) + + sqlStore, err := sqlstore.ProvideService(cfg, nil, &migrations.OSSMigrations{}, bus, tracer) + if err != nil { + return fmt.Errorf("%v: %w", "failed to initialize SQL store", err) + } + + q := models.GetApiKeysQuery{} + sqlStore.GetAPIKeys(cmd.Context.Context, &q) + + if len(q.Result) < 1 { + logger.Info(color.GreenString("No duplicate users found.\n\n")) + } + + for _, p := range q.Result { + logger.Infof("id: %v version: %s\n", p.Name, p.Key) + } + + return nil + } +}