Samuel A. Falvo II | 704a750 | 2013-07-10 15:23:43 -0700 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
Samuel A. Falvo II | e91ff6d | 2013-07-11 15:46:10 -0700 | [diff] [blame] | 6 | "crypto/rand" |
Samuel A. Falvo II | 704a750 | 2013-07-10 15:23:43 -0700 | [diff] [blame] | 7 | ) |
| 8 | |
| 9 | // getCredentials will verify existence of needed credential information |
| 10 | // provided through environment variables. This function will not return |
| 11 | // if at least one piece of required information is missing. |
| 12 | func getCredentials() (provider, username, password string) { |
| 13 | provider = os.Getenv("SDK_PROVIDER") |
| 14 | username = os.Getenv("SDK_USERNAME") |
| 15 | password = os.Getenv("SDK_PASSWORD") |
| 16 | |
| 17 | if (provider == "") || (username == "") || (password == "") { |
| 18 | fmt.Fprintf(os.Stderr, "One or more of the following environment variables aren't set:\n") |
| 19 | fmt.Fprintf(os.Stderr, " SDK_PROVIDER=\"%s\"\n", provider) |
| 20 | fmt.Fprintf(os.Stderr, " SDK_USERNAME=\"%s\"\n", username) |
| 21 | fmt.Fprintf(os.Stderr, " SDK_PASSWORD=\"%s\"\n", password) |
| 22 | os.Exit(1) |
| 23 | } |
| 24 | |
| 25 | return |
| 26 | } |
Samuel A. Falvo II | e91ff6d | 2013-07-11 15:46:10 -0700 | [diff] [blame] | 27 | |
| 28 | // randomString generates a string of given length, but random content. |
| 29 | // All content will be within the ASCII graphic character set. |
| 30 | // (Implementation from Even Shaw's contribution on |
| 31 | // http://stackoverflow.com/questions/12771930/what-is-the-fastest-way-to-generate-a-long-random-string-in-go). |
Samuel A. Falvo II | e3b2d7a | 2013-07-12 11:08:02 -0700 | [diff] [blame^] | 32 | func randomString(prefix string, n int) string { |
Samuel A. Falvo II | e91ff6d | 2013-07-11 15:46:10 -0700 | [diff] [blame] | 33 | const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" |
| 34 | var bytes = make([]byte, n) |
| 35 | rand.Read(bytes) |
| 36 | for i, b := range bytes { |
| 37 | bytes[i] = alphanum[b % byte(len(alphanum))] |
| 38 | } |
Samuel A. Falvo II | e3b2d7a | 2013-07-12 11:08:02 -0700 | [diff] [blame^] | 39 | return prefix + string(bytes) |
Samuel A. Falvo II | e91ff6d | 2013-07-11 15:46:10 -0700 | [diff] [blame] | 40 | } |