bump compose-go version to v1.17.0 to fix issue with depends_on

Signed-off-by: Guillaume Lours <705411+glours@users.noreply.github.com>
This commit is contained in:
Guillaume Lours
2023-07-27 10:35:12 +02:00
committed by CrazyMax
parent 50fbdd86f9
commit f3775c0046
31 changed files with 2317 additions and 352 deletions

View File

@ -17,6 +17,7 @@
package template
import (
"errors"
"fmt"
"regexp"
"sort"
@ -71,77 +72,148 @@ type Mapping func(string) (string, bool)
// the substitution and an error.
type SubstituteFunc func(string, Mapping) (string, bool, error)
// SubstituteWith substitute variables in the string with their values.
// It accepts additional substitute function.
func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, subsFuncs ...SubstituteFunc) (string, error) {
var outerErr error
// ReplacementFunc is a user-supplied function that is apply to the matching
// substring. Returns the value as a string and an error.
type ReplacementFunc func(string, Mapping, *Config) (string, error)
type Config struct {
pattern *regexp.Regexp
substituteFunc SubstituteFunc
replacementFunc ReplacementFunc
logging bool
}
type Option func(*Config)
func WithPattern(pattern *regexp.Regexp) Option {
return func(cfg *Config) {
cfg.pattern = pattern
}
}
func WithSubstitutionFunction(subsFunc SubstituteFunc) Option {
return func(cfg *Config) {
cfg.substituteFunc = subsFunc
}
}
func WithReplacementFunction(replacementFunc ReplacementFunc) Option {
return func(cfg *Config) {
cfg.replacementFunc = replacementFunc
}
}
func WithoutLogging(cfg *Config) {
cfg.logging = false
}
// SubstituteWithOptions substitute variables in the string with their values.
// It accepts additional options such as a custom function or pattern.
func SubstituteWithOptions(template string, mapping Mapping, options ...Option) (string, error) {
var returnErr error
result := pattern.ReplaceAllStringFunc(template, func(substring string) string {
_, subsFunc := getSubstitutionFunctionForTemplate(substring)
if len(subsFuncs) > 0 {
subsFunc = subsFuncs[0]
}
cfg := &Config{
pattern: defaultPattern,
replacementFunc: DefaultReplacementFunc,
logging: true,
}
for _, o := range options {
o(cfg)
}
closingBraceIndex := getFirstBraceClosingIndex(substring)
rest := ""
if closingBraceIndex > -1 {
rest = substring[closingBraceIndex+1:]
substring = substring[0 : closingBraceIndex+1]
}
matches := pattern.FindStringSubmatch(substring)
groups := matchGroups(matches, pattern)
if escaped := groups["escaped"]; escaped != "" {
return escaped
}
braced := false
substitution := groups["named"]
if substitution == "" {
substitution = groups["braced"]
braced = true
}
if substitution == "" {
outerErr = &InvalidTemplateError{Template: template}
result := cfg.pattern.ReplaceAllStringFunc(template, func(substring string) string {
replacement, err := cfg.replacementFunc(substring, mapping, cfg)
if err != nil {
// Add the template for template errors
var tmplErr *InvalidTemplateError
if errors.As(err, &tmplErr) {
if tmplErr.Template == "" {
tmplErr.Template = template
}
}
// Save the first error to be returned
if returnErr == nil {
returnErr = outerErr
returnErr = err
}
return ""
}
if braced {
var (
value string
applied bool
)
value, applied, outerErr = subsFunc(substitution, mapping)
if outerErr != nil {
if returnErr == nil {
returnErr = outerErr
}
return ""
}
if applied {
interpolatedNested, err := SubstituteWith(rest, mapping, pattern)
if err != nil {
return ""
}
return value + interpolatedNested
}
}
value, ok := mapping(substitution)
if !ok {
logrus.Warnf("The %q variable is not set. Defaulting to a blank string.", substitution)
}
return value
return replacement
})
return result, returnErr
}
func DefaultReplacementFunc(substring string, mapping Mapping, cfg *Config) (string, error) {
value, _, err := DefaultReplacementAppliedFunc(substring, mapping, cfg)
return value, err
}
func DefaultReplacementAppliedFunc(substring string, mapping Mapping, cfg *Config) (string, bool, error) {
pattern := cfg.pattern
subsFunc := cfg.substituteFunc
if subsFunc == nil {
_, subsFunc = getSubstitutionFunctionForTemplate(substring)
}
closingBraceIndex := getFirstBraceClosingIndex(substring)
rest := ""
if closingBraceIndex > -1 {
rest = substring[closingBraceIndex+1:]
substring = substring[0 : closingBraceIndex+1]
}
matches := pattern.FindStringSubmatch(substring)
groups := matchGroups(matches, pattern)
if escaped := groups["escaped"]; escaped != "" {
return escaped, true, nil
}
braced := false
substitution := groups["named"]
if substitution == "" {
substitution = groups["braced"]
braced = true
}
if substitution == "" {
return "", false, &InvalidTemplateError{}
}
if braced {
value, applied, err := subsFunc(substitution, mapping)
if err != nil {
return "", false, err
}
if applied {
interpolatedNested, err := SubstituteWith(rest, mapping, pattern)
if err != nil {
return "", false, err
}
return value + interpolatedNested, true, nil
}
}
value, ok := mapping(substitution)
if !ok && cfg.logging {
logrus.Warnf("The %q variable is not set. Defaulting to a blank string.", substitution)
}
return value, ok, nil
}
// SubstituteWith substitute variables in the string with their values.
// It accepts additional substitute function.
func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, subsFuncs ...SubstituteFunc) (string, error) {
options := []Option{
WithPattern(pattern),
}
if len(subsFuncs) > 0 {
options = append(options, WithSubstitutionFunction(subsFuncs[0]))
}
return SubstituteWithOptions(template, mapping, options...)
}
func getSubstitutionFunctionForTemplate(template string) (string, SubstituteFunc) {
interpolationMapping := []struct {
string