utils.go => validations.go
This commit is contained in:
parent
8666a21d0b
commit
583f443e7e
1 changed files with 0 additions and 0 deletions
43
utils/validations.go
Normal file
43
utils/validations.go
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/acarl005/stripansi"
|
||||
)
|
||||
|
||||
func ClearUnprintableChars(s string, allowNewlines bool) string {
|
||||
// This will remove ANSI color codes.
|
||||
s = stripansi.Strip(s)
|
||||
|
||||
return strings.Map(func(r rune) rune {
|
||||
if unicode.IsPrint(r) || (allowNewlines && r == '\n') {
|
||||
return r
|
||||
}
|
||||
return -1
|
||||
}, s)
|
||||
}
|
||||
|
||||
var devNameRegexp = regexp.MustCompile(`^[0-9a-z_-]+$`)
|
||||
|
||||
func ValidateDevName(s string) bool {
|
||||
// Allow mapped devices.
|
||||
s = strings.TrimPrefix(s, "mapper/")
|
||||
|
||||
return devNameRegexp.MatchString(s)
|
||||
}
|
||||
|
||||
var unixUsernameRegexp = regexp.MustCompile(`^[a-z_]([a-z0-9_-]{0,31}|[a-z0-9_-]{0,30}\$)$`)
|
||||
|
||||
func ValidateUnixUsername(s string) bool {
|
||||
return unixUsernameRegexp.MatchString(s)
|
||||
}
|
||||
|
||||
func Uint16ToBytesBE(v uint16) []byte {
|
||||
b := make([]byte, 2)
|
||||
binary.BigEndian.PutUint16(b, v)
|
||||
return b
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue