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

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

View File

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

View File

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

View File

@ -7,7 +7,6 @@ import (
"path"
"coopcloud.tech/abra/pkg/config"
"coopcloud.tech/abra/pkg/envfile"
"coopcloud.tech/abra/pkg/log"
"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
}
func LintEnvConfigPresent(recipe recipe.Recipe) (bool, error) {
envSample := fmt.Sprintf("%s/%s/.env.sample", config.RECIPES_DIR, recipe.Name)
if _, err := os.Stat(envSample); !os.IsNotExist(err) {
func LintEnvConfigPresent(r recipe.Recipe) (bool, error) {
r2 := recipe.Get2(r.Name)
if _, err := os.Stat(r2.SampleEnvPath); !os.IsNotExist(err) {
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
// the recipe. This typically means that no domain is required to deploy and
// therefore no matching traefik deploy label will be present.
func LintTraefikEnabledSkipCondition(recipe recipe.Recipe) (bool, error) {
envSamplePath := path.Join(config.RECIPES_DIR, recipe.Name, ".env.sample")
sampleEnv, err := envfile.ReadEnv(envSamplePath)
func LintTraefikEnabledSkipCondition(r recipe.Recipe) (bool, error) {
r2 := recipe.Get2(r.Name)
sampleEnv, err := r2.SampleEnv()
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 {

View File

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

View File

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

View File

@ -13,9 +13,7 @@ import (
"strings"
"coopcloud.tech/abra/pkg/catalogue"
"coopcloud.tech/abra/pkg/compose"
"coopcloud.tech/abra/pkg/config"
"coopcloud.tech/abra/pkg/envfile"
"coopcloud.tech/abra/pkg/formatter"
gitPkg "coopcloud.tech/abra/pkg/git"
"coopcloud.tech/abra/pkg/limit"
@ -143,29 +141,6 @@ func (r Recipe) Dir() string {
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
func (r Recipe) Tags() ([]string, error) {
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)
}
envSamplePath := path.Join(config.RECIPES_DIR, r.Name, ".env.sample")
sampleEnv, err := envfile.ReadEnv(envSamplePath)
sampleEnv, err := r.SampleEnv()
if err != nil {
return Recipe{}, err
}