29 lines
778 B
Go
29 lines
778 B
Go
package fs
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// ctlHandler is a function that handles a single ctl command.
|
|
type ctlHandler func(args []string, ctx HandlerCtx) error
|
|
|
|
// ctlDispatch returns a Write handler that dispatches based on the first
|
|
// whitespace-delimited word in the input. The map keys are command names;
|
|
// values are the handlers. If the command is unknown, an error is returned.
|
|
func ctlDispatch(handlers map[string]ctlHandler) func(HandlerCtx, []byte) error {
|
|
return func(ctx HandlerCtx, data []byte) error {
|
|
input := strings.TrimSpace(string(data))
|
|
if input == "" {
|
|
return nil
|
|
}
|
|
parts := strings.Fields(input)
|
|
cmd := parts[0]
|
|
h, ok := handlers[cmd]
|
|
if !ok {
|
|
return fmt.Errorf("unknown ctl command: %s", cmd)
|
|
}
|
|
return h(parts[1:], ctx)
|
|
}
|
|
}
|