cli: avoid misleading error message when creating an admin user

This commit is contained in:
Frédéric Guillot 2024-03-23 14:05:05 -07:00
parent ad1d349a0c
commit 3db3f9884f
4 changed files with 25 additions and 16 deletions

View file

@ -16,7 +16,7 @@ func askCredentials() (string, string) {
fd := int(os.Stdin.Fd()) fd := int(os.Stdin.Fd())
if !term.IsTerminal(fd) { if !term.IsTerminal(fd) {
printErrorAndExit(fmt.Errorf("this is not a terminal, exiting")) printErrorAndExit(fmt.Errorf("this is not an interactive terminal, exiting"))
} }
fmt.Print("Enter Username: ") fmt.Print("Enter Username: ")

View file

@ -23,7 +23,7 @@ const (
flagVersionHelp = "Show application version" flagVersionHelp = "Show application version"
flagMigrateHelp = "Run SQL migrations" flagMigrateHelp = "Run SQL migrations"
flagFlushSessionsHelp = "Flush all sessions (disconnect users)" flagFlushSessionsHelp = "Flush all sessions (disconnect users)"
flagCreateAdminHelp = "Create admin user" flagCreateAdminHelp = "Create an admin user from an interactive terminal"
flagResetPasswordHelp = "Reset user password" flagResetPasswordHelp = "Reset user password"
flagResetFeedErrorsHelp = "Clear all feed errors for all users" flagResetFeedErrorsHelp = "Clear all feed errors for all users"
flagDebugModeHelp = "Show debug logs" flagDebugModeHelp = "Show debug logs"
@ -191,7 +191,7 @@ func Parse() {
} }
if flagCreateAdmin { if flagCreateAdmin {
createAdmin(store) createAdminUserFromInteractiveTerminal(store)
return return
} }
@ -211,9 +211,8 @@ func Parse() {
printErrorAndExit(err) printErrorAndExit(err)
} }
// Create admin user and start the daemon.
if config.Opts.CreateAdmin() { if config.Opts.CreateAdmin() {
createAdmin(store) createAdminUserFromEnvironmentVariables(store)
} }
if flagRefreshFeeds { if flagRefreshFeeds {

View file

@ -12,15 +12,20 @@ import (
"miniflux.app/v2/internal/validator" "miniflux.app/v2/internal/validator"
) )
func createAdmin(store *storage.Storage) { func createAdminUserFromEnvironmentVariables(store *storage.Storage) {
userCreationRequest := &model.UserCreationRequest{ createAdminUser(store, config.Opts.AdminUsername(), config.Opts.AdminPassword())
Username: config.Opts.AdminUsername(), }
Password: config.Opts.AdminPassword(),
IsAdmin: true,
}
if userCreationRequest.Username == "" || userCreationRequest.Password == "" { func createAdminUserFromInteractiveTerminal(store *storage.Storage) {
userCreationRequest.Username, userCreationRequest.Password = askCredentials() username, password := askCredentials()
createAdminUser(store, username, password)
}
func createAdminUser(store *storage.Storage, username, password string) {
userCreationRequest := &model.UserCreationRequest{
Username: username,
Password: password,
IsAdmin: true,
} }
if store.UserExists(userCreationRequest.Username) { if store.UserExists(userCreationRequest.Username) {
@ -34,7 +39,12 @@ func createAdmin(store *storage.Storage) {
printErrorAndExit(validationErr.Error()) printErrorAndExit(validationErr.Error())
} }
if _, err := store.CreateUser(userCreationRequest); err != nil { if user, err := store.CreateUser(userCreationRequest); err != nil {
printErrorAndExit(err) printErrorAndExit(err)
} else {
slog.Info("Created new admin user",
slog.String("username", user.Username),
slog.Int64("user_id", user.ID),
)
} }
} }

View file

@ -1,5 +1,5 @@
.\" Manpage for miniflux. .\" Manpage for miniflux.
.TH "MINIFLUX" "1" "March 19, 2024" "\ \&" "\ \&" .TH "MINIFLUX" "1" "March 23, 2024" "\ \&" "\ \&"
.SH NAME .SH NAME
miniflux \- Minimalist and opinionated feed reader miniflux \- Minimalist and opinionated feed reader
@ -31,7 +31,7 @@ Load configuration file\&.
.PP .PP
.B \-create-admin .B \-create-admin
.RS 4 .RS 4
Create admin user\&. Create an admin user from an interactive terminal\&.
.RE .RE
.PP .PP
.B \-debug .B \-debug