(ctx context.Context, dataDirPath string, opts *ResetPasswordOptions)
| 67 | } |
| 68 | |
| 69 | func ResetPassword(ctx context.Context, dataDirPath string, opts *ResetPasswordOptions) error { |
| 70 | path.FormatAllPath(dataDirPath) |
| 71 | |
| 72 | config, err := conf.ReadConfig(path.GetConfigFilePath()) |
| 73 | if err != nil { |
| 74 | return fmt.Errorf("read config file failed: %w", err) |
| 75 | } |
| 76 | |
| 77 | db, err := initDatabase(config.Data.Database.Driver, config.Data.Database.Connection) |
| 78 | if err != nil { |
| 79 | return fmt.Errorf("connect database failed: %w", err) |
| 80 | } |
| 81 | defer func() { |
| 82 | _ = db.Close() |
| 83 | }() |
| 84 | |
| 85 | cache, cacheCleanup, err := data.NewCache(config.Data.Cache) |
| 86 | if err != nil { |
| 87 | return fmt.Errorf("initialize cache failed: %w", err) |
| 88 | } |
| 89 | defer cacheCleanup() |
| 90 | |
| 91 | dataData, dataCleanup, err := data.NewData(db, cache) |
| 92 | if err != nil { |
| 93 | return fmt.Errorf("initialize data layer failed: %w", err) |
| 94 | } |
| 95 | defer dataCleanup() |
| 96 | |
| 97 | userRepo := user.NewUserRepo(dataData) |
| 98 | authRepo := auth.NewAuthRepo(dataData) |
| 99 | apiKeyRepo := api_key.NewAPIKeyRepo(dataData) |
| 100 | authSvc := authService.NewAuthService(authRepo, apiKeyRepo) |
| 101 | |
| 102 | email := strings.TrimSpace(opts.Email) |
| 103 | if email == "" { |
| 104 | reader := bufio.NewReader(os.Stdin) |
| 105 | fmt.Print("Please input user email: ") |
| 106 | emailInput, err := reader.ReadString('\n') |
| 107 | if err != nil { |
| 108 | return fmt.Errorf("read email input failed: %w", err) |
| 109 | } |
| 110 | email = strings.TrimSpace(emailInput) |
| 111 | } |
| 112 | |
| 113 | userInfo, exist, err := userRepo.GetByEmail(ctx, email) |
| 114 | if err != nil { |
| 115 | return fmt.Errorf("query user failed: %w", err) |
| 116 | } |
| 117 | if !exist { |
| 118 | return fmt.Errorf("user not found: %s", email) |
| 119 | } |
| 120 | |
| 121 | fmt.Printf("You are going to reset password for user: %s\n", email) |
| 122 | |
| 123 | password := strings.TrimSpace(opts.Password) |
| 124 | |
| 125 | if password != "" { |
| 126 | printWarning("Passing password via command line may be recorded in shell history") |
no test coverage detected