Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added update password command #255

Merged
merged 5 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/litmuschaos/litmusctl/pkg/cmd/run"
"github.com/litmuschaos/litmusctl/pkg/cmd/save"
"github.com/litmuschaos/litmusctl/pkg/cmd/update"

"github.com/litmuschaos/litmusctl/pkg/cmd/connect"
"github.com/litmuschaos/litmusctl/pkg/cmd/delete"
Expand Down Expand Up @@ -74,6 +75,7 @@ func init() {
rootCmd.AddCommand(upgrade.UpgradeCmd)
rootCmd.AddCommand(save.SaveCmd)
rootCmd.AddCommand(run.RunCmd)
rootCmd.AddCommand(update.UpdateCmd)

// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
Expand Down
107 changes: 107 additions & 0 deletions pkg/cmd/update/password.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package update

import (
"encoding/json"
"errors"
"io"
"net/http"

"github.com/litmuschaos/litmusctl/pkg/apis"
"github.com/litmuschaos/litmusctl/pkg/types"
"github.com/litmuschaos/litmusctl/pkg/utils"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
)

var PasswordCmd = &cobra.Command{
Use: "password",
Short: `Updates an account's password.
Examples(s)
#update a user's password
litmusctl update password
`,
Run: func(cmd *cobra.Command, args []string) {
var (
updatePasswordRequest types.UpdatePasswordInput
)

credentials, err := utils.GetCredentials(cmd)
if err != nil {
utils.PrintError(err)
Baalekshan marked this conversation as resolved.
Show resolved Hide resolved
}

promptUsername := promptui.Prompt{
Label: "Username",
}
updatePasswordRequest.Username, err = promptUsername.Run()
if err != nil {
utils.PrintError(err)
}

promptOldPassword := promptui.Prompt{
Label: "Old Password",
Mask: '*',
}
updatePasswordRequest.OldPassword, err = promptOldPassword.Run()
if err != nil {
utils.PrintError(err)
}

NEW_PASSWORD:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this, you can just add comment
// New password validation

Copy link
Contributor Author

@Baalekshan Baalekshan Sep 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SarthakJain26 It was for a goto condition
if the new and confirm passwords fails, it agains runs the code under NEW_PASSWORD:
instead of the user requiring to rerun the command again


promptNewPassword := promptui.Prompt{
Label: "New Password",
Mask: '*',
}
updatePasswordRequest.NewPassword, err = promptNewPassword.Run()
if err != nil {
utils.PrintError(err)
}

promptConfirmPassword := promptui.Prompt{
Label: "Confirm New Password",
Mask: '*',
}
confirmPassword, err := promptConfirmPassword.Run()
if err != nil {
utils.PrintError(err)
}

if updatePasswordRequest.NewPassword != confirmPassword {
utils.Red.Println("\nPasswords do not match. Please try again.")
goto NEW_PASSWORD
}
payloadBytes, _ := json.Marshal(updatePasswordRequest)

resp, err := apis.SendRequest(
apis.SendRequestParams{
Endpoint: credentials.Endpoint + utils.AuthAPIPath + "/update/password/",
Token: "Bearer " + credentials.Token,
},
payloadBytes,
string(types.Post),
)
if err != nil {
utils.PrintError(err)
}
defer resp.Body.Close()

bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
utils.PrintError(err)
}

if resp.StatusCode == http.StatusOK {
utils.White_B.Println("\nPassword updated successfully!")
} else {
err := errors.New("Unmatched status code: " + string(bodyBytes))
if err != nil {
utils.Red.Println("\nError updating password: ", err)
}
}
},
}

func init() {
UpdateCmd.AddCommand(PasswordCmd)
}
16 changes: 16 additions & 0 deletions pkg/cmd/update/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package update

import (
"github.com/spf13/cobra"
)


var UpdateCmd = &cobra.Command{
Use: "update",
Short: `It updates ChaosCenter account's details.
Examples

#update password
litmusctl update password
`,
}
6 changes: 6 additions & 0 deletions pkg/types/api_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,9 @@ type Credentials struct {
Endpoint string
ServerEndpoint string
}

type UpdatePasswordInput struct {
Username string `json:"username,omitempty"`
OldPassword string
NewPassword string
}