فهرست منبع

cli: avoid misleading error message when creating an admin user

Frédéric Guillot 2 سال پیش
والد
کامیت
3db3f9884f
4فایلهای تغییر یافته به همراه24 افزوده شده و 15 حذف شده
  1. 1 1
      internal/cli/ask_credentials.go
  2. 3 4
      internal/cli/cli.go
  3. 18 8
      internal/cli/create_admin.go
  4. 2 2
      miniflux.1

+ 1 - 1
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: ")

+ 3 - 4
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 {

+ 18 - 8
internal/cli/create_admin.go

@@ -12,17 +12,22 @@ import (
 	"miniflux.app/v2/internal/validator"
 )
 
-func createAdmin(store *storage.Storage) {
+func createAdminUserFromEnvironmentVariables(store *storage.Storage) {
+	createAdminUser(store, config.Opts.AdminUsername(), config.Opts.AdminPassword())
+}
+
+func createAdminUserFromInteractiveTerminal(store *storage.Storage) {
+	username, password := askCredentials()
+	createAdminUser(store, username, password)
+}
+
+func createAdminUser(store *storage.Storage, username, password string) {
 	userCreationRequest := &model.UserCreationRequest{
-		Username: config.Opts.AdminUsername(),
-		Password: config.Opts.AdminPassword(),
+		Username: username,
+		Password: password,
 		IsAdmin:  true,
 	}
 
-	if userCreationRequest.Username == "" || userCreationRequest.Password == "" {
-		userCreationRequest.Username, userCreationRequest.Password = askCredentials()
-	}
-
 	if store.UserExists(userCreationRequest.Username) {
 		slog.Info("Skipping admin user creation because it already exists",
 			slog.String("username", 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),
+		)
 	}
 }

+ 2 - 2
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