Skip to content

Commit

Permalink
feat: added update password command (#255)
Browse files Browse the repository at this point in the history
* feat: added update password command

Signed-off-by: Baalekshan <[email protected]>

* feat: added update password command

Signed-off-by: Baalekshan <[email protected]>

* feat: added update password command

Signed-off-by: Baalekshan <[email protected]>

* feat: added update password command

Signed-off-by: Baalekshan <[email protected]>

* feat: added update password command

Signed-off-by: Baalekshan <[email protected]>

---------

Signed-off-by: Baalekshan <[email protected]>
  • Loading branch information
Baalekshan committed Sep 17, 2024
1 parent 4f49849 commit 0980230
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
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
111 changes: 111 additions & 0 deletions pkg/cmd/update/password.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package update

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

"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)
}

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:

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)
os.Exit(1)
}
defer resp.Body.Close()

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

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)
os.Exit(1)
}
}
},
}

func init() {
UpdateCmd.AddCommand(PasswordCmd)
}
15 changes: 15 additions & 0 deletions pkg/cmd/update/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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
}

0 comments on commit 0980230

Please sign in to comment.