WIP: feat: translation support
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
See #483
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package recipe
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
@ -13,6 +14,7 @@ import (
|
||||
loader "coopcloud.tech/abra/pkg/upstream/stack"
|
||||
"github.com/distribution/reference"
|
||||
composetypes "github.com/docker/cli/cli/compose/types"
|
||||
"github.com/leonelquinteros/gotext"
|
||||
)
|
||||
|
||||
// GetComposeFiles gets the list of compose files for an app (or recipe if you
|
||||
@ -24,7 +26,7 @@ func (r Recipe) GetComposeFiles(appEnv map[string]string) ([]string, error) {
|
||||
if err := ensurePathExists(r.ComposePath); err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
log.Debugf("no COMPOSE_FILE detected, loading default: %s", r.ComposePath)
|
||||
log.Debug(gotext.Get("no COMPOSE_FILE detected, loading default: %s", r.ComposePath))
|
||||
return []string{r.ComposePath}, nil
|
||||
}
|
||||
|
||||
@ -33,7 +35,7 @@ func (r Recipe) GetComposeFiles(appEnv map[string]string) ([]string, error) {
|
||||
if err := ensurePathExists(path); err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
log.Debugf("COMPOSE_FILE detected, loading %s", path)
|
||||
log.Debug(gotext.Get("COMPOSE_FILE detected, loading %s", path))
|
||||
return []string{path}, nil
|
||||
}
|
||||
|
||||
@ -42,7 +44,7 @@ func (r Recipe) GetComposeFiles(appEnv map[string]string) ([]string, error) {
|
||||
numComposeFiles := strings.Count(composeFileEnvVar, ":") + 1
|
||||
envVars := strings.SplitN(composeFileEnvVar, ":", numComposeFiles)
|
||||
if len(envVars) != numComposeFiles {
|
||||
return composeFiles, fmt.Errorf("COMPOSE_FILE (=\"%s\") parsing failed?", composeFileEnvVar)
|
||||
return composeFiles, errors.New(gotext.Get("COMPOSE_FILE (=\"%s\") parsing failed?", composeFileEnvVar))
|
||||
}
|
||||
|
||||
for _, file := range envVars {
|
||||
@ -53,8 +55,8 @@ func (r Recipe) GetComposeFiles(appEnv map[string]string) ([]string, error) {
|
||||
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)
|
||||
log.Debug(gotext.Get("COMPOSE_FILE detected (%s), loading %s", composeFileEnvVar, strings.Join(envVars, ", ")))
|
||||
log.Debug(gotext.Get("retrieved %s configs for %s", strings.Join(composeFiles, ", "), r.Name))
|
||||
|
||||
return composeFiles, nil
|
||||
}
|
||||
@ -67,7 +69,7 @@ func (r Recipe) GetComposeConfig(env map[string]string) (*composetypes.Config, e
|
||||
}
|
||||
|
||||
if len(composeFiles) == 0 {
|
||||
return nil, fmt.Errorf("%s is missing a compose.yml or compose.*.yml file?", r.Name)
|
||||
return nil, errors.New(gotext.Get("%s is missing a compose.yml or compose.*.yml file?", r.Name))
|
||||
}
|
||||
|
||||
if env == nil {
|
||||
@ -102,7 +104,7 @@ func (r Recipe) GetVersionLabelLocal() (string, error) {
|
||||
}
|
||||
|
||||
if label == "" {
|
||||
return label, fmt.Errorf("%s has no version label? try running \"abra recipe sync %s\" first?", r.Name, r.Name)
|
||||
return label, errors.New(gotext.Get("%s has no version label? try running \"abra recipe sync %s\" first?", r.Name, r.Name))
|
||||
}
|
||||
|
||||
return label, nil
|
||||
@ -118,7 +120,7 @@ func (r Recipe) UpdateTag(image, tag string) (bool, error) {
|
||||
return false, err
|
||||
}
|
||||
|
||||
log.Debugf("considering %s config(s) for tag update", strings.Join(composeFiles, ", "))
|
||||
log.Debug(gotext.Get("considering %s config(s) for tag update", strings.Join(composeFiles, ", ")))
|
||||
|
||||
for _, composeFile := range composeFiles {
|
||||
opts := stack.Deploy{Composefiles: []string{composeFile}}
|
||||
@ -148,13 +150,13 @@ func (r Recipe) UpdateTag(image, tag string) (bool, error) {
|
||||
case reference.NamedTagged:
|
||||
composeTag = img.(reference.NamedTagged).Tag()
|
||||
default:
|
||||
log.Debugf("unable to parse %s, skipping", img)
|
||||
log.Debug(gotext.Get("unable to parse %s, skipping", img))
|
||||
continue
|
||||
}
|
||||
|
||||
composeImage := formatter.StripTagMeta(reference.Path(img))
|
||||
|
||||
log.Debugf("parsed %s from %s", composeTag, service.Image)
|
||||
log.Debug(gotext.Get("parsed %s from %s", composeTag, service.Image))
|
||||
|
||||
if image == composeImage {
|
||||
bytes, err := ioutil.ReadFile(composeFile)
|
||||
@ -166,7 +168,7 @@ func (r Recipe) UpdateTag(image, tag string) (bool, error) {
|
||||
new := fmt.Sprintf("%s:%s", composeImage, tag)
|
||||
replacedBytes := strings.Replace(string(bytes), old, new, -1)
|
||||
|
||||
log.Debugf("updating %s to %s in %s", old, new, compose.Filename)
|
||||
log.Debug(gotext.Get("updating %s to %s in %s", old, new, compose.Filename))
|
||||
|
||||
if err := os.WriteFile(compose.Filename, []byte(replacedBytes), 0o764); err != nil {
|
||||
return false, err
|
||||
@ -186,7 +188,7 @@ func (r Recipe) UpdateLabel(pattern, serviceName, label string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Debugf("considering %s config(s) for label update", strings.Join(composeFiles, ", "))
|
||||
log.Debug(gotext.Get("considering %s config(s) for label update", strings.Join(composeFiles, ", ")))
|
||||
|
||||
for _, composeFile := range composeFiles {
|
||||
opts := stack.Deploy{Composefiles: []string{composeFile}}
|
||||
@ -224,27 +226,27 @@ func (r Recipe) UpdateLabel(pattern, serviceName, label string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
old := fmt.Sprintf("coop-cloud.${STACK_NAME}.version=%s", value)
|
||||
old := gotext.Get("coop-cloud.${STACK_NAME}.version=%s", value)
|
||||
replacedBytes := strings.Replace(string(bytes), old, label, -1)
|
||||
|
||||
if old == label {
|
||||
log.Warnf("%s is already set, nothing to do?", label)
|
||||
log.Warnf(gotext.Get("%s is already set, nothing to do?", label))
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Debugf("updating %s to %s in %s", old, label, compose.Filename)
|
||||
log.Debug(gotext.Get("updating %s to %s in %s", old, label, compose.Filename))
|
||||
|
||||
if err := ioutil.WriteFile(compose.Filename, []byte(replacedBytes), 0o764); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Infof("synced label %s to service %s", label, serviceName)
|
||||
log.Infof(gotext.Get("synced label %s to service %s", label, serviceName))
|
||||
}
|
||||
}
|
||||
|
||||
if !discovered {
|
||||
log.Warn("no existing label found, automagic insertion not supported yet")
|
||||
log.Fatalf("add '- \"%s\"' manually to the 'app' service in %s", label, composeFile)
|
||||
log.Warn(gotext.Get("no existing label found, automagic insertion not supported yet"))
|
||||
log.Fatal(gotext.Get("add '- \"%s\"' manually to the 'app' service in %s", label, composeFile))
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user