cli/compose: use stdlib errors

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-08-11 15:03:39 +02:00
parent 2dd462cc36
commit d38317c781
7 changed files with 63 additions and 64 deletions

View File

@ -2,6 +2,8 @@ package convert
import (
"context"
"errors"
"fmt"
"os"
"sort"
"strings"
@ -14,7 +16,6 @@ import (
"github.com/moby/moby/api/types/swarm"
"github.com/moby/moby/api/types/versions"
"github.com/moby/moby/client"
"github.com/pkg/errors"
)
const (
@ -34,16 +35,16 @@ func Services(
for _, service := range config.Services {
secrets, err := convertServiceSecrets(ctx, apiClient, namespace, service.Secrets, config.Secrets)
if err != nil {
return nil, errors.Wrapf(err, "service %s", service.Name)
return nil, fmt.Errorf("service %s: %w", service.Name, err)
}
configs, err := convertServiceConfigObjs(ctx, apiClient, namespace, service, config.Configs)
if err != nil {
return nil, errors.Wrapf(err, "service %s", service.Name)
return nil, fmt.Errorf("service %s: %w", service.Name, err)
}
serviceSpec, err := Service(apiClient.ClientVersion(), namespace, service, config.Networks, config.Volumes, secrets, configs)
if err != nil {
return nil, errors.Wrapf(err, "service %s", service.Name)
return nil, fmt.Errorf("service %s: %w", service.Name, err)
}
result[service.Name] = serviceSpec
}
@ -212,7 +213,7 @@ func convertServiceNetworks(
for networkName, network := range networks {
networkConfig, ok := networkConfigs[networkName]
if !ok && networkName != defaultNetwork {
return nil, errors.Errorf("undefined network %q", networkName)
return nil, fmt.Errorf("undefined network %q", networkName)
}
var aliases []string
var driverOpts map[string]string
@ -256,7 +257,7 @@ func convertServiceSecrets(
lookup := func(key string) (composetypes.FileObjectConfig, error) {
secretSpec, exists := secretSpecs[key]
if !exists {
return composetypes.FileObjectConfig{}, errors.Errorf("undefined secret %q", key)
return composetypes.FileObjectConfig{}, fmt.Errorf("undefined secret %q", key)
}
return composetypes.FileObjectConfig(secretSpec), nil
}
@ -301,7 +302,7 @@ func convertServiceConfigObjs(
lookup := func(key string) (composetypes.FileObjectConfig, error) {
configSpec, exists := configSpecs[key]
if !exists {
return composetypes.FileObjectConfig{}, errors.Errorf("undefined config %q", key)
return composetypes.FileObjectConfig{}, fmt.Errorf("undefined config %q", key)
}
return composetypes.FileObjectConfig(configSpec), nil
}
@ -318,7 +319,7 @@ func convertServiceConfigObjs(
})
}
// finally, after converting all of the file objects, create any
// finally, after converting all file objects, create any
// Runtime-type configs that are needed. these are configs that are not
// mounted into the container, but are used in some other way by the
// container runtime. Currently, this only means CredentialSpecs, but in
@ -442,7 +443,7 @@ func convertHealthcheck(healthcheck *composetypes.HealthCheckConfig) (*container
)
if healthcheck.Disable {
if len(healthcheck.Test) != 0 {
return nil, errors.Errorf("test and disable can't be set at the same time")
return nil, errors.New("test and disable can't be set at the same time")
}
return &container.HealthConfig{
Test: []string{"NONE"},
@ -494,7 +495,7 @@ func convertRestartPolicy(restart string, source *composetypes.RestartPolicy) (*
MaxAttempts: &attempts,
}, nil
default:
return nil, errors.Errorf("unknown restart policy: %s", restart)
return nil, fmt.Errorf("unknown restart policy: %s", restart)
}
}
@ -618,12 +619,12 @@ func convertDeployMode(mode string, replicas *uint64) (swarm.ServiceMode, error)
switch mode {
case "global-job":
if replicas != nil {
return serviceMode, errors.Errorf("replicas can only be used with replicated or replicated-job mode")
return serviceMode, errors.New("replicas can only be used with replicated or replicated-job mode")
}
serviceMode.GlobalJob = &swarm.GlobalJob{}
case "global":
if replicas != nil {
return serviceMode, errors.Errorf("replicas can only be used with replicated or replicated-job mode")
return serviceMode, errors.New("replicas can only be used with replicated or replicated-job mode")
}
serviceMode.Global = &swarm.GlobalService{}
case "replicated-job":
@ -634,7 +635,7 @@ func convertDeployMode(mode string, replicas *uint64) (swarm.ServiceMode, error)
case "replicated", "":
serviceMode.Replicated = &swarm.ReplicatedService{Replicas: replicas}
default:
return serviceMode, errors.Errorf("Unknown mode: %s", mode)
return serviceMode, fmt.Errorf("unknown mode: %s", mode)
}
return serviceMode, nil
}
@ -667,9 +668,9 @@ func convertCredentialSpec(namespace Namespace, spec composetypes.CredentialSpec
case l == 0:
return nil, nil
case l == 2:
return nil, errors.Errorf("invalid credential spec: cannot specify both %s and %s", o[0], o[1])
return nil, fmt.Errorf("invalid credential spec: cannot specify both %s and %s", o[0], o[1])
case l > 2:
return nil, errors.Errorf("invalid credential spec: cannot specify both %s, and %s", strings.Join(o[:l-1], ", "), o[l-1])
return nil, fmt.Errorf("invalid credential spec: cannot specify both %s, and %s", strings.Join(o[:l-1], ", "), o[l-1])
}
swarmCredSpec := swarm.CredentialSpec(spec)
// if we're using a swarm Config for the credential spec, over-write it
@ -688,7 +689,7 @@ func convertCredentialSpec(namespace Namespace, spec composetypes.CredentialSpec
return &swarmCredSpec, nil
}
}
return nil, errors.Errorf("invalid credential spec: spec specifies config %v, but no such config can be found", swarmCredSpec.Config)
return nil, fmt.Errorf("invalid credential spec: spec specifies config %v, but no such config can be found", swarmCredSpec.Config)
}
return &swarmCredSpec, nil
}

View File

@ -1,11 +1,12 @@
package convert
import (
"errors"
"fmt"
"strings"
composetypes "github.com/docker/cli/cli/compose/types"
"github.com/moby/moby/api/types/mount"
"github.com/pkg/errors"
)
type volumes map[string]composetypes.VolumeConfig
@ -59,7 +60,7 @@ func handleVolumeToMount(
stackVolume, exists := stackVolumes[volume.Source]
if !exists {
return mount.Mount{}, errors.Errorf("undefined volume %q", volume.Source)
return mount.Mount{}, fmt.Errorf("undefined volume %q", volume.Source)
}
result.Source = namespace.Scope(volume.Source)
@ -219,7 +220,7 @@ func handleClusterToMount(
// external volumes with a given group exist.
stackVolume, exists := stackVolumes[volume.Source]
if !exists {
return mount.Mount{}, errors.Errorf("undefined volume %q", volume.Source)
return mount.Mount{}, fmt.Errorf("undefined volume %q", volume.Source)
}
// if the volume is not specified with a group source, we may namespace