aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorBryan McNulty <bryanmcnulty@protonmail.com>2025-02-26 19:42:55 -0600
committerBryan McNulty <bryanmcnulty@protonmail.com>2025-02-26 19:42:55 -0600
commit0591eed275fd24e5c1b959e1a182bea6f6275c34 (patch)
treed32e53640e65627180de9a290449309591bae771 /internal
parent930699ae66d1ddaf44fec71e04c33ab193531584 (diff)
downloadgoexec-0591eed275fd24e5c1b959e1a182bea6f6275c34.tar.gz
goexec-0591eed275fd24e5c1b959e1a182bea6f6275c34.zip
Initial commit +scmrexec
Diffstat (limited to 'internal')
-rw-r--r--internal/util/util.go35
1 files changed, 35 insertions, 0 deletions
diff --git a/internal/util/util.go b/internal/util/util.go
new file mode 100644
index 0000000..252815e
--- /dev/null
+++ b/internal/util/util.go
@@ -0,0 +1,35 @@
+package util
+
+import (
+ "math/rand" // not crypto secure
+ "regexp"
+)
+
+const randHostnameCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-"
+const randStringCharset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
+
+var (
+ // Up to 15 characters; only letters, digits, and hyphens (with hyphens not at the start or end).
+ randHostnameRegex = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9-]{0,14}[a-zA-Z0-9]$`)
+)
+
+func RandomHostname() (hostname string) {
+ for {
+ // between 2 and 10 characters
+ if hostname = RandomStringFromCharset(randHostnameCharset, rand.Intn(8)+2); randHostnameRegex.MatchString(hostname) {
+ return
+ }
+ }
+}
+
+func RandomString() string {
+ return RandomStringFromCharset(randStringCharset, rand.Intn(10)+6)
+}
+
+func RandomStringFromCharset(charset string, length int) string {
+ b := make([]byte, length)
+ for i := range length {
+ b[i] = charset[rand.Intn(len(charset))]
+ }
+ return string(b)
+}