refactor(recipe): use method or variable for .env.sample

This commit is contained in:
p4u1 2024-07-08 11:26:01 +02:00
parent 2f41b6d8b4
commit c861c09cce
11 changed files with 47 additions and 85 deletions

View File

@ -2,7 +2,6 @@ package app
import ( import (
"fmt" "fmt"
"path"
"coopcloud.tech/abra/cli/internal" "coopcloud.tech/abra/cli/internal"
appPkg "coopcloud.tech/abra/pkg/app" appPkg "coopcloud.tech/abra/pkg/app"
@ -121,7 +120,7 @@ var appNewCommand = cli.Command{
log.Debugf("%s sanitised as %s for new app", internal.Domain, sanitisedAppName) log.Debugf("%s sanitised as %s for new app", internal.Domain, sanitisedAppName)
if err := appPkg.TemplateAppEnvSample( if err := appPkg.TemplateAppEnvSample(
recipe.Name, r,
internal.Domain, internal.Domain,
internal.NewAppServer, internal.NewAppServer,
internal.Domain, internal.Domain,
@ -142,8 +141,7 @@ var appNewCommand = cli.Command{
log.Fatal(err) log.Fatal(err)
} }
envSamplePath := path.Join(config.RECIPES_DIR, recipe.Name, ".env.sample") secretsConfig, err := secret.ReadSecretsConfig(r.SampleEnvPath, composeFiles, appPkg.StackName(internal.Domain))
secretsConfig, err := secret.ReadSecretsConfig(envSamplePath, composeFiles, appPkg.StackName(internal.Domain))
if err != nil { if err != nil {
return err return err
} }

View File

@ -12,6 +12,7 @@ import (
"coopcloud.tech/abra/pkg/config" "coopcloud.tech/abra/pkg/config"
"coopcloud.tech/abra/pkg/git" "coopcloud.tech/abra/pkg/git"
"coopcloud.tech/abra/pkg/log" "coopcloud.tech/abra/pkg/log"
"coopcloud.tech/abra/pkg/recipe"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -55,6 +56,7 @@ recipe and domain in the sample environment config).
`, `,
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
recipeName := c.Args().First() recipeName := c.Args().First()
r := recipe.Get2(recipeName)
if recipeName == "" { if recipeName == "" {
internal.ShowSubcommandHelpAndError(c, errors.New("no recipe name provided")) internal.ShowSubcommandHelpAndError(c, errors.New("no recipe name provided"))
@ -80,7 +82,7 @@ recipe and domain in the sample environment config).
toParse := []string{ toParse := []string{
path.Join(config.RECIPES_DIR, recipeName, "README.md"), path.Join(config.RECIPES_DIR, recipeName, "README.md"),
path.Join(config.RECIPES_DIR, recipeName, ".env.sample"), r.SampleEnvPath,
} }
for _, path := range toParse { for _, path := range toParse {
tpl, err := template.ParseFiles(path) tpl, err := template.ParseFiles(path)

View File

@ -10,6 +10,7 @@ import (
"coopcloud.tech/abra/pkg/config" "coopcloud.tech/abra/pkg/config"
gitPkg "coopcloud.tech/abra/pkg/git" gitPkg "coopcloud.tech/abra/pkg/git"
"coopcloud.tech/abra/pkg/log" "coopcloud.tech/abra/pkg/log"
recipePkg "coopcloud.tech/abra/pkg/recipe"
"coopcloud.tech/tagcmp" "coopcloud.tech/tagcmp"
"github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2"
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
@ -44,6 +45,7 @@ local file system.
BashComplete: autocomplete.RecipeNameComplete, BashComplete: autocomplete.RecipeNameComplete,
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
recipe := internal.ValidateRecipe(c) recipe := internal.ValidateRecipe(c)
r := recipePkg.Get2(recipe.Name)
mainApp, err := internal.GetMainAppImage(recipe) mainApp, err := internal.GetMainAppImage(recipe)
if err != nil { if err != nil {
@ -192,7 +194,7 @@ likely to change.
mainService := "app" mainService := "app"
label := fmt.Sprintf("coop-cloud.${STACK_NAME}.version=%s", nextTag) label := fmt.Sprintf("coop-cloud.${STACK_NAME}.version=%s", nextTag)
if !internal.Dry { if !internal.Dry {
if err := recipe.UpdateLabel("compose.y*ml", mainService, label); err != nil { if err := r.UpdateLabel("compose.y*ml", mainService, label); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} else { } else {

View File

@ -294,7 +294,7 @@ You may invoke this command in "wizard" mode and be prompted for input:
} }
} }
if upgradeTag != "skip" { if upgradeTag != "skip" {
ok, err := recipe.UpdateTag(image, upgradeTag) ok, err := r.UpdateTag(image, upgradeTag)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -7,9 +7,9 @@ import (
"coopcloud.tech/abra/cli/internal" "coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/pkg/autocomplete" "coopcloud.tech/abra/pkg/autocomplete"
"coopcloud.tech/abra/pkg/formatter" "coopcloud.tech/abra/pkg/formatter"
"coopcloud.tech/abra/pkg/log"
recipePkg "coopcloud.tech/abra/pkg/recipe" recipePkg "coopcloud.tech/abra/pkg/recipe"
"github.com/olekukonko/tablewriter" "github.com/olekukonko/tablewriter"
"github.com/sirupsen/logrus"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -42,16 +42,16 @@ var recipeVersionCommand = cli.Command{
catl, err := recipePkg.ReadRecipeCatalogue(internal.Offline) catl, err := recipePkg.ReadRecipeCatalogue(internal.Offline)
if err != nil { if err != nil {
log.Fatal(err) logrus.Fatal(err)
} }
recipeMeta, ok := catl[recipe.Name] recipeMeta, ok := catl[recipe.Name]
if !ok { if !ok {
log.Fatalf("%s is not published on the catalogue?", recipe.Name) logrus.Fatalf("%s is not published on the catalogue?", recipe.Name)
} }
if len(recipeMeta.Versions) == 0 { if len(recipeMeta.Versions) == 0 {
log.Fatalf("%s has no catalogue published versions?", recipe.Name) logrus.Fatalf("%s has no catalogue published versions?", recipe.Name)
} }
tableCols := []string{"version", "service", "image", "tag"} tableCols := []string{"version", "service", "image", "tag"}

View File

@ -358,9 +358,8 @@ func GetAppNames() ([]string, error) {
// TemplateAppEnvSample copies the example env file for the app into the users // TemplateAppEnvSample copies the example env file for the app into the users
// env files. // env files.
func TemplateAppEnvSample(recipeName, appName, server, domain string) error { func TemplateAppEnvSample(r recipe.Recipe2, appName, server, domain string) error {
envSamplePath := path.Join(config.RECIPES_DIR, recipeName, ".env.sample") envSample, err := os.ReadFile(r.SampleEnvPath)
envSample, err := os.ReadFile(envSamplePath)
if err != nil { if err != nil {
return err return err
} }
@ -380,14 +379,14 @@ func TemplateAppEnvSample(recipeName, appName, server, domain string) error {
return err return err
} }
newContents := strings.Replace(string(read), recipeName+".example.com", domain, -1) newContents := strings.Replace(string(read), r.Name+".example.com", domain, -1)
err = os.WriteFile(appEnvPath, []byte(newContents), 0) err = os.WriteFile(appEnvPath, []byte(newContents), 0)
if err != nil { if err != nil {
return err return err
} }
log.Debugf("copied & templated %s to %s", envSamplePath, appEnvPath) log.Debugf("copied & templated %s to %s", r.SampleEnvPath, appEnvPath)
return nil return nil
} }
@ -511,15 +510,8 @@ func ExposeAllEnv(stackName string, compose *composetypes.Config, appEnv envfile
func CheckEnv(app App) ([]envfile.EnvVar, error) { func CheckEnv(app App) ([]envfile.EnvVar, error) {
var envVars []envfile.EnvVar var envVars []envfile.EnvVar
envSamplePath := path.Join(config.RECIPES_DIR, app.Recipe, ".env.sample") r := recipe.Get2(app.Recipe)
if _, err := os.Stat(envSamplePath); err != nil { envSample, err := r.SampleEnv()
if os.IsNotExist(err) {
return envVars, fmt.Errorf("%s does not exist?", envSamplePath)
}
return envVars, err
}
envSample, err := envfile.ReadEnv(envSamplePath)
if err != nil { if err != nil {
return envVars, err return envVars, err
} }

View File

@ -2,7 +2,6 @@ package envfile_test
import ( import (
"fmt" "fmt"
"path"
"reflect" "reflect"
"slices" "slices"
"strings" "strings"
@ -117,8 +116,8 @@ func TestCheckEnv(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
envSamplePath := path.Join(config.RECIPES_DIR, r.Name, ".env.sample") r2 := recipe.Get2(r.Name)
envSample, err := envfile.ReadEnv(envSamplePath) envSample, err := r2.SampleEnv()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -151,8 +150,8 @@ func TestCheckEnvError(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
envSamplePath := path.Join(config.RECIPES_DIR, r.Name, ".env.sample") r2 := recipe.Get2(r.Name)
envSample, err := envfile.ReadEnv(envSamplePath) envSample, err := r2.SampleEnv()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -187,8 +186,8 @@ func TestEnvVarCommentsRemoved(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
envSamplePath := path.Join(config.RECIPES_DIR, r.Name, ".env.sample") r2 := recipe.Get2(r.Name)
envSample, err := envfile.ReadEnv(envSamplePath) envSample, err := r2.SampleEnv()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -219,8 +218,8 @@ func TestEnvVarModifiersIncluded(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
envSamplePath := path.Join(config.RECIPES_DIR, r.Name, ".env.sample") r2 := recipe.Get2(r.Name)
envSample, modifiers, err := envfile.ReadEnvWithModifiers(envSamplePath) envSample, modifiers, err := envfile.ReadEnvWithModifiers(r2.SampleEnvPath)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -7,7 +7,6 @@ import (
"path" "path"
"coopcloud.tech/abra/pkg/config" "coopcloud.tech/abra/pkg/config"
"coopcloud.tech/abra/pkg/envfile"
"coopcloud.tech/abra/pkg/log" "coopcloud.tech/abra/pkg/log"
"coopcloud.tech/abra/pkg/recipe" "coopcloud.tech/abra/pkg/recipe"
recipePkg "coopcloud.tech/abra/pkg/recipe" recipePkg "coopcloud.tech/abra/pkg/recipe"
@ -210,9 +209,9 @@ func LintComposeVersion(recipe recipe.Recipe) (bool, error) {
return true, nil return true, nil
} }
func LintEnvConfigPresent(recipe recipe.Recipe) (bool, error) { func LintEnvConfigPresent(r recipe.Recipe) (bool, error) {
envSample := fmt.Sprintf("%s/%s/.env.sample", config.RECIPES_DIR, recipe.Name) r2 := recipe.Get2(r.Name)
if _, err := os.Stat(envSample); !os.IsNotExist(err) { if _, err := os.Stat(r2.SampleEnvPath); !os.IsNotExist(err) {
return true, nil return true, nil
} }
@ -233,11 +232,11 @@ func LintAppService(recipe recipe.Recipe) (bool, error) {
// confirms that there is no "DOMAIN=..." in the .env.sample configuration of // confirms that there is no "DOMAIN=..." in the .env.sample configuration of
// the recipe. This typically means that no domain is required to deploy and // the recipe. This typically means that no domain is required to deploy and
// therefore no matching traefik deploy label will be present. // therefore no matching traefik deploy label will be present.
func LintTraefikEnabledSkipCondition(recipe recipe.Recipe) (bool, error) { func LintTraefikEnabledSkipCondition(r recipe.Recipe) (bool, error) {
envSamplePath := path.Join(config.RECIPES_DIR, recipe.Name, ".env.sample") r2 := recipe.Get2(r.Name)
sampleEnv, err := envfile.ReadEnv(envSamplePath) sampleEnv, err := r2.SampleEnv()
if err != nil { if err != nil {
return false, fmt.Errorf("Unable to discover .env.sample for %s", recipe.Name) return false, fmt.Errorf("Unable to discover .env.sample for %s", r2.Name)
} }
if _, ok := sampleEnv["DOMAIN"]; !ok { if _, ok := sampleEnv["DOMAIN"]; !ok {

View File

@ -1,14 +1,11 @@
package compose package recipe
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"path"
"path/filepath" "path/filepath"
"strings" "strings"
"coopcloud.tech/abra/pkg/config"
"coopcloud.tech/abra/pkg/envfile"
"coopcloud.tech/abra/pkg/formatter" "coopcloud.tech/abra/pkg/formatter"
"coopcloud.tech/abra/pkg/log" "coopcloud.tech/abra/pkg/log"
"coopcloud.tech/abra/pkg/upstream/stack" "coopcloud.tech/abra/pkg/upstream/stack"
@ -18,8 +15,11 @@ import (
) )
// UpdateTag updates an image tag in-place on file system local compose files. // UpdateTag updates an image tag in-place on file system local compose files.
func UpdateTag(pattern, image, tag, recipeName string) (bool, error) { func (r Recipe2) UpdateTag(image, tag string) (bool, error) {
composeFiles, err := filepath.Glob(pattern) fullPattern := fmt.Sprintf("%s/compose**yml", r.Dir)
image = formatter.StripTagMeta(image)
composeFiles, err := filepath.Glob(fullPattern)
if err != nil { if err != nil {
return false, err return false, err
} }
@ -29,8 +29,7 @@ func UpdateTag(pattern, image, tag, recipeName string) (bool, error) {
for _, composeFile := range composeFiles { for _, composeFile := range composeFiles {
opts := stack.Deploy{Composefiles: []string{composeFile}} opts := stack.Deploy{Composefiles: []string{composeFile}}
envSamplePath := path.Join(config.RECIPES_DIR, recipeName, ".env.sample") sampleEnv, err := r.SampleEnv()
sampleEnv, err := envfile.ReadEnv(envSamplePath)
if err != nil { if err != nil {
return false, err return false, err
} }
@ -86,8 +85,9 @@ func UpdateTag(pattern, image, tag, recipeName string) (bool, error) {
} }
// UpdateLabel updates a label in-place on file system local compose files. // UpdateLabel updates a label in-place on file system local compose files.
func UpdateLabel(pattern, serviceName, label, recipeName string) error { func (r Recipe2) UpdateLabel(pattern, serviceName, label string) error {
composeFiles, err := filepath.Glob(pattern) fullPattern := fmt.Sprintf("%s/%s", r.Dir, pattern)
composeFiles, err := filepath.Glob(fullPattern)
if err != nil { if err != nil {
return err return err
} }
@ -97,8 +97,7 @@ func UpdateLabel(pattern, serviceName, label, recipeName string) error {
for _, composeFile := range composeFiles { for _, composeFile := range composeFiles {
opts := stack.Deploy{Composefiles: []string{composeFile}} opts := stack.Deploy{Composefiles: []string{composeFile}}
envSamplePath := path.Join(config.RECIPES_DIR, recipeName, ".env.sample") sampleEnv, err := r.SampleEnv()
sampleEnv, err := envfile.ReadEnv(envSamplePath)
if err != nil { if err != nil {
return err return err
} }

View File

@ -2,15 +2,12 @@ package recipe
import ( import (
"fmt" "fmt"
"path"
"coopcloud.tech/abra/pkg/config"
"coopcloud.tech/abra/pkg/envfile" "coopcloud.tech/abra/pkg/envfile"
) )
func (r Recipe2) SampleEnv() (map[string]string, error) { func (r Recipe2) SampleEnv() (map[string]string, error) {
envSamplePath := path.Join(config.RECIPES_DIR, r.Name, ".env.sample") sampleEnv, err := envfile.ReadEnv(r.SampleEnvPath)
sampleEnv, err := envfile.ReadEnv(envSamplePath)
if err != nil { if err != nil {
return sampleEnv, fmt.Errorf("unable to discover .env.sample for %s", r.Name) return sampleEnv, fmt.Errorf("unable to discover .env.sample for %s", r.Name)
} }

View File

@ -13,9 +13,7 @@ import (
"strings" "strings"
"coopcloud.tech/abra/pkg/catalogue" "coopcloud.tech/abra/pkg/catalogue"
"coopcloud.tech/abra/pkg/compose"
"coopcloud.tech/abra/pkg/config" "coopcloud.tech/abra/pkg/config"
"coopcloud.tech/abra/pkg/envfile"
"coopcloud.tech/abra/pkg/formatter" "coopcloud.tech/abra/pkg/formatter"
gitPkg "coopcloud.tech/abra/pkg/git" gitPkg "coopcloud.tech/abra/pkg/git"
"coopcloud.tech/abra/pkg/limit" "coopcloud.tech/abra/pkg/limit"
@ -143,29 +141,6 @@ func (r Recipe) Dir() string {
return path.Join(config.RECIPES_DIR, r.Name) return path.Join(config.RECIPES_DIR, r.Name)
} }
// UpdateLabel updates a recipe label
func (r Recipe) UpdateLabel(pattern, serviceName, label string) error {
fullPattern := fmt.Sprintf("%s/%s/%s", config.RECIPES_DIR, r.Name, pattern)
if err := compose.UpdateLabel(fullPattern, serviceName, label, r.Name); err != nil {
return err
}
return nil
}
// UpdateTag updates a recipe tag
func (r Recipe) UpdateTag(image, tag string) (bool, error) {
pattern := fmt.Sprintf("%s/%s/compose**yml", config.RECIPES_DIR, r.Name)
image = formatter.StripTagMeta(image)
ok, err := compose.UpdateTag(pattern, image, tag, r.Name)
if err != nil {
return false, err
}
return ok, nil
}
// Tags list the recipe tags // Tags list the recipe tags
func (r Recipe) Tags() ([]string, error) { func (r Recipe) Tags() ([]string, error) {
var tags []string var tags []string
@ -209,8 +184,7 @@ func Get(recipeName string, offline bool) (Recipe, error) {
return Recipe{}, fmt.Errorf("%s is missing a compose.yml or compose.*.yml file?", r.Name) return Recipe{}, fmt.Errorf("%s is missing a compose.yml or compose.*.yml file?", r.Name)
} }
envSamplePath := path.Join(config.RECIPES_DIR, r.Name, ".env.sample") sampleEnv, err := r.SampleEnv()
sampleEnv, err := envfile.ReadEnv(envSamplePath)
if err != nil { if err != nil {
return Recipe{}, err return Recipe{}, err
} }