Jelajahi Sumber

refactor(cli): use cleaner syntax for common exit routine

gudvinr 5 hari lalu
induk
melakukan
f963ea35a5

+ 4 - 4
internal/cli/ask_credentials.go

@@ -25,24 +25,24 @@ func askCredentials() (string, string) {
 	reader := bufio.NewReader(os.Stdin)
 	reader := bufio.NewReader(os.Stdin)
 	username, err := reader.ReadString('\n')
 	username, err := reader.ReadString('\n')
 	if err != nil {
 	if err != nil {
-		printErrorAndExit(fmt.Errorf("unable to read username: %w", err))
+		printfAndExit("unable to read username: %w", err)
 	}
 	}
 
 
 	fmt.Print("Enter Password: ")
 	fmt.Print("Enter Password: ")
 
 
 	state, err := term.GetState(fd)
 	state, err := term.GetState(fd)
 	if err != nil {
 	if err != nil {
-		printErrorAndExit(fmt.Errorf("unable to get terminal state: %w", err))
+		printfAndExit("unable to get terminal state: %w", err)
 	}
 	}
 	defer func() {
 	defer func() {
 		if restoreErr := term.Restore(fd, state); restoreErr != nil {
 		if restoreErr := term.Restore(fd, state); restoreErr != nil {
-			printErrorAndExit(fmt.Errorf("unable to restore terminal state: %w", restoreErr))
+			printfAndExit("unable to restore terminal state: %w", restoreErr)
 		}
 		}
 	}()
 	}()
 
 
 	bytePassword, err := term.ReadPassword(fd)
 	bytePassword, err := term.ReadPassword(fd)
 	if err != nil {
 	if err != nil {
-		printErrorAndExit(fmt.Errorf("unable to read password: %w", err))
+		printfAndExit("unable to read password: %w", err)
 	}
 	}
 
 
 	fmt.Print("\n")
 	fmt.Print("\n")

+ 11 - 6
internal/cli/cli.go

@@ -124,7 +124,7 @@ func Parse() {
 	default:
 	default:
 		logFileHandler, err = os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
 		logFileHandler, err = os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
 		if err != nil {
 		if err != nil {
-			printErrorAndExit(fmt.Errorf("unable to open log file: %v", err))
+			printfAndExit("unable to open log file: %v", err)
 		}
 		}
 		defer logFileHandler.(*os.File).Close()
 		defer logFileHandler.(*os.File).Close()
 	}
 	}
@@ -143,15 +143,15 @@ func Parse() {
 	}
 	}
 
 
 	if err := static.GenerateBinaryBundles(); err != nil {
 	if err := static.GenerateBinaryBundles(); err != nil {
-		printErrorAndExit(fmt.Errorf("unable to generate binary files bundle: %v", err))
+		printfAndExit("unable to generate binary files bundle: %v", err)
 	}
 	}
 
 
 	if err := static.GenerateStylesheetsBundles(); err != nil {
 	if err := static.GenerateStylesheetsBundles(); err != nil {
-		printErrorAndExit(fmt.Errorf("unable to generate stylesheets bundle: %v", err))
+		printfAndExit("unable to generate stylesheets bundle: %v", err)
 	}
 	}
 
 
 	if err := static.GenerateJavascriptBundles(config.Opts.WebAuthn()); err != nil {
 	if err := static.GenerateJavascriptBundles(config.Opts.WebAuthn()); err != nil {
-		printErrorAndExit(fmt.Errorf("unable to generate javascript bundle: %v", err))
+		printfAndExit("unable to generate javascript bundle: %v", err)
 	}
 	}
 
 
 	db, err := database.NewConnectionPool(
 	db, err := database.NewConnectionPool(
@@ -161,7 +161,7 @@ func Parse() {
 		config.Opts.DatabaseConnectionLifetime(),
 		config.Opts.DatabaseConnectionLifetime(),
 	)
 	)
 	if err != nil {
 	if err != nil {
-		printErrorAndExit(fmt.Errorf("unable to connect to database: %v", err))
+		printfAndExit("unable to connect to database: %v", err)
 	}
 	}
 	defer db.Close()
 	defer db.Close()
 
 
@@ -231,7 +231,7 @@ func Parse() {
 		slog.Info("Initializing proxy rotation", slog.Int("proxies_count", len(config.Opts.HTTPClientProxies())))
 		slog.Info("Initializing proxy rotation", slog.Int("proxies_count", len(config.Opts.HTTPClientProxies())))
 		proxyrotator.ProxyRotatorInstance, err = proxyrotator.NewProxyRotator(config.Opts.HTTPClientProxies())
 		proxyrotator.ProxyRotatorInstance, err = proxyrotator.NewProxyRotator(config.Opts.HTTPClientProxies())
 		if err != nil {
 		if err != nil {
-			printErrorAndExit(fmt.Errorf("unable to initialize proxy rotator: %v", err))
+			printfAndExit("unable to initialize proxy rotator: %v", err)
 		}
 		}
 	}
 	}
 
 
@@ -252,3 +252,8 @@ func printErrorAndExit(err error) {
 	fmt.Fprintln(os.Stderr, err)
 	fmt.Fprintln(os.Stderr, err)
 	os.Exit(1)
 	os.Exit(1)
 }
 }
+
+func printfAndExit(format string, args ...any) {
+	err := fmt.Errorf(format, args...)
+	printErrorAndExit(err)
+}

+ 3 - 3
internal/cli/export_feeds.go

@@ -13,17 +13,17 @@ import (
 func exportUserFeeds(store *storage.Storage, username string) {
 func exportUserFeeds(store *storage.Storage, username string) {
 	user, err := store.UserByUsername(username)
 	user, err := store.UserByUsername(username)
 	if err != nil {
 	if err != nil {
-		printErrorAndExit(fmt.Errorf("unable to find user: %w", err))
+		printfAndExit("unable to find user: %w", err)
 	}
 	}
 
 
 	if user == nil {
 	if user == nil {
-		printErrorAndExit(fmt.Errorf("user %q not found", username))
+		printfAndExit("user %q not found", username)
 	}
 	}
 
 
 	opmlHandler := opml.NewHandler(store)
 	opmlHandler := opml.NewHandler(store)
 	opmlExport, err := opmlHandler.Export(user.ID)
 	opmlExport, err := opmlHandler.Export(user.ID)
 	if err != nil {
 	if err != nil {
-		printErrorAndExit(fmt.Errorf("unable to export feeds: %w", err))
+		printfAndExit("unable to export feeds: %w", err)
 	}
 	}
 
 
 	fmt.Println(opmlExport)
 	fmt.Println(opmlExport)

+ 2 - 3
internal/cli/health_check.go

@@ -4,7 +4,6 @@
 package cli // import "miniflux.app/v2/internal/cli"
 package cli // import "miniflux.app/v2/internal/cli"
 
 
 import (
 import (
-	"fmt"
 	"log/slog"
 	"log/slog"
 	"net/http"
 	"net/http"
 	"time"
 	"time"
@@ -22,12 +21,12 @@ func doHealthCheck(healthCheckEndpoint string) {
 	client := &http.Client{Timeout: 3 * time.Second}
 	client := &http.Client{Timeout: 3 * time.Second}
 	resp, err := client.Get(healthCheckEndpoint)
 	resp, err := client.Get(healthCheckEndpoint)
 	if err != nil {
 	if err != nil {
-		printErrorAndExit(fmt.Errorf(`health check failure: %v`, err))
+		printfAndExit(`health check failure: %v`, err)
 	}
 	}
 	defer resp.Body.Close()
 	defer resp.Body.Close()
 
 
 	if resp.StatusCode != 200 {
 	if resp.StatusCode != 200 {
-		printErrorAndExit(fmt.Errorf(`health check failed with status code %d`, resp.StatusCode))
+		printfAndExit(`health check failed with status code %d`, resp.StatusCode)
 	}
 	}
 
 
 	slog.Debug(`Health check is passing`)
 	slog.Debug(`Health check is passing`)