Пример #1
0
// Handle command
func Handle(v resp.Array, ex *CommandExtras) error {
	ex.Buffer.Truncate(0) // Truncate all data in the buffer

	if len(v) == 0 {
		log6.Debug("Command handler, len of the input array is 0")
		return resp.NewError(ErrFmtNoCommand).WriteTo(ex.Buffer)
	}

	args := v.ToArgs()
	//log6.Debug("Command handling:%v", humanArgs(args))

	cmd := strings.ToLower(args[0].String())
	a, err := findCmdFunc(cmd)
	if err != nil {
		log6.Debug("Command handler, cannt found command: %v", cmd)
		return resp.NewError(ErrFmtUnknownCommand, cmd).WriteTo(ex.Buffer)
	}

	if a.c != 0 && len(v) != a.c { //a.c = 0 means to check the number in f
		return resp.NewError(ErrFmtWrongNumberArgument, cmd).WriteTo(ex.Buffer)
	}

	if !ex.IsConnAuthed && ex.Password != "" && cmd != "auth" {
		return resp.NewError(ErrAuthed).WriteTo(ex.Buffer)
	}

	return a.f(args[1:], ex)
}