feat: add secret generate (untested, moving fast)

This commit is contained in:
decentral1se 2021-08-31 11:59:07 +02:00
parent d68f2f5686
commit 57e641689a
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
2 changed files with 54 additions and 4 deletions

View File

@ -26,8 +26,8 @@
- [x] `rm`
- [x] `run`
- [ ] `rollback`
- [ ] `secret` (WIP: decentral1se)
- [ ] `generate` (WIP: decentral1se)
- [x] `secret`
- [x] `generate`
- [x] `insert`
- [x] `rm`
- [x] `ls`

View File

@ -26,9 +26,59 @@ var allSecretsFlag = &cli.BoolFlag{
}
var appSecretGenerateCommand = &cli.Command{
Name: "generate",
Usage: "Generate secrets",
Name: "generate",
Usage: "Generate secrets",
ArgsUsage: "<secret> <version>",
Flags: []cli.Flag{allSecretsFlag, internal.PassFlag},
Action: func(c *cli.Context) error {
appName := c.Args().First()
if appName == "" {
internal.ShowSubcommandHelpAndError(c, errors.New("no app name provided"))
}
if c.Args().Get(1) != "" && allSecrets {
internal.ShowSubcommandHelpAndError(c, errors.New("cannot use '<secret> <version>' and '--all' together"))
}
appFiles, err := config.LoadAppFiles("")
if err != nil {
logrus.Fatal(err)
}
appEnv, err := config.GetApp(appFiles, appName)
if err != nil {
logrus.Fatal(err)
}
secretsToCreate := make(map[string]string)
host := appFiles[appName].Server
secretEnvVars := secret.ReadSecretEnvVars(appEnv.Env)
if allSecrets {
secretsToCreate = secretEnvVars
} else {
secretName := c.Args().Get(1)
secretVersion := c.Args().Get(2)
for sec, _ := range secretEnvVars {
parsed := secret.ParseSecretEnvVarName(sec)
if secretName == parsed {
secretsToCreate[sec] = secretVersion
}
}
}
secretVals, err := secret.GenerateSecrets(secretsToCreate, appEnv.StackName(), host)
if err != nil {
logrus.Fatal(err)
}
if internal.Pass {
for name, data := range secretVals {
if err := secret.PassInsertSecret(data, name, appEnv.StackName(), host); err != nil {
logrus.Fatal(err)
}
}
}
return nil
},
}