vendor: update compose-go to v2.0.0-rc.3

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2024-01-31 14:15:41 +01:00
committed by CrazyMax
parent d0c4bed484
commit 13beda8b11
97 changed files with 5770 additions and 2719 deletions

View File

@ -0,0 +1,44 @@
/*
Copyright 2020 The Compose Specification Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package paths
import "strings"
func (r *relativePathsResolver) absContextPath(value any) (any, error) {
v := value.(string)
if strings.Contains(v, "://") { // `docker-image://` or any builder specific context type
return v, nil
}
if isRemoteContext(v) {
return v, nil
}
return r.absPath(v)
}
// isRemoteContext returns true if the value is a Git reference or HTTP(S) URL.
//
// Any other value is assumed to be a local filesystem path and returns false.
//
// See: https://github.com/moby/buildkit/blob/18fc875d9bfd6e065cd8211abc639434ba65aa56/frontend/dockerui/context.go#L76-L79
func isRemoteContext(maybeURL string) bool {
for _, prefix := range []string{"https://", "http://", "git://", "ssh://", "github.com/", "git@"} {
if strings.HasPrefix(maybeURL, prefix) {
return true
}
}
return false
}

View File

@ -0,0 +1,25 @@
/*
Copyright 2020 The Compose Specification Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package paths
func (r *relativePathsResolver) absExtendsPath(value any) (any, error) {
v := value.(string)
if r.isRemoteResource(v) {
return v, nil
}
return r.absPath(v)
}

View File

@ -0,0 +1,37 @@
/*
Copyright 2020 The Compose Specification Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package paths
import (
"os"
"path/filepath"
"strings"
"github.com/sirupsen/logrus"
)
func ExpandUser(p string) string {
if strings.HasPrefix(p, "~") {
home, err := os.UserHomeDir()
if err != nil {
logrus.Warn("cannot expand '~', because the environment lacks HOME")
return p
}
return filepath.Join(home, p[1:])
}
return p
}

View File

@ -0,0 +1,161 @@
/*
Copyright 2020 The Compose Specification Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package paths
import (
"errors"
"fmt"
"path/filepath"
"github.com/compose-spec/compose-go/v2/tree"
"github.com/compose-spec/compose-go/v2/types"
)
type resolver func(any) (any, error)
// ResolveRelativePaths make relative paths absolute
func ResolveRelativePaths(project map[string]any, base string, remotes []RemoteResource) error {
r := relativePathsResolver{
workingDir: base,
remotes: remotes,
}
r.resolvers = map[tree.Path]resolver{
"services.*.build.context": r.absContextPath,
"services.*.build.additional_contexts.*": r.absContextPath,
"services.*.env_file.*.path": r.absPath,
"services.*.extends.file": r.absExtendsPath,
"services.*.develop.watch.*.path": r.absPath,
"services.*.volumes.*": r.absVolumeMount,
"configs.*.file": r.maybeUnixPath,
"secrets.*.file": r.maybeUnixPath,
"include.path": r.absPath,
"include.project_directory": r.absPath,
"include.env_file": r.absPath,
"volumes.*": r.volumeDriverOpts,
}
_, err := r.resolveRelativePaths(project, tree.NewPath())
return err
}
type RemoteResource func(path string) bool
type relativePathsResolver struct {
workingDir string
remotes []RemoteResource
resolvers map[tree.Path]resolver
}
func (r *relativePathsResolver) isRemoteResource(path string) bool {
for _, remote := range r.remotes {
if remote(path) {
return true
}
}
return false
}
func (r *relativePathsResolver) resolveRelativePaths(value any, p tree.Path) (any, error) {
for pattern, resolver := range r.resolvers {
if p.Matches(pattern) {
return resolver(value)
}
}
switch v := value.(type) {
case map[string]any:
for k, e := range v {
resolved, err := r.resolveRelativePaths(e, p.Next(k))
if err != nil {
return nil, err
}
v[k] = resolved
}
case []any:
for i, e := range v {
resolved, err := r.resolveRelativePaths(e, p.Next("[]"))
if err != nil {
return nil, err
}
v[i] = resolved
}
}
return value, nil
}
func (r *relativePathsResolver) absPath(value any) (any, error) {
switch v := value.(type) {
case []any:
for i, s := range v {
abs, err := r.absPath(s)
if err != nil {
return nil, err
}
v[i] = abs
}
return v, nil
case string:
v = ExpandUser(v)
if filepath.IsAbs(v) {
return v, nil
}
if v != "" {
return filepath.Join(r.workingDir, v), nil
}
return v, nil
}
return nil, fmt.Errorf("unexpected type %T", value)
}
func (r *relativePathsResolver) absVolumeMount(a any) (any, error) {
vol := a.(map[string]any)
if vol["type"] != types.VolumeTypeBind {
return vol, nil
}
src, ok := vol["source"]
if !ok {
return nil, errors.New(`invalid mount config for type "bind": field Source must not be empty`)
}
abs, err := r.maybeUnixPath(src.(string))
if err != nil {
return nil, err
}
vol["source"] = abs
return vol, nil
}
func (r *relativePathsResolver) volumeDriverOpts(a any) (any, error) {
if a == nil {
return nil, nil
}
vol := a.(map[string]any)
if vol["driver"] != "local" {
return vol, nil
}
do, ok := vol["driver_opts"]
if !ok {
return vol, nil
}
opts := do.(map[string]any)
if dev, ok := opts["device"]; opts["o"] == "bind" && ok {
// This is actually a bind mount
path, err := r.maybeUnixPath(dev)
if err != nil {
return nil, err
}
opts["device"] = path
}
return vol, nil
}

View File

@ -0,0 +1,40 @@
/*
Copyright 2020 The Compose Specification Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package paths
import (
"path"
"path/filepath"
)
func (r *relativePathsResolver) maybeUnixPath(a any) (any, error) {
p := a.(string)
p = ExpandUser(p)
// Check if source is an absolute path (either Unix or Windows), to
// handle a Windows client with a Unix daemon or vice-versa.
//
// Note that this is not required for Docker for Windows when specifying
// a local Windows path, because Docker for Windows translates the Windows
// path into a valid path within the VM.
if !path.IsAbs(p) && !isWindowsAbs(p) {
if filepath.IsAbs(p) {
return p, nil
}
return filepath.Join(r.workingDir, p), nil
}
return p, nil
}

View File

@ -0,0 +1,82 @@
/*
Copyright 2020 The Compose Specification Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package paths
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// https://github.com/golang/go/blob/master/LICENSE
// This file contains utilities to check for Windows absolute paths on Linux.
// The code in this file was largely copied from the Golang filepath package
// https://github.com/golang/go/blob/1d0e94b1e13d5e8a323a63cd1cc1ef95290c9c36/src/path/filepath/path_windows.go#L12-L65
func isSlash(c uint8) bool {
return c == '\\' || c == '/'
}
// isAbs reports whether the path is a Windows absolute path.
func isWindowsAbs(path string) (b bool) {
l := volumeNameLen(path)
if l == 0 {
return false
}
path = path[l:]
if path == "" {
return false
}
return isSlash(path[0])
}
// volumeNameLen returns length of the leading volume name on Windows.
// It returns 0 elsewhere.
// nolint: gocyclo
func volumeNameLen(path string) int {
if len(path) < 2 {
return 0
}
// with drive letter
c := path[0]
if path[1] == ':' && ('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z') {
return 2
}
// is it UNC? https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
if l := len(path); l >= 5 && isSlash(path[0]) && isSlash(path[1]) &&
!isSlash(path[2]) && path[2] != '.' {
// first, leading `\\` and next shouldn't be `\`. its server name.
for n := 3; n < l-1; n++ {
// second, next '\' shouldn't be repeated.
if isSlash(path[n]) {
n++
// third, following something characters. its share name.
if !isSlash(path[n]) {
if path[n] == '.' {
break
}
for ; n < l; n++ {
if isSlash(path[n]) {
break
}
}
return n
}
break
}
}
}
return 0
}