Initial QSfera import
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package generators
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// NType is an enum type for the different types of NATS connections
|
||||
type NType int
|
||||
|
||||
// Enum values for NType
|
||||
const (
|
||||
NTypeBus NType = iota
|
||||
NTypeKeyValue
|
||||
NTypeRegistry
|
||||
)
|
||||
|
||||
// String returns the string representation of a NType
|
||||
func (n NType) String() string {
|
||||
return []string{"bus", "kv", "reg"}[n]
|
||||
}
|
||||
|
||||
// GenerateConnectionName generates a connection name for a NATS connection
|
||||
// The connection name will be formatted as follows: "hostname:pid:service:type"
|
||||
func GenerateConnectionName(service string, ntype NType) string {
|
||||
host, err := os.Hostname()
|
||||
if err != nil {
|
||||
host = ""
|
||||
}
|
||||
|
||||
return firstNRunes(host, 5) + ":" + strconv.Itoa(os.Getpid()) + ":" + service + ":" + ntype.String()
|
||||
}
|
||||
|
||||
// firstNRunes returns the first n runes of a string
|
||||
func firstNRunes(s string, n int) string {
|
||||
runes := []rune(s)
|
||||
if n > len(runes) {
|
||||
n = len(runes)
|
||||
}
|
||||
return string(runes[:n])
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package generators
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
const (
|
||||
// PasswordChars contains alphanumeric chars (0-9, A-Z, a-z), plus "-=+!@#$%^&*."
|
||||
PasswordChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-=+!@#$%^&*."
|
||||
// AlphaNumChars contains alphanumeric chars (0-9, A-Z, a-z)
|
||||
AlphaNumChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||
)
|
||||
|
||||
// GenerateRandomPassword generates a random password with the given length.
|
||||
// The password will contain chars picked from the `PasswordChars` constant.
|
||||
// If an error happens, the string will be empty and the error will be non-nil.
|
||||
//
|
||||
// This is equivalent to `GenerateRandomString(PasswordChars, length)`
|
||||
func GenerateRandomPassword(length int) (string, error) {
|
||||
return generateString(PasswordChars, length)
|
||||
}
|
||||
|
||||
// GenerateRandomString generates a random string with the given length
|
||||
// based on the chars provided. You can use `PasswordChars` or `AlphaNumChars`
|
||||
// constants, or even any other string.
|
||||
//
|
||||
// Chars from the provided string will be picked uniformly. The provided
|
||||
// constants have unique chars, which means that all the chars will have the
|
||||
// same probability of being picked.
|
||||
// You can use your own strings to change that probability. For example, using
|
||||
// "AAAB" you'll have a 75% of probability of getting "A" and 25% of "B"
|
||||
func GenerateRandomString(chars string, length int) (string, error) {
|
||||
return generateString(chars, length)
|
||||
}
|
||||
|
||||
func generateString(chars string, length int) (string, error) {
|
||||
ret := make([]byte, length)
|
||||
for i := 0; i < length; i++ {
|
||||
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(chars))))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
ret[i] = chars[num.Int64()]
|
||||
}
|
||||
|
||||
return string(ret), nil
|
||||
}
|
||||
Reference in New Issue
Block a user