Simplify create-account (#1608)

This commit is contained in:
Neil Alexander 2020-12-03 10:55:17 +00:00 committed by GitHub
parent 1f3a498601
commit ec7a0e42ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,24 +20,27 @@ import (
"fmt" "fmt"
"os" "os"
"github.com/matrix-org/dendrite/setup"
"github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/setup/config"
"github.com/matrix-org/dendrite/userapi/storage/accounts" "github.com/matrix-org/dendrite/userapi/storage/accounts"
"github.com/matrix-org/gomatrixserverlib" "github.com/sirupsen/logrus"
) )
const usage = `Usage: %s const usage = `Usage: %s
Generate a new Matrix account for testing purposes. Creates a new user account on the homeserver.
Example:
./create-account --config dendrite.yaml --username alice --password foobarbaz
Arguments: Arguments:
` `
var ( var (
database = flag.String("database", "", "The location of the account database.") username = flag.String("username", "", "The username of the account to register (specify the localpart only, e.g. 'alice' for '@alice:domain.com')")
username = flag.String("username", "", "The user ID localpart to register e.g 'alice' in '@alice:localhost'.") password = flag.String("password", "", "The password to associate with the account (optional, account will be password-less if not specified)")
password = flag.String("password", "", "Optional. The password to register with. If not specified, this account will be password-less.")
serverNameStr = flag.String("servername", "localhost", "The Matrix server domain which will form the domain part of the user ID.")
) )
func main() { func main() {
@ -45,36 +48,24 @@ func main() {
fmt.Fprintf(os.Stderr, usage, os.Args[0]) fmt.Fprintf(os.Stderr, usage, os.Args[0])
flag.PrintDefaults() flag.PrintDefaults()
} }
cfg := setup.ParseFlags(true)
flag.Parse()
if *username == "" { if *username == "" {
flag.Usage() flag.Usage()
fmt.Println("Missing --username")
os.Exit(1) os.Exit(1)
} }
if *database == "" {
flag.Usage()
fmt.Println("Missing --database")
os.Exit(1)
}
serverName := gomatrixserverlib.ServerName(*serverNameStr)
accountDB, err := accounts.NewDatabase(&config.DatabaseOptions{ accountDB, err := accounts.NewDatabase(&config.DatabaseOptions{
ConnectionString: config.DataSource(*database), ConnectionString: cfg.UserAPI.AccountDatabase.ConnectionString,
}, serverName) }, cfg.Global.ServerName)
if err != nil { if err != nil {
fmt.Println(err.Error()) logrus.Fatalln("Failed to connect to the database:", err.Error())
os.Exit(1)
} }
_, err = accountDB.CreateAccount(context.Background(), *username, *password, "") _, err = accountDB.CreateAccount(context.Background(), *username, *password, "")
if err != nil { if err != nil {
fmt.Println(err.Error()) logrus.Fatalln("Failed to create the account:", err.Error())
os.Exit(1)
} }
fmt.Println("Created account") logrus.Infoln("Created account", *username)
} }