forked from gruntwork-io/cloud-nuke
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move unique id function to separate go package file
- Loading branch information
Showing
6 changed files
with
40 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package util | ||
|
||
import ( | ||
"bytes" | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
// Returns a unique (ish) id we can attach to resources and tfstate files so they don't conflict with each other | ||
// Uses base 62 to generate a 6 character string that's unlikely to collide with the handful of tests we run in | ||
// parallel. Based on code here: http://stackoverflow.com/a/9543797/483528 | ||
func UniqueID() string { | ||
|
||
const BASE_62_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" | ||
const UNIQUE_ID_LENGTH = 6 // Should be good for 62^6 = 56+ billion combinations | ||
|
||
var out bytes.Buffer | ||
|
||
rand := rand.New(rand.NewSource(time.Now().UnixNano())) | ||
for i := 0; i < UNIQUE_ID_LENGTH; i++ { | ||
out.WriteByte(BASE_62_CHARS[rand.Intn(len(BASE_62_CHARS))]) | ||
} | ||
|
||
return out.String() | ||
} |