From 131d179fcdc65b758ad8fc428a3463f24d2a87d8 Mon Sep 17 00:00:00 2001 From: onee-only Date: Mon, 4 Mar 2024 12:55:59 +0900 Subject: [PATCH 1/3] Remove duplicate code --- src/pattern.go | 3 +-- src/server.go | 14 ++++++-------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/pattern.go b/src/pattern.go index 95f23556b9d..bf92ca19a89 100644 --- a/src/pattern.go +++ b/src/pattern.go @@ -209,11 +209,10 @@ func parseTerms(fuzzy bool, caseMode Case, normalize bool, str string) []termSet // Flip exactness if fuzzy && !inv { typ = termExact - text = text[1:] } else { typ = termFuzzy - text = text[1:] } + text = text[1:] } else if strings.HasPrefix(text, "^") { if typ == termSuffix { typ = termEqual diff --git a/src/server.go b/src/server.go index d5a148df58b..03fe78f32fc 100644 --- a/src/server.go +++ b/src/server.go @@ -236,17 +236,15 @@ func parseGetParams(query string) getParams { for _, pair := range strings.Split(query, "&") { parts := strings.SplitN(pair, "=", 2) if len(parts) == 2 { + val, err := strconv.Atoi(parts[1]) + if err != nil { + continue + } switch parts[0] { case "limit": - val, err := strconv.Atoi(parts[1]) - if err == nil { - params.limit = val - } + params.limit = val case "offset": - val, err := strconv.Atoi(parts[1]) - if err == nil { - params.offset = val - } + params.offset = val } } } From 284297516485fa0def7ff72ceded1e4f461f0c2a Mon Sep 17 00:00:00 2001 From: onee-only Date: Mon, 4 Mar 2024 12:56:41 +0900 Subject: [PATCH 2/3] Update error return position according to convention --- src/options.go | 6 +++--- src/server.go | 20 ++++++++++---------- src/terminal.go | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/options.go b/src/options.go index 09787a44b78..84e1c888a87 100644 --- a/src/options.go +++ b/src/options.go @@ -1866,7 +1866,7 @@ func parseOptions(opts *Options, allArgs []string) { addr := defaultListenAddr if given { var err error - err, addr = parseListenAddress(str) + addr, err = parseListenAddress(str) if err != nil { errorExit(err.Error()) } @@ -1964,14 +1964,14 @@ func parseOptions(opts *Options, allArgs []string) { } else if match, value := optString(arg, "--tabstop="); match { opts.Tabstop = atoi(value) } else if match, value := optString(arg, "--listen="); match { - err, addr := parseListenAddress(value) + addr, err := parseListenAddress(value) if err != nil { errorExit(err.Error()) } opts.ListenAddr = &addr opts.Unsafe = false } else if match, value := optString(arg, "--listen-unsafe="); match { - err, addr := parseListenAddress(value) + addr, err := parseListenAddress(value) if err != nil { errorExit(err.Error()) } diff --git a/src/server.go b/src/server.go index 03fe78f32fc..5307f471561 100644 --- a/src/server.go +++ b/src/server.go @@ -53,47 +53,47 @@ func (addr listenAddress) IsLocal() bool { var defaultListenAddr = listenAddress{"localhost", 0} -func parseListenAddress(address string) (error, listenAddress) { +func parseListenAddress(address string) (listenAddress, error) { parts := strings.SplitN(address, ":", 3) if len(parts) == 1 { parts = []string{"localhost", parts[0]} } if len(parts) != 2 { - return fmt.Errorf("invalid listen address: %s", address), defaultListenAddr + return defaultListenAddr, fmt.Errorf("invalid listen address: %s", address) } portStr := parts[len(parts)-1] port, err := strconv.Atoi(portStr) if err != nil || port < 0 || port > 65535 { - return fmt.Errorf("invalid listen port: %s", portStr), defaultListenAddr + return defaultListenAddr, fmt.Errorf("invalid listen port: %s", portStr) } if len(parts[0]) == 0 { parts[0] = "localhost" } - return nil, listenAddress{parts[0], port} + return listenAddress{parts[0], port}, nil } -func startHttpServer(address listenAddress, actionChannel chan []*action, responseChannel chan string) (error, int) { +func startHttpServer(address listenAddress, actionChannel chan []*action, responseChannel chan string) (int, error) { host := address.host port := address.port apiKey := os.Getenv("FZF_API_KEY") if !address.IsLocal() && len(apiKey) == 0 { - return fmt.Errorf("FZF_API_KEY is required to allow remote access"), port + return port, fmt.Errorf("FZF_API_KEY is required to allow remote access") } addrStr := fmt.Sprintf("%s:%d", host, port) listener, err := net.Listen("tcp", addrStr) if err != nil { - return fmt.Errorf("failed to listen on %s", addrStr), port + return port, fmt.Errorf("failed to listen on %s", addrStr) } if port == 0 { addr := listener.Addr().String() parts := strings.Split(addr, ":") if len(parts) < 2 { - return fmt.Errorf("cannot extract port: %s", addr), port + return port, fmt.Errorf("cannot extract port: %s", addr) } var err error port, err = strconv.Atoi(parts[len(parts)-1]) if err != nil { - return err, port + return port, err } } @@ -119,7 +119,7 @@ func startHttpServer(address listenAddress, actionChannel chan []*action, respon listener.Close() }() - return nil, port + return port, nil } // Here we are writing a simplistic HTTP server without using net/http diff --git a/src/terminal.go b/src/terminal.go index 2069571ab71..9590140d0e4 100644 --- a/src/terminal.go +++ b/src/terminal.go @@ -829,7 +829,7 @@ func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal { _, t.hasLoadActions = t.keymap[tui.Load.AsEvent()] if t.listenAddr != nil { - err, port := startHttpServer(*t.listenAddr, t.serverInputChan, t.serverOutputChan) + port, err := startHttpServer(*t.listenAddr, t.serverInputChan, t.serverOutputChan) if err != nil { errorExit(err.Error()) } From 5d4a2089c1aa2acbdab08dbf6248978269aa5327 Mon Sep 17 00:00:00 2001 From: onee-only Date: Mon, 4 Mar 2024 21:06:09 +0900 Subject: [PATCH 3/3] Update parseGetParams to call strconv.Atoi when params are valid --- src/server.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/server.go b/src/server.go index 5307f471561..c3954d8da35 100644 --- a/src/server.go +++ b/src/server.go @@ -236,15 +236,15 @@ func parseGetParams(query string) getParams { for _, pair := range strings.Split(query, "&") { parts := strings.SplitN(pair, "=", 2) if len(parts) == 2 { - val, err := strconv.Atoi(parts[1]) - if err != nil { - continue - } switch parts[0] { - case "limit": - params.limit = val - case "offset": - params.offset = val + case "limit", "offset": + if val, err := strconv.Atoi(parts[1]); err == nil { + if parts[0] == "limit" { + params.limit = val + } else { + params.offset = val + } + } } } }