forked from toolshed/abra
refactor(recipe): move GetComposeFiles to new struct
This commit is contained in:
@ -3,6 +3,7 @@ package recipe
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
@ -14,6 +15,50 @@ import (
|
||||
composetypes "github.com/docker/cli/cli/compose/types"
|
||||
)
|
||||
|
||||
// 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 (r Recipe2) GetComposeFiles(appEnv map[string]string) ([]string, error) {
|
||||
composeFileEnvVar, ok := appEnv["COMPOSE_FILE"]
|
||||
if !ok {
|
||||
if err := ensurePathExists(r.ComposePath); err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
log.Debugf("no COMPOSE_FILE detected, loading default: %s", r.ComposePath)
|
||||
return []string{r.ComposePath}, nil
|
||||
}
|
||||
|
||||
if !strings.Contains(composeFileEnvVar, ":") {
|
||||
path := fmt.Sprintf("%s/%s", r.Dir, composeFileEnvVar)
|
||||
if err := ensurePathExists(path); err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
log.Debugf("COMPOSE_FILE detected, loading %s", path)
|
||||
return []string{path}, nil
|
||||
}
|
||||
|
||||
var composeFiles []string
|
||||
|
||||
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", r.Dir, file)
|
||||
if err := ensurePathExists(path); err != nil {
|
||||
return composeFiles, err
|
||||
}
|
||||
composeFiles = append(composeFiles, path)
|
||||
}
|
||||
|
||||
log.Debugf("COMPOSE_FILE detected (%s), loading %s", composeFileEnvVar, strings.Join(envVars, ", "))
|
||||
log.Debugf("retrieved %s configs for %s", strings.Join(composeFiles, ", "), r.Name)
|
||||
|
||||
return composeFiles, nil
|
||||
}
|
||||
|
||||
// UpdateTag updates an image tag in-place on file system local compose files.
|
||||
func (r Recipe2) UpdateTag(image, tag string) (bool, error) {
|
||||
fullPattern := fmt.Sprintf("%s/compose**yml", r.Dir)
|
||||
@ -74,7 +119,7 @@ func (r Recipe2) UpdateTag(image, tag string) (bool, error) {
|
||||
|
||||
log.Debugf("updating %s to %s in %s", old, new, compose.Filename)
|
||||
|
||||
if err := ioutil.WriteFile(compose.Filename, []byte(replacedBytes), 0764); err != nil {
|
||||
if err := os.WriteFile(compose.Filename, []byte(replacedBytes), 0764); err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
|
@ -188,6 +188,7 @@ func Get2(name string) Recipe2 {
|
||||
Dir: dir,
|
||||
SSHURL: fmt.Sprintf(config.SSH_URL_TEMPLATE, name),
|
||||
|
||||
ComposePath: path.Join(dir, "compose.yml"),
|
||||
ReadmePath: path.Join(dir, "README.md"),
|
||||
SampleEnvPath: path.Join(dir, ".env.sample"),
|
||||
}
|
||||
@ -198,6 +199,7 @@ type Recipe2 struct {
|
||||
Dir string
|
||||
SSHURL string
|
||||
|
||||
ComposePath string
|
||||
ReadmePath string
|
||||
SampleEnvPath string
|
||||
}
|
||||
@ -776,50 +778,3 @@ func ensurePathExists(path string) error {
|
||||
}
|
||||
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
|
||||
}
|
||||
log.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
|
||||
}
|
||||
log.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)
|
||||
}
|
||||
|
||||
log.Debugf("COMPOSE_FILE detected (%s), loading %s", composeFileEnvVar, strings.Join(envVars, ", "))
|
||||
log.Debugf("retrieved %s configs for %s", strings.Join(composeFiles, ", "), recipe)
|
||||
|
||||
return composeFiles, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user