-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added
update password
command (#255)
* 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
1 parent
4f49849
commit 0980230
Showing
4 changed files
with
134 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,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) | ||
} |
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,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 | ||
`, | ||
} |
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