From 76bb7a55d5da472e6076cd2f792f3029fc94e10d Mon Sep 17 00:00:00 2001 From: louis Date: Thu, 2 May 2024 11:47:48 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8Add=20command=20to=20change=20user=20p?= =?UTF-8?q?assword?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/user.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/cmd/user.go b/cmd/user.go index f4ffe25b..ba76dcaa 100644 --- a/cmd/user.go +++ b/cmd/user.go @@ -71,6 +71,31 @@ var ( fmt.Printf("Deleted user %s\n", email) }, } + + userChangePasswordCmd = &cobra.Command{ + Use: "password", + Short: "Change a user's password", + Run: func(cmd *cobra.Command, args []string) { + if email == "" { + log.Fatal("email is required") + } + if password == "" { + log.Fatal("password is required") + } + + user, err := store.FindUserByEmail(email) + if err != nil { + log.WithError(err).Fatal("could not find user") + } + + user.UpdatePassword(password) + if err := store.SaveUser(&user); err != nil { + log.WithError(err).Fatal("could not save user") + } + + fmt.Printf("Changed password for user %s\n", email) + }, + } ) func init() { @@ -81,4 +106,8 @@ func init() { userCmd.AddCommand(userDeleteCmd) userDeleteCmd.Flags().StringVar(&email, "email", "", "email address of the user") + + userCmd.AddCommand(userChangePasswordCmd) + userChangePasswordCmd.Flags().StringVar(&email, "email", "", "email address of the user") + userChangePasswordCmd.Flags().StringVar(&password, "password", "", "new password of the user") }