refactor: make SecretValue internal

This commit is contained in:
decentral1se 2021-09-04 23:35:56 +02:00
parent fadbbabe09
commit 4e92057f61
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC

View File

@ -14,7 +14,9 @@ import (
"github.com/schultz-is/passgen"
)
type SecretValue struct {
// secretValue represents a parsed `SECRET_FOO=v1 # length=bar` env var config
// secret definition.
type secretValue struct {
Version string
Length int
}
@ -73,23 +75,23 @@ func ParseGeneratedSecretName(secret string, appEnv config.App) string {
return withoutAppName[:idx]
}
func ParseSecretEnvVarValue(secretValue string) (SecretValue, error) {
values := strings.Split(secretValue, "#")
func ParseSecretEnvVarValue(secret string) (secretValue, error) {
values := strings.Split(secret, "#")
if len(values) == 0 {
return SecretValue{}, fmt.Errorf("unable to parse '%s'", secretValue)
return secretValue{}, fmt.Errorf("unable to parse '%s'", secret)
}
if len(values) == 1 {
return SecretValue{Version: values[0], Length: 0}, nil
return secretValue{Version: values[0], Length: 0}, nil
} else {
split := strings.Split(values[1], "=")
parsed := split[len(split)-1]
stripped := strings.ReplaceAll(parsed, " ", "")
length, err := strconv.Atoi(stripped)
if err != nil {
return SecretValue{}, err
return secretValue{}, err
}
version := strings.ReplaceAll(values[0], " ", "")
return SecretValue{Version: version, Length: length}, nil
return secretValue{Version: version, Length: length}, nil
}
}