refactor: clear up app/recipe usage

See coop-cloud/go-abra#36.
This commit is contained in:
2021-09-05 00:14:27 +02:00
parent 5e4114036b
commit ff21237a21
3 changed files with 112 additions and 95 deletions

View File

@ -45,14 +45,14 @@ var newAppNameFlag = &cli.StringFlag{
}
var appNewDescription = `
This command takes an app recipe and uses it to create a new app. This new app
This command takes a recipe and uses it to create a new app. This new app
configuration is stored in your ~/.abra directory under the appropriate server.
This command does not deploy your app for you. You will need to run "abra app
deploy <app>" to do so.
You can see what apps can be created (i.e. values for the <type> argument) by
running "abra recipe ls".
You can see what recipes are available (i.e. values for the <recipe> argument)
by running "abra recipe ls".
Passing the "--secrets/-S" flag will automatically generate secrets for your
app and store them encrypted at rest on the chosen target server. These
@ -75,27 +75,29 @@ var appNewCommand = &cli.Command{
internal.PassFlag,
internal.SecretsFlag,
},
ArgsUsage: "<type>",
ArgsUsage: "<recipe>",
Action: action,
}
func appLookup(appType string) (catalogue.App, error) {
catl, err := catalogue.ReadAppsCatalogue()
// getRecipe retrieves a recipe from the recipe catalogue.
func getRecipe(recipeName string) (catalogue.Recipe, error) {
catl, err := catalogue.ReadRecipeCatalogue()
if err != nil {
return catalogue.App{}, err
return catalogue.Recipe{}, err
}
app, ok := catl[appType]
recipe, ok := catl[recipeName]
if !ok {
return catalogue.App{}, fmt.Errorf("app type does not exist: %s", appType)
return catalogue.Recipe{}, fmt.Errorf("recipe '%s' does not exist?", recipeName)
}
if err := app.EnsureExists(); err != nil {
return catalogue.App{}, err
if err := recipe.EnsureExists(); err != nil {
return catalogue.Recipe{}, err
}
return app, nil
return recipe, nil
}
// ensureDomainFlag checks if the domain flag was used. if not, asks the user for it
// ensureDomainFlag checks if the domain flag was used. if not, asks the user for it/
func ensureDomainFlag() error {
if domain == "" {
prompt := &survey.Input{
@ -108,7 +110,7 @@ func ensureDomainFlag() error {
return nil
}
// ensureServerFlag checks if the server flag was used. if not, asks the user for it
// ensureServerFlag checks if the server flag was used. if not, asks the user for it.
func ensureServerFlag() error {
appFiles, err := config.LoadAppFiles(newAppServer)
if err != nil {
@ -127,7 +129,7 @@ func ensureServerFlag() error {
return nil
}
// ensureServerFlag checks if the AppName flag was used. if not, asks the user for it
// ensureServerFlag checks if the AppName flag was used. if not, asks the user for it.
func ensureAppNameFlag() error {
if newAppName == "" {
prompt := &survey.Input{
@ -141,6 +143,7 @@ func ensureAppNameFlag() error {
return nil
}
// createSecrets creates all secrets for a new app.
func createSecrets(sanitisedAppName string) (secrets, error) {
appEnvPath := path.Join(config.ABRA_DIR, "servers", newAppServer, fmt.Sprintf("%s.env", sanitisedAppName))
appEnv, err := config.ReadEnv(appEnvPath)
@ -165,23 +168,24 @@ func createSecrets(sanitisedAppName string) (secrets, error) {
return secrets, nil
}
// action is the main command-line action for this package
func action(c *cli.Context) error {
appType := c.Args().First()
if appType == "" {
internal.ShowSubcommandHelpAndError(c, errors.New("no app type provided"))
recipeName := c.Args().First()
if recipeName == "" {
internal.ShowSubcommandHelpAndError(c, errors.New("no recipe provided"))
}
if err := config.EnsureAbraDirExists(); err != nil {
logrus.Fatal(err)
}
app, err := appLookup(appType)
recipe, err := getRecipe(recipeName)
if err != nil {
logrus.Fatal(err)
}
latestVersion := app.LatestVersion()
if err := app.EnsureVersion(latestVersion); err != nil {
latestVersion := recipe.LatestVersion()
if err := recipe.EnsureVersion(latestVersion); err != nil {
logrus.Fatal(err)
}
@ -203,7 +207,7 @@ func action(c *cli.Context) error {
logrus.Fatalf("'%s' cannot be longer than 45 characters", sanitisedAppName)
}
if err := config.CopyAppEnvSample(appType, newAppName, newAppServer); err != nil {
if err := config.CopyAppEnvSample(recipeName, newAppName, newAppServer); err != nil {
logrus.Fatal(err)
}
@ -218,15 +222,13 @@ func action(c *cli.Context) error {
for secret := range secrets {
secretTable.Append([]string{secret, secrets[secret]})
}
// Defer secret table first so it is last no matter what
defer secretTable.Render()
}
tableCol := []string{"Name", "Domain", "Type", "Server"}
table := abraFormatter.CreateTable(tableCol)
table.Append([]string{sanitisedAppName, domain, appType, newAppServer})
table.Append([]string{sanitisedAppName, domain, recipeName, newAppServer})
defer table.Render()
return nil
}