-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add reset subcommand for user cOCommand
Signed-off-by: Alan Tang <[email protected]>
- Loading branch information
1 parent
22a748d
commit eacc5c6
Showing
4 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package user | ||
|
||
import ( | ||
"github.com/goharbor/harbor-cli/pkg/api" | ||
"github.com/goharbor/harbor-cli/pkg/prompt" | ||
"github.com/goharbor/harbor-cli/pkg/views/user/reset" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func UserResetCmd() *cobra.Command { | ||
|
||
cmd := &cobra.Command{ | ||
Use: "reset [username]", | ||
Short: "reset user's password", | ||
Long: "reset user's password by username", | ||
Args: cobra.MaximumNArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
var err error | ||
var userId int64 | ||
resetView := &reset.ResetView{} | ||
|
||
if len(args) > 0 { | ||
userId, _ = api.GetUsersIdByName(args[0]) | ||
} else { | ||
userId = prompt.GetUserIdFromUser() | ||
} | ||
|
||
reset.ResetUserView(resetView) | ||
|
||
err = api.ResetPassword(userId, *resetView) | ||
|
||
if err != nil { | ||
log.Errorf("failed to reset user's password: %v", err) | ||
} | ||
|
||
}, | ||
} | ||
|
||
return cmd | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package reset | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
|
||
"github.com/charmbracelet/huh" | ||
"github.com/goharbor/harbor-cli/pkg/utils" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type ResetView struct { | ||
OldPassword string | ||
NewPassword string | ||
Comfirm string | ||
} | ||
|
||
func ResetUserView(resetView *ResetView) { | ||
theme := huh.ThemeCharm() | ||
err := huh.NewForm( | ||
huh.NewGroup( | ||
huh.NewInput(). | ||
Title("Old Password"). | ||
EchoMode(huh.EchoModePassword). | ||
Value(&resetView.OldPassword). | ||
Validate(func(str string) error { | ||
if strings.TrimSpace(str) == "" { | ||
return errors.New("password cannot be empty or only spaces") | ||
} | ||
if err := utils.ValidatePassword(str); err != nil { | ||
return err | ||
} | ||
return nil | ||
}), | ||
huh.NewInput(). | ||
Title("New Password"). | ||
EchoMode(huh.EchoModePassword). | ||
Value(&resetView.NewPassword). | ||
Validate(func(str string) error { | ||
if strings.TrimSpace(str) == "" { | ||
return errors.New("password cannot be empty or only spaces") | ||
} | ||
if err := utils.ValidatePassword(str); err != nil { | ||
return err | ||
} | ||
return nil | ||
}), | ||
huh.NewInput(). | ||
Title("Comfirm Password"). | ||
EchoMode(huh.EchoModePassword). | ||
Value(&resetView.Comfirm). | ||
Validate(func(str string) error { | ||
if resetView.Comfirm != resetView.NewPassword { | ||
return errors.New("passwords do not match, please try again") | ||
} | ||
return nil | ||
}), | ||
), | ||
).WithTheme(theme).Run() | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |