add users-manager command

This commit is contained in:
eleijonmarck
2022-07-15 17:10:34 +01:00
parent 13b23fd512
commit 4da2c77179
2 changed files with 66 additions and 0 deletions
+12
View File
@@ -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{
@@ -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
}
}