diff --git a/pkg/cmd/root/root.go b/pkg/cmd/root/root.go index d23b7e8f..0c3f8937 100644 --- a/pkg/cmd/root/root.go +++ b/pkg/cmd/root/root.go @@ -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" @@ -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, diff --git a/pkg/cmd/update/password.go b/pkg/cmd/update/password.go new file mode 100644 index 00000000..8d7545d4 --- /dev/null +++ b/pkg/cmd/update/password.go @@ -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) +} diff --git a/pkg/cmd/update/update.go b/pkg/cmd/update/update.go new file mode 100644 index 00000000..e5f5cd03 --- /dev/null +++ b/pkg/cmd/update/update.go @@ -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 + `, +} diff --git a/pkg/types/api_types.go b/pkg/types/api_types.go index e04581c7..ad60cd46 100644 --- a/pkg/types/api_types.go +++ b/pkg/types/api_types.go @@ -41,3 +41,9 @@ type Credentials struct { Endpoint string ServerEndpoint string } + +type UpdatePasswordInput struct { + Username string `json:"username,omitempty"` + OldPassword string + NewPassword string +}