WIP another pass on the app new command

This commit is contained in:
decentral1se 2021-07-30 22:55:00 +02:00
parent e728bcd7ac
commit 5771f6c158
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
3 changed files with 58 additions and 9 deletions

View File

@ -5,26 +5,28 @@ import (
"fmt"
"io/ioutil"
"path"
"regexp"
"sort"
"strconv"
"strings"
"coopcloud.tech/abra/catalogue"
"coopcloud.tech/abra/client"
"coopcloud.tech/abra/config"
"coopcloud.tech/abra/secret"
"github.com/AlecAivazis/survey/v2"
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/cli/cli/command/idresolver"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm"
"github.com/schultz-is/passgen"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
var appNewCommand = &cli.Command{
Name: "new",
Usage: "Create a new app of <type>",
Usage: "Create a new app",
Description: `
This command takes a recipe and uses it to cook up a new app. This app
configuration is stored in your ~/.abra directory under the appropriate server.
@ -82,7 +84,7 @@ locally in a pass store (see passwordstore.org for more).
servers := appFiles.GetServers()
if Server == "" {
prompt := &survey.Select{
Message: "Select server to create this app for:",
Message: "Select app server:",
Options: servers,
}
if err := survey.AskOne(prompt, &Server); err != nil {
@ -92,7 +94,7 @@ locally in a pass store (see passwordstore.org for more).
if Domain == "" {
prompt := &survey.Input{
Message: "Specify domain for this app:",
Message: "Specify app domain",
}
if err := survey.AskOne(prompt, &Domain); err != nil {
logrus.Fatal(err)
@ -126,15 +128,57 @@ locally in a pass store (see passwordstore.org for more).
logrus.Fatal(err)
}
// generate secrets if asked to do so
if Secrets {
appEnv, err := config.ReadEnv(appEnvPath)
if err != nil {
logrus.Fatal(err)
}
var secretEnvVars []string
for envVar := range appEnv {
regex := regexp.MustCompile(`^SECRET.*VERSION.*`)
secretEnvVar := regex.Find([]byte(envVar))
secretEnvVars = append(secretEnvVars, string(secretEnvVar))
}
var length int
var secretValue string
var secrets map[string]string
for _, secretEnvVar := range secretEnvVars {
withoutPrefix := strings.TrimPrefix(secretEnvVar, "SECRET_")
regex := regexp.MustCompile(`_VERSION=.*`)
secretName := string(regex.Find([]byte(withoutPrefix)))
if strings.Contains(secretEnvVar, "length") {
regex := regexp.MustCompile(`s/.*[^0-9]\([0-9]\+\)[^0-9]*$/\1/`)
match := regex.Find([]byte(secretEnvVar))
length, err = strconv.Atoi(string(match))
if err != nil {
logrus.Fatal(err)
}
passwords, err := secret.GeneratePasswords(1, uint(length))
if err != nil {
logrus.Fatal(err)
}
secretValue = passwords[0]
secrets[secretEnvVar] = secretValue
} else {
passphrases, err := secret.GeneratePassphrases(1)
if err != nil {
logrus.Fatal(err)
}
secretValue = passphrases[0]
secrets[secretEnvVar] = secretValue
}
go client.StoreSecret(secretName, secretValue, Server)
}
}
// save them in a pass store if asked to do so
// TODO: save them in a pass store if asked to do so
if Pass {
}
// Output some instructions on how to deploy this thing
// TODO: Output some instructions on how to deploy this thing
return nil
},

5
client/secret.go Normal file
View File

@ -0,0 +1,5 @@
package client
func StoreSecret(secretName, secretValue, server string) error {
return nil
}

View File

@ -203,7 +203,7 @@ func GetAppStatuses(appFiles AppFiles) (map[string]string, error) {
// TODO: maybe better names than read and get
func readAppFile(appFile AppFile, name AppName) (App, error) {
env, err := readEnv(appFile.Path)
env, err := ReadEnv(appFile.Path)
if err != nil {
return App{}, fmt.Errorf("env file for '%s' couldn't be read: %s", name, err.Error())
}
@ -214,7 +214,7 @@ func readAppFile(appFile AppFile, name AppName) (App, error) {
return app, nil
}
func readEnv(filePath string) (AppEnv, error) {
func ReadEnv(filePath string) (AppEnv, error) {
var envFile AppEnv
envFile, err := godotenv.Read(filePath)
if err != nil {