From 3db3f9884fc06a03256a8c9c19880cb4b46e9a26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Sat, 23 Mar 2024 14:05:05 -0700 Subject: [PATCH] cli: avoid misleading error message when creating an admin user --- internal/cli/ask_credentials.go | 2 +- internal/cli/cli.go | 7 +++---- internal/cli/create_admin.go | 28 +++++++++++++++++++--------- miniflux.1 | 4 ++-- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/internal/cli/ask_credentials.go b/internal/cli/ask_credentials.go index 1c1e2c12..a4264a9b 100644 --- a/internal/cli/ask_credentials.go +++ b/internal/cli/ask_credentials.go @@ -16,7 +16,7 @@ func askCredentials() (string, string) { fd := int(os.Stdin.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: ") diff --git a/internal/cli/cli.go b/internal/cli/cli.go index a1ef7eef..f56bb959 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -23,7 +23,7 @@ const ( flagVersionHelp = "Show application version" flagMigrateHelp = "Run SQL migrations" flagFlushSessionsHelp = "Flush all sessions (disconnect users)" - flagCreateAdminHelp = "Create admin user" + flagCreateAdminHelp = "Create an admin user from an interactive terminal" flagResetPasswordHelp = "Reset user password" flagResetFeedErrorsHelp = "Clear all feed errors for all users" flagDebugModeHelp = "Show debug logs" @@ -191,7 +191,7 @@ func Parse() { } if flagCreateAdmin { - createAdmin(store) + createAdminUserFromInteractiveTerminal(store) return } @@ -211,9 +211,8 @@ func Parse() { printErrorAndExit(err) } - // Create admin user and start the daemon. if config.Opts.CreateAdmin() { - createAdmin(store) + createAdminUserFromEnvironmentVariables(store) } if flagRefreshFeeds { diff --git a/internal/cli/create_admin.go b/internal/cli/create_admin.go index d45073f3..f8199819 100644 --- a/internal/cli/create_admin.go +++ b/internal/cli/create_admin.go @@ -12,15 +12,20 @@ import ( "miniflux.app/v2/internal/validator" ) -func createAdmin(store *storage.Storage) { - userCreationRequest := &model.UserCreationRequest{ - Username: config.Opts.AdminUsername(), - Password: config.Opts.AdminPassword(), - IsAdmin: true, - } +func createAdminUserFromEnvironmentVariables(store *storage.Storage) { + createAdminUser(store, config.Opts.AdminUsername(), config.Opts.AdminPassword()) +} - if userCreationRequest.Username == "" || userCreationRequest.Password == "" { - userCreationRequest.Username, userCreationRequest.Password = askCredentials() +func createAdminUserFromInteractiveTerminal(store *storage.Storage) { + 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) { @@ -34,7 +39,12 @@ func createAdmin(store *storage.Storage) { printErrorAndExit(validationErr.Error()) } - if _, err := store.CreateUser(userCreationRequest); err != nil { + if user, err := store.CreateUser(userCreationRequest); err != nil { printErrorAndExit(err) + } else { + slog.Info("Created new admin user", + slog.String("username", user.Username), + slog.Int64("user_id", user.ID), + ) } } diff --git a/miniflux.1 b/miniflux.1 index 37aba644..7d36803c 100644 --- a/miniflux.1 +++ b/miniflux.1 @@ -1,5 +1,5 @@ .\" Manpage for miniflux. -.TH "MINIFLUX" "1" "March 19, 2024" "\ \&" "\ \&" +.TH "MINIFLUX" "1" "March 23, 2024" "\ \&" "\ \&" .SH NAME miniflux \- Minimalist and opinionated feed reader @@ -31,7 +31,7 @@ Load configuration file\&. .PP .B \-create-admin .RS 4 -Create admin user\&. +Create an admin user from an interactive terminal\&. .RE .PP .B \-debug