Skip to content

Instantly share code, notes, and snippets.

@chris-skud
Last active Dec 1, 2017
Embed
What would you like to do?
log envconfig replacing sensitive values with stars
package main
import (
"fmt"
"log"
"reflect"
"strings"
"github.com/kelseyhightower/envconfig"
)
type envConfig struct {
SDLC string `envconfig:"SDLC" default:"local"`
DBHost string `envconfig:"DB_HOST" default:"localhost"`
DBPort string `envconfig:"DB_PORT" default:"5432"`
}
func (e *envConfig) String() string {
outStr := ""
v := reflect.ValueOf(*e)
for i := 0; i < v.NumField(); i++ {
name := fmt.Sprintf("%v", v.Type().Field(i).Name)
value := fmt.Sprintf("%v", v.Field(i).Interface())
if strings.Contains(name, "Secret") || strings.Contains(name, "Password") || strings.Contains(name, "Key") {
value = strings.Repeat("*", len(value))
}
outStr = outStr + fmt.Sprintf("%s:%s, ", name, value)
}
return outStr
}
func main() {
var envVar envConfig
err := envconfig.Process("CM", &envVar)
if err != nil {
log.Fatalf("Error parsing environment: %v", err)
}
log.Println(envVar.String())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment