Skip to content

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

Merged
merged 3 commits into from
Aug 13, 2019
Merged

Conversation

yorinasub17
Copy link
Contributor

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 of random.UniqueId here that is more generic, where you can control char sets and digits.

Note that this uses crypto/rand instead of math/rand, to allow for potential usage with password generation.

const LowerLetters = "abcdefghijklmnopqrstuvwxyz"
const SpecialChars = "<>[]{}()-_*%&/?\"'\\"

var Base62Chars = Digits + UpperLetters + LowerLetters
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat 👍

Copy link
Member

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.

Copy link
Contributor Author

@yorinasub17 yorinasub17 Aug 13, 2019

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 😉

Copy link
Member

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?

Copy link
Contributor Author

@yorinasub17 yorinasub17 Aug 13, 2019

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")

Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in d9def43

…ted, and add documentation to make the char sets more clear
@yorinasub17
Copy link
Contributor Author

Ok addressed the question about being more clear about the usage. Will merge now and release. Thanks for the review!

@yorinasub17 yorinasub17 merged commit 2b40fc3 into master Aug 13, 2019
@yorinasub17 yorinasub17 deleted the yori-random branch August 13, 2019 19:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants