Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor some code #3655

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down Expand Up @@ -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())
}
Expand Down
3 changes: 1 addition & 2 deletions src/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 16 additions & 18 deletions src/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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
Expand Down Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this. We don't want to call Atoi for other unsupported parameters.

This is what I'm suggesting.

switch parts[0] {
case "limit", "offset":
	if val, err := strconv.Atoi(parts[1]); err == nil {
		if parts[0] == "limit" {
			params.limit = val
		} else {
			params.offset = val
		}
	}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks much better! I'll update it as you suggested. Thank you for your feedback!

}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
Loading