28 lines
710 B
Go
28 lines
710 B
Go
package fs
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// rdwrHandler is a request-response ctl command handler.
|
|
type rdwrHandler func(ctx HandlerCtx, args []string) ([]byte, error)
|
|
|
|
// rdwrDispatch returns a Request handler that dispatches based on the first
|
|
// whitespace-delimited word in the input.
|
|
func rdwrDispatch(handlers map[string]rdwrHandler) func(HandlerCtx, []byte) ([]byte, error) {
|
|
return func(ctx HandlerCtx, data []byte) ([]byte, error) {
|
|
input := strings.TrimSpace(string(data))
|
|
if input == "" {
|
|
return nil, nil
|
|
}
|
|
parts := strings.Fields(input)
|
|
cmd := parts[0]
|
|
h, ok := handlers[cmd]
|
|
if !ok {
|
|
return nil, fmt.Errorf("unknown ctl command: %s", cmd)
|
|
}
|
|
return h(ctx, parts[1:])
|
|
}
|
|
}
|