-
Notifications
You must be signed in to change notification settings - Fork 26
Add random string generation capabilities #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
const LowerLetters = "abcdefghijklmnopqrstuvwxyz" | ||
const SpecialChars = "<>[]{}()-_*%&/?\"'\\" | ||
|
||
var Base62Chars = Digits + UpperLetters + LowerLetters |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh wait, I think with all the special characters, this is not base 62. At the least, the variable should be renamed. Note also that the special characters may limit reusability of this unique ID, as many identifiers and even passwords don't allow those characters, whereas almost everything supports alphanumeric.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't have the special characters 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh... Right. So is that variable unused?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a convenience variable. The function allows you to control what characters to use to generate the string. Examples:
// Only lower case chars + digits
random.RandomString(6, random.Digits + random.LowerLetters)
// alphanumerics + special chars
random.RandomString(6, random.Base62Chars + random.SpecialChars)
// Only alphanumerics (base62)
random.RandomString(6, random.Base62Chars)
// Only abc
random.RandomString(6, "abc")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohhh, I gotcha now. Perhaps add a test case using some of the other character sets to make that a bit clearer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in d9def43
8e44d7d
to
1be8d59
Compare
…ted, and add documentation to make the char sets more clear
Ok addressed the question about being more clear about the usage. Will merge now and release. Thanks for the review! |
In the GCP bootstrap script I am currently developing, I have a need to generate a random string that I can use as a unique identifier for seeding the Project ID when creating a new project. I haven't seen us provide this as a library outside of
terratest
, so I ported a version ofrandom.UniqueId
here that is more generic, where you can control char sets and digits.Note that this uses
crypto/rand
instead ofmath/rand
, to allow for potential usage with password generation.