forked from toolshed/abra
feat: finally implement app new command
This commit is contained in:
@ -80,39 +80,55 @@ func ParseSecretEnvVarValue(secretValue string) (SecretValue, error) {
|
||||
if err != nil {
|
||||
return SecretValue{}, err
|
||||
}
|
||||
return SecretValue{Version: values[0], Length: length}, nil
|
||||
version := strings.ReplaceAll(values[0], " ", "")
|
||||
return SecretValue{Version: version, Length: length}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func GenerateSecrets(secretEnvVars map[string]string, server string) (map[string]string, error) {
|
||||
func GenerateSecrets(secretEnvVars map[string]string, appName, server string) (map[string]string, error) {
|
||||
secrets := make(map[string]string)
|
||||
|
||||
ch := make(chan error, len(secretEnvVars))
|
||||
for secretEnvVar := range secretEnvVars {
|
||||
secretName := ParseSecretEnvVarName(secretEnvVar)
|
||||
secretValue, err := ParseSecretEnvVarValue(secretEnvVars[secretEnvVar])
|
||||
go func(s string) {
|
||||
secretName := ParseSecretEnvVarName(s)
|
||||
secretValue, err := ParseSecretEnvVarValue(secretEnvVars[s])
|
||||
if err != nil {
|
||||
ch <- err
|
||||
return
|
||||
}
|
||||
secretRemoteName := fmt.Sprintf("%s_%s_%s", appName, secretName, secretValue.Version)
|
||||
if secretValue.Length > 0 {
|
||||
passwords, err := GeneratePasswords(1, uint(secretValue.Length))
|
||||
if err != nil {
|
||||
ch <- err
|
||||
return
|
||||
}
|
||||
if err := client.StoreSecret(secretRemoteName, passwords[0], server); err != nil {
|
||||
ch <- err
|
||||
return
|
||||
}
|
||||
secrets[secretName] = passwords[0]
|
||||
} else {
|
||||
passphrases, err := GeneratePassphrases(1)
|
||||
if err != nil {
|
||||
ch <- err
|
||||
return
|
||||
}
|
||||
if err := client.StoreSecret(secretRemoteName, passphrases[0], server); err != nil {
|
||||
ch <- err
|
||||
}
|
||||
secrets[secretName] = passphrases[0]
|
||||
}
|
||||
ch <- nil
|
||||
}(secretEnvVar)
|
||||
}
|
||||
|
||||
for range secretEnvVars {
|
||||
err := <-ch
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if secretValue.Length > 0 {
|
||||
passwords, err := GeneratePasswords(1, uint(secretValue.Length))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
secrets[secretName] = passwords[0]
|
||||
if err := client.StoreSecret(secretName, passwords[0], server); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
passphrases, err := GeneratePassphrases(1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
secrets[secretName] = passphrases[0]
|
||||
if err := client.StoreSecret(secretName, passphrases[0], server); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return secrets, nil
|
||||
|
Reference in New Issue
Block a user