diff --git a/pkg/app/app.go b/pkg/app/app.go index 2e250916..2a2289c5 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -478,61 +478,6 @@ func GetAppStatuses(apps []App, MachineReadable bool) (map[string]map[string]str return statuses, nil } -// ensurePathExists ensures that a path exists. -func ensurePathExists(path string) error { - if _, err := os.Stat(path); err != nil && os.IsNotExist(err) { - return err - } - return nil -} - -// GetComposeFiles gets the list of compose files for an app (or recipe if you -// don't already have an app) which should be merged into a composetypes.Config -// while respecting the COMPOSE_FILE env var. -func GetComposeFiles(recipe string, appEnv AppEnv) ([]string, error) { - var composeFiles []string - - composeFileEnvVar, ok := appEnv["COMPOSE_FILE"] - if !ok { - path := fmt.Sprintf("%s/%s/compose.yml", config.RECIPES_DIR, recipe) - if err := ensurePathExists(path); err != nil { - return composeFiles, err - } - logrus.Debugf("no COMPOSE_FILE detected, loading default: %s", path) - composeFiles = append(composeFiles, path) - return composeFiles, nil - } - - if !strings.Contains(composeFileEnvVar, ":") { - path := fmt.Sprintf("%s/%s/%s", config.RECIPES_DIR, recipe, composeFileEnvVar) - if err := ensurePathExists(path); err != nil { - return composeFiles, err - } - logrus.Debugf("COMPOSE_FILE detected, loading %s", path) - composeFiles = append(composeFiles, path) - return composeFiles, nil - } - - numComposeFiles := strings.Count(composeFileEnvVar, ":") + 1 - envVars := strings.SplitN(composeFileEnvVar, ":", numComposeFiles) - if len(envVars) != numComposeFiles { - return composeFiles, fmt.Errorf("COMPOSE_FILE (=\"%s\") parsing failed?", composeFileEnvVar) - } - - for _, file := range envVars { - path := fmt.Sprintf("%s/%s/%s", config.RECIPES_DIR, recipe, file) - if err := ensurePathExists(path); err != nil { - return composeFiles, err - } - composeFiles = append(composeFiles, path) - } - - logrus.Debugf("COMPOSE_FILE detected (%s), loading %s", composeFileEnvVar, strings.Join(envVars, ", ")) - logrus.Debugf("retrieved %s configs for %s", strings.Join(composeFiles, ", "), recipe) - - return composeFiles, nil -} - // GetAppComposeConfig retrieves a compose specification for a recipe. This // specification is the result of a merge of all the compose.**.yml files in // the recipe repository. diff --git a/pkg/recipe/recipe.go b/pkg/recipe/recipe.go index 3f4111ad..465a8529 100644 --- a/pkg/recipe/recipe.go +++ b/pkg/recipe/recipe.go @@ -1055,3 +1055,58 @@ func UpdateRepositories(repos RepoCatalogue, recipeName string) error { func getReposTopicUrl(repoName string) string { return fmt.Sprintf("https://git.coopcloud.tech/api/v1/repos/coop-cloud/%s/topics", repoName) } + +// ensurePathExists ensures that a path exists. +func ensurePathExists(path string) error { + if _, err := os.Stat(path); err != nil && os.IsNotExist(err) { + return err + } + return nil +} + +// GetComposeFiles gets the list of compose files for an app (or recipe if you +// don't already have an app) which should be merged into a composetypes.Config +// while respecting the COMPOSE_FILE env var. +func GetComposeFiles(recipe string, appEnv map[string]string) ([]string, error) { + var composeFiles []string + + composeFileEnvVar, ok := appEnv["COMPOSE_FILE"] + if !ok { + path := fmt.Sprintf("%s/%s/compose.yml", config.RECIPES_DIR, recipe) + if err := ensurePathExists(path); err != nil { + return composeFiles, err + } + logrus.Debugf("no COMPOSE_FILE detected, loading default: %s", path) + composeFiles = append(composeFiles, path) + return composeFiles, nil + } + + if !strings.Contains(composeFileEnvVar, ":") { + path := fmt.Sprintf("%s/%s/%s", config.RECIPES_DIR, recipe, composeFileEnvVar) + if err := ensurePathExists(path); err != nil { + return composeFiles, err + } + logrus.Debugf("COMPOSE_FILE detected, loading %s", path) + composeFiles = append(composeFiles, path) + return composeFiles, nil + } + + numComposeFiles := strings.Count(composeFileEnvVar, ":") + 1 + envVars := strings.SplitN(composeFileEnvVar, ":", numComposeFiles) + if len(envVars) != numComposeFiles { + return composeFiles, fmt.Errorf("COMPOSE_FILE (=\"%s\") parsing failed?", composeFileEnvVar) + } + + for _, file := range envVars { + path := fmt.Sprintf("%s/%s/%s", config.RECIPES_DIR, recipe, file) + if err := ensurePathExists(path); err != nil { + return composeFiles, err + } + composeFiles = append(composeFiles, path) + } + + logrus.Debugf("COMPOSE_FILE detected (%s), loading %s", composeFileEnvVar, strings.Join(envVars, ", ")) + logrus.Debugf("retrieved %s configs for %s", strings.Join(composeFiles, ", "), recipe) + + return composeFiles, nil +}