-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cmd): add database user password update
- Loading branch information
1 parent
6649668
commit e90e9ab
Showing
3 changed files
with
108 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
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,70 @@ | ||
package users | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/Scalingo/cli/config" | ||
"github.com/Scalingo/cli/io" | ||
"github.com/Scalingo/go-scalingo/v6" | ||
"github.com/Scalingo/go-utils/errors/v2" | ||
"github.com/Scalingo/gopassword" | ||
) | ||
|
||
func UpdateUser(ctx context.Context, app, addonUUID, username string) error { | ||
isSupported, err := doesDatabaseHandleUserManagement(ctx, app, addonUUID) | ||
if err != nil { | ||
return errors.Wrap(ctx, err, "get user management information") | ||
} | ||
|
||
if !isSupported { | ||
io.Error(ErrDatabaseNotSupportUserManagement) | ||
return nil | ||
} | ||
|
||
if usernameValidation, ok := isUsernameValid(username); !ok { | ||
io.Error(usernameValidation) | ||
return nil | ||
} | ||
|
||
password, confirmedPassword, err := askForPassword(ctx) | ||
if err != nil { | ||
return errors.Wrap(ctx, err, "ask for password") | ||
} | ||
|
||
passwordValidation, ok := isPasswordValid(password, confirmedPassword) | ||
if !ok && password != "" { | ||
io.Error(passwordValidation) | ||
return nil | ||
} | ||
|
||
isPasswordGenerated := false | ||
if password == "" { | ||
isPasswordGenerated = true | ||
password = gopassword.Generate(64) | ||
confirmedPassword = password | ||
} | ||
|
||
c, err := config.ScalingoClient(ctx) | ||
if err != nil { | ||
return errors.Wrap(ctx, err, "get Scalingo client") | ||
} | ||
|
||
user := scalingo.DatabaseUpdateUserParam{ | ||
DatabaseID: addonUUID, | ||
Password: password, | ||
PasswordConfirmation: confirmedPassword, | ||
} | ||
databaseUsers, err := c.DatabaseUpdateUser(ctx, app, addonUUID, username, user) | ||
if err != nil { | ||
return errors.Wrap(ctx, err, "update password of the given database user") | ||
} | ||
|
||
if isPasswordGenerated { | ||
fmt.Printf("User \"%s\" updated with password \"%s\".\n", databaseUsers.Name, password) | ||
return nil | ||
} | ||
|
||
fmt.Printf("User \"%s\" password updated.\n", databaseUsers.Name) | ||
return nil | ||
} |