56 lines
1.5 KiB
Swift
56 lines
1.5 KiB
Swift
import DatabaseClientLive
|
|
import Dependencies
|
|
import FluentSQLiteDriver
|
|
import SharedModels
|
|
import Vapor
|
|
|
|
struct GenerateAdminUserCommand: AsyncCommand {
|
|
|
|
struct Signature: CommandSignature {
|
|
@Option(name: "username", short: "u")
|
|
var userame: String?
|
|
|
|
@Option(name: "email", short: "e")
|
|
var email: String?
|
|
|
|
@Option(name: "password", short: "p")
|
|
var password: String?
|
|
|
|
}
|
|
|
|
var help: String {
|
|
"Generate admin user in the database."
|
|
}
|
|
|
|
func run(using context: CommandContext, signature: Signature) async throws {
|
|
let database = DatabaseClient.live(database: context.application.db(.sqlite))
|
|
|
|
let username = signature.userame ?? Environment.get("ADMIN_USERNAME")
|
|
guard let username else {
|
|
throw Abort(.badRequest, reason: "Username not provided or found in the environment.")
|
|
}
|
|
|
|
let email = signature.email ?? Environment.get("ADMIN_EMAIL")
|
|
guard let email else {
|
|
throw Abort(.badRequest, reason: "Email not provided or found in the environment.")
|
|
}
|
|
|
|
let password = signature.password ?? Environment.get("ADMIN_PASSWORD")
|
|
guard let password else {
|
|
throw Abort(.badRequest, reason: "Password not provided or found in the environment.")
|
|
}
|
|
|
|
let adminUser = User.Create(
|
|
username: username,
|
|
email: email,
|
|
password: password,
|
|
confirmPassword: password
|
|
)
|
|
|
|
_ = try await database.users.create(adminUser)
|
|
|
|
context.console.print("Generated admin user: \(adminUser.username)")
|
|
}
|
|
|
|
}
|