2023-08-25 15:12:19 +01:00
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
2023-08-25 16:54:58 +01:00
|
|
|
"regexp"
|
2023-08-25 15:12:19 +01:00
|
|
|
"strings"
|
|
|
|
|
"unicode"
|
2023-08-27 15:30:51 +01:00
|
|
|
|
|
|
|
|
"github.com/acarl005/stripansi"
|
2023-08-25 15:12:19 +01:00
|
|
|
)
|
|
|
|
|
|
2023-08-27 15:30:51 +01:00
|
|
|
func ClearUnprintableChars(s string, allowNewlines bool) string {
|
|
|
|
|
// This will remove ANSI color codes.
|
|
|
|
|
s = stripansi.Strip(s)
|
|
|
|
|
|
2023-08-25 15:12:19 +01:00
|
|
|
return strings.Map(func(r rune) rune {
|
2023-08-27 15:30:51 +01:00
|
|
|
if unicode.IsPrint(r) || (allowNewlines && r == '\n') {
|
2023-08-25 15:12:19 +01:00
|
|
|
return r
|
|
|
|
|
}
|
|
|
|
|
return -1
|
|
|
|
|
}, s)
|
|
|
|
|
}
|
2023-08-25 16:54:58 +01:00
|
|
|
|
|
|
|
|
var devNameRegexp = regexp.MustCompile("^[0-9a-z_-]+$")
|
|
|
|
|
|
|
|
|
|
func ValidateDevName(s string) bool {
|
|
|
|
|
// Allow mapped devices.
|
|
|
|
|
s = strings.TrimPrefix(s, "mapper/")
|
|
|
|
|
|
|
|
|
|
return devNameRegexp.MatchString(s)
|
|
|
|
|
}
|