From 3838a59fa1251dff278d5d6c75f52d6354b838af Mon Sep 17 00:00:00 2001 From: eleijonmarck Date: Wed, 20 Jul 2022 13:57:50 +0100 Subject: [PATCH] refactor: imports and renaming --- .../commands/conflict_user_command.go | 50 +++++++++++++++++-- .../commands/conflict_user_command_test.go | 6 ++- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/pkg/cmd/grafana-cli/commands/conflict_user_command.go b/pkg/cmd/grafana-cli/commands/conflict_user_command.go index ec5503b1462..286117771b9 100644 --- a/pkg/cmd/grafana-cli/commands/conflict_user_command.go +++ b/pkg/cmd/grafana-cli/commands/conflict_user_command.go @@ -13,6 +13,7 @@ import ( "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/db" "github.com/grafana/grafana/pkg/services/sqlstore/migrations" @@ -91,16 +92,16 @@ func runConflictingUsersCommand() func(context *cli.Context) error { return fmt.Errorf("%v: %w", "failed to initialize SQL store", err) } - users, err := GetUsersWithConflictingEmailsOrLogins(context.Context, sqlStore) + conflicts, err := GetUsersWithConflictingEmailsOrLogins(context.Context, sqlStore) if err != nil { return fmt.Errorf("%v: %w", "failed to get users with conflicting logins", err) } - if len(users) < 1 { + if len(conflicts) < 1 { logger.Info(color.GreenString("No Conflicting users found.\n\n")) return nil } - for _, cUser := range users { + for _, cUser := range conflicts { logger.Infof("A user conflict found. \n") cType := cUser.Conflict() @@ -166,6 +167,46 @@ func confirm() bool { } +func promptToMerge(cUser ConflictingUsers) (int64, error) { + logger.Infof("Choose which user to merge into:") + scanner := bufio.NewScanner(os.Stdin) + if ok := scanner.Scan(); !ok { + if err := scanner.Err(); err != nil { + return -1, fmt.Errorf("can't read conflict option from stdin: %w", err) + } + return -1, fmt.Errorf("can't read conflict option from stdin") + } + chosenUser := scanner.Text() + if !strings.Contains(cUser.Ids, chosenUser) { + return -1, fmt.Errorf("not a conflicting user id") + } + v, err := strconv.ParseInt(chosenUser, 10, 64) + if err != nil { + return -1, fmt.Errorf("could not parse id from string") + } + return v, nil +} + +func promptSameIdentification(cUser ConflictingUsers) (int64, error) { + logger.Infof("Found same identification for users, choose which user to keep and update:") + scanner := bufio.NewScanner(os.Stdin) + if ok := scanner.Scan(); !ok { + if err := scanner.Err(); err != nil { + return -1, fmt.Errorf("can't read conflict option from stdin: %w", err) + } + return -1, fmt.Errorf("can't read conflict option from stdin") + } + chosenUser := scanner.Text() + if !strings.Contains(cUser.Ids, chosenUser) { + return -1, fmt.Errorf("not a conflicting user id") + } + v, err := strconv.ParseInt(chosenUser, 10, 64) + if err != nil { + return -1, fmt.Errorf("could not parse id from string") + } + return v, nil +} + func (c ConflictingUsers) Print() { ids := strings.Split(c.Ids, ",") emails := strings.Split(c.ConflictEmails, ",") @@ -186,7 +227,7 @@ const ( func (cUser ConflictingUsers) Conflict() conflictType { // FIXME: - // need to make sure that we get sameidentification when that happens instead of email/logins cased + // need to make sure that we get same identification when that happens instead of email/logins cased var cType conflictType if cUser.SameIdentificationConflictIds { cType = SameIdentification @@ -232,6 +273,7 @@ func GetUsersWithConflictingEmailsOrLogins(ctx context.Context, s *sqlstore.SQLS } return stats, nil } + func conflictingUserEntriesSQL(s *sqlstore.SQLStore) string { userDialect := db.DB.GetDialect(s).Quote("user") // this query counts how many users have the same login or email. diff --git a/pkg/cmd/grafana-cli/commands/conflict_user_command_test.go b/pkg/cmd/grafana-cli/commands/conflict_user_command_test.go index d1281a96144..1b8efac87d4 100644 --- a/pkg/cmd/grafana-cli/commands/conflict_user_command_test.go +++ b/pkg/cmd/grafana-cli/commands/conflict_user_command_test.go @@ -5,6 +5,8 @@ import ( "fmt" "testing" + "github.com/grafana/grafana/pkg/models" + ac "github.com/grafana/grafana/pkg/services/accesscontrol" "github.com/grafana/grafana/pkg/services/sqlstore" "github.com/grafana/grafana/pkg/services/user" "github.com/stretchr/testify/require" @@ -26,7 +28,7 @@ func TestUserManagerListConflictingUsers(t *testing.T) { require.Nil(t, err) } - // "Skipping duplicate users test for mysql as it does make unique constraint case insensitive by default + // "Skipping conflicting users test for mysql as it does make unique constraint case insensitive by default if sqlStore.GetDialect().DriverName() != "mysql" { dupUserEmailcmd := user.CreateUserCommand{ Email: "USERDUPLICATETEST1@TEST.COM", @@ -36,7 +38,7 @@ func TestUserManagerListConflictingUsers(t *testing.T) { _, err := sqlStore.CreateUser(context.Background(), dupUserEmailcmd) require.NoError(t, err) - // add additional user with duplicate login where DOMAIN is upper case + // add additional user with conflicting login where DOMAIN is upper case dupUserLogincmd := user.CreateUserCommand{ Email: "userduplicatetest1@test.com", Name: "user name 1",