From 3eeb38531b1c4b025e45afecd7baad7a25a8f1dd Mon Sep 17 00:00:00 2001 From: Benjamin Dahlmanns Date: Sun, 22 Apr 2018 13:50:04 +0200 Subject: [PATCH] Simplify parameter checking and use panic in order to fail fast in case of errors --- uinput.go | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/uinput.go b/uinput.go index ecfda37..f2f3e7f 100644 --- a/uinput.go +++ b/uinput.go @@ -77,7 +77,6 @@ device. Here are a few examples of how to use the virtual touch pad: package uinput import ( - "errors" "fmt" "io" "os" @@ -187,12 +186,8 @@ type vTouchPad struct { // CreateTouchPad will create a new touch pad device. note that you will need to define the x and y axis boundaries // (min and max) within which the cursor maybe moved around. func CreateTouchPad(path string, name []byte, minX int32, maxX int32, minY int32, maxY int32) (TouchPad, error) { - if path == "" { - return nil, errors.New("device path must not be empty") - } - if len(name) > uinputMaxNameSize { - return nil, fmt.Errorf("device name %s is too long (maximum of %d characters allowed)", name, uinputMaxNameSize) - } + validateDevicePath(path) + validateUinputName(name) fd, err := createTouchPad(path, name, minX, maxX, minY, maxY) if err != nil { @@ -303,12 +298,8 @@ func (vTouch vTouchPad) Close() error { // CreateMouse will create a new mouse input device. A mouse is a device that allows relative input. // Relative input means that all changes to the x and y coordinates of the mouse pointer will be func CreateMouse(path string, name []byte) (Mouse, error) { - if path == "" { - return nil, errors.New("device path must not be empty") - } - if len(name) > uinputMaxNameSize { - return nil, fmt.Errorf("device name %s is too long (maximum of %d characters allowed)", name, uinputMaxNameSize) - } + validateDevicePath(path) + validateUinputName(name) fd, err := createMouse(path, name) if err != nil { @@ -438,12 +429,8 @@ func (vRel vMouse) Close() error { // CreateKeyboard will create a new keyboard using the given uinput // device path of the uinput device. func CreateKeyboard(path string, name []byte) (Keyboard, error) { - if path == "" { - return nil, errors.New("device path must not be empty") - } - if len(name) > uinputMaxNameSize { - return nil, fmt.Errorf("device name %s is too long (maximum of %d characters allowed)", name, uinputMaxNameSize) - } + validateDevicePath(path) + validateUinputName(name) fd, err := createVKeyboardDevice(path, name) if err != nil { @@ -509,3 +496,15 @@ func (vk vKeyboard) KeyUp(key int) error { func (vk vKeyboard) Close() error { return closeDevice(vk.deviceFile) } + +func validateDevicePath(path string) { + if path == "" { + panic("device path must not be empty") + } +} + +func validateUinputName(name []byte) { + if len(name) > uinputMaxNameSize { + panic(fmt.Sprintf("device name %s is too long (maximum of %d characters allowed)", name, uinputMaxNameSize)) + } +}