vendor: update to compose-go 1.13.4

Signed-off-by: Nick Sieger <nick@nicksieger.com>
This commit is contained in:
Nick Sieger
2023-04-21 10:52:58 -05:00
parent afcaa8df5f
commit 956a1be656
34 changed files with 1130 additions and 770 deletions

View File

@ -17,29 +17,65 @@
package cli
import (
"fmt"
"bytes"
"io"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/compose-spec/compose-go/consts"
"github.com/compose-spec/compose-go/dotenv"
"github.com/compose-spec/compose-go/errdefs"
"github.com/compose-spec/compose-go/loader"
"github.com/compose-spec/compose-go/types"
"github.com/compose-spec/compose-go/utils"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
// ProjectOptions groups the command line options recommended for a Compose implementation
// ProjectOptions provides common configuration for loading a project.
type ProjectOptions struct {
Name string
WorkingDir string
// Name is a valid Compose project name to be used or empty.
//
// If empty, the project loader will automatically infer a reasonable
// project name if possible.
Name string
// WorkingDir is a file path to use as the project directory or empty.
//
// If empty, the project loader will automatically infer a reasonable
// working directory if possible.
WorkingDir string
// ConfigPaths are file paths to one or more Compose files.
//
// These are applied in order by the loader following the merge logic
// as described in the spec.
//
// The first entry is required and is the primary Compose file.
// For convenience, WithConfigFileEnv and WithDefaultConfigPath
// are provided to populate this in a predictable manner.
ConfigPaths []string
// Environment are additional environment variables to make available
// for interpolation.
//
// NOTE: For security, the loader does not automatically expose any
// process environment variables. For convenience, WithOsEnv can be
// used if appropriate.
Environment map[string]string
EnvFile string
// EnvFiles are file paths to ".env" files with additional environment
// variable data.
//
// These are loaded in-order, so it is possible to override variables or
// in subsequent files.
//
// This field is optional, but any file paths that are included here must
// exist or an error will be returned during load.
EnvFiles []string
loadOptions []func(*loader.Options)
}
@ -63,8 +99,12 @@ func NewProjectOptions(configs []string, opts ...ProjectOptionsFn) (*ProjectOpti
// WithName defines ProjectOptions' name
func WithName(name string) ProjectOptionsFn {
return func(o *ProjectOptions) error {
// a project (once loaded) cannot have an empty name
// however, on the options object, the name is optional: if unset,
// a name will be inferred by the loader, so it's legal to set the
// name to an empty string here
if name != loader.NormalizeProjectName(name) {
return fmt.Errorf("%q is not a valid project name", name)
return loader.InvalidProjectNameErr(name)
}
o.Name = name
return nil
@ -187,9 +227,19 @@ func WithOsEnv(o *ProjectOptions) error {
}
// WithEnvFile set an alternate env file
// deprecated - use WithEnvFiles
func WithEnvFile(file string) ProjectOptionsFn {
var files []string
if file != "" {
files = []string{file}
}
return WithEnvFiles(files...)
}
// WithEnvFiles set alternate env files
func WithEnvFiles(file ...string) ProjectOptionsFn {
return func(options *ProjectOptions) error {
options.EnvFile = file
options.EnvFiles = file
return nil
}
}
@ -200,7 +250,7 @@ func WithDotEnv(o *ProjectOptions) error {
if err != nil {
return err
}
envMap, err := GetEnvFromFile(o.Environment, wd, o.EnvFile)
envMap, err := GetEnvFromFile(o.Environment, wd, o.EnvFiles)
if err != nil {
return err
}
@ -213,55 +263,63 @@ func WithDotEnv(o *ProjectOptions) error {
return nil
}
func GetEnvFromFile(currentEnv map[string]string, workingDir string, filename string) (map[string]string, error) {
func GetEnvFromFile(currentEnv map[string]string, workingDir string, filenames []string) (map[string]string, error) {
envMap := make(map[string]string)
dotEnvFile := filename
if dotEnvFile == "" {
dotEnvFile = filepath.Join(workingDir, ".env")
dotEnvFiles := filenames
if len(dotEnvFiles) == 0 {
dotEnvFiles = append(dotEnvFiles, filepath.Join(workingDir, ".env"))
}
abs, err := filepath.Abs(dotEnvFile)
if err != nil {
return envMap, err
}
dotEnvFile = abs
s, err := os.Stat(dotEnvFile)
if os.IsNotExist(err) {
if filename != "" {
return nil, errors.Errorf("Couldn't find env file: %s", filename)
for _, dotEnvFile := range dotEnvFiles {
abs, err := filepath.Abs(dotEnvFile)
if err != nil {
return envMap, err
}
return envMap, nil
}
if err != nil {
return envMap, err
}
dotEnvFile = abs
if s.IsDir() {
if filename == "" {
return envMap, nil
s, err := os.Stat(dotEnvFile)
if os.IsNotExist(err) {
if len(filenames) == 0 {
return envMap, nil
}
return envMap, errors.Errorf("Couldn't find env file: %s", dotEnvFile)
}
return envMap, errors.Errorf("%s is a directory", dotEnvFile)
}
file, err := os.Open(dotEnvFile)
if err != nil {
return envMap, errors.Wrapf(err, "failed to read %s", dotEnvFile)
}
defer file.Close()
env, err := dotenv.ParseWithLookup(file, func(k string) (string, bool) {
v, ok := currentEnv[k]
if !ok {
return "", false
if err != nil {
return envMap, err
}
if s.IsDir() {
if len(filenames) == 0 {
return envMap, nil
}
return envMap, errors.Errorf("%s is a directory", dotEnvFile)
}
b, err := os.ReadFile(dotEnvFile)
if os.IsNotExist(err) {
return nil, errors.Errorf("Couldn't read env file: %s", dotEnvFile)
}
if err != nil {
return envMap, err
}
env, err := dotenv.ParseWithLookup(bytes.NewReader(b), func(k string) (string, bool) {
v, ok := envMap[k]
if ok {
return v, true
}
v, ok = currentEnv[k]
if !ok {
return "", false
}
return v, true
})
if err != nil {
return envMap, errors.Wrapf(err, "failed to read %s", dotEnvFile)
}
for k, v := range env {
envMap[k] = v
}
return v, true
})
if err != nil {
return envMap, errors.Wrapf(err, "failed to read %s", dotEnvFile)
}
for k, v := range env {
envMap[k] = v
}
return envMap, nil
@ -393,7 +451,10 @@ func withNamePrecedenceLoad(absWorkingDir string, options *ProjectOptions) func(
} else if nameFromEnv, ok := options.Environment[consts.ComposeProjectName]; ok && nameFromEnv != "" {
opts.SetProjectName(nameFromEnv, true)
} else {
opts.SetProjectName(filepath.Base(absWorkingDir), false)
opts.SetProjectName(
loader.NormalizeProjectName(filepath.Base(absWorkingDir)),
false,
)
}
}
}