Last active
August 10, 2018 19:08
-
-
Save zeddee/73b09f037a7b7c594d8c63dc44cb799d to your computer and use it in GitHub Desktop.
Read secrets from a file, where secrets are the format `KEY=VALUE`, and returns a map. Access by calling secret["KEY"] => "VALUE".
This file contains hidden or 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
package main | |
import ( | |
"bufio" | |
"log" | |
"os" | |
"strings" | |
) | |
// ReadSecret gets API Key from a file | |
func ReadSecret(file string) map[string]string { | |
config := make(map[string]string) | |
f, err := os.Open(file) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer f.Close() | |
reader := bufio.NewReader(f) | |
// Reads only the first line; ignores all other lines | |
for { | |
line, err := reader.ReadString('\n') | |
if err != nil { | |
return config | |
} | |
// Check presence of "=" sign in line | |
equals := strings.Index(line, "=") | |
// Test for presence of config; | |
// if present, return value of config key. | |
switch { | |
case strings.Index(line, "#") >= 0: | |
// Skip if line is a comment | |
break | |
case equals < 0, strings.Index(line, " ") >= 0: | |
// Log as invalid config if line does not contain "=", or if contains spaces. | |
//log.Printf("Invalid config at %s", line) | |
case equals >= 0: | |
k := Chomp(strings.Split(line, "=")[0]) | |
v := Chomp(strings.Split(line, "=")[1]) | |
config[k] = v | |
default: | |
break | |
} | |
} | |
} | |
// Chomp removes trailing spaces and newline chars from strings | |
func Chomp(in string) string { | |
// Inelegantly removing extraneous spaces | |
s := strings.Split(in, " ") | |
if len(s) > 1 { | |
var in2 string | |
for _, v := range s { | |
in2 = in2 + v | |
} | |
in = in2 | |
} | |
if strings.HasSuffix(in, "\r") { | |
// Check for CRLF | |
return Chomp(in[:len(in)-1]) | |
} else if strings.HasSuffix(in, "\n") { | |
// Check for LF | |
return Chomp(in[:len(in)-1]) | |
} else if strings.HasSuffix(in, " ") { | |
// Check for trailing spaces at end of input. | |
return Chomp(in[:len(in)-1]) | |
} else if strings.HasPrefix(in, " ") { | |
// Check for traling spaces at start of input. | |
return Chomp(in[1:]) | |
} | |
return in | |
} |
This file contains hidden or 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
package main | |
import ( | |
"log" | |
"testing" | |
) | |
func TestReadSecret(t *testing.T) { | |
file := ".env_test" | |
out := ReadSecret(file) | |
if len(out) == 0 { | |
t.Errorf("Couldn't read %s file", file) | |
} | |
for i := range out { | |
log.Println(out[i]) | |
} | |
} | |
func TestChomp(t *testing.T) { | |
out := Chomp(" Test\n\r \n\n\r") | |
if out != "Test" { | |
t.Error("Failed: chomp failed to remove LF line endings and trailing spaces.") | |
} | |
out = Chomp(" T e st \n\r \n\n\r") | |
if out != "Test" { | |
t.Error("Failed: chomp failed to remove extraneous spaces.") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment