remove unused code to only rely on api.Service

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof
2025-10-20 12:36:30 +02:00
committed by Guillaume Lours
parent e19e1278b5
commit e25265dd55
6 changed files with 25 additions and 132 deletions

View File

@@ -40,8 +40,6 @@ import (
"github.com/docker/cli/cli/command"
"github.com/docker/cli/pkg/kvfile"
"github.com/docker/compose/v2/cmd/formatter"
"github.com/docker/compose/v2/internal/desktop"
"github.com/docker/compose/v2/internal/experimental"
"github.com/docker/compose/v2/internal/tracing"
"github.com/docker/compose/v2/pkg/api"
ui "github.com/docker/compose/v2/pkg/progress"
@@ -91,14 +89,6 @@ func init() {
dotenv.RegisterFormat("raw", rawEnv)
}
type Backend interface {
api.Service
SetDesktopClient(cli *desktop.Client)
SetExperiments(experiments *experimental.State)
}
// Command defines a compose CLI command as a func with args
type Command func(context.Context, []string) error
@@ -426,7 +416,7 @@ func RunningAsStandalone() bool {
}
// RootCommand returns the compose command with its child commands
func RootCommand(dockerCli command.Cli, backend Backend) *cobra.Command { //nolint:gocyclo
func RootCommand(dockerCli command.Cli, backend api.Service) *cobra.Command { //nolint:gocyclo
// filter out useless commandConn.CloseWrite warning message that can occur
// when using a remote context that is unreachable: "commandConn.CloseWrite: commandconn: failed to wait: signal: killed"
// https://github.com/docker/cli/blob/e1f24d3c93df6752d3c27c8d61d18260f141310c/cli/connhelper/commandconn/commandconn.go#L203-L215
@@ -437,7 +427,6 @@ func RootCommand(dockerCli command.Cli, backend Backend) *cobra.Command { //noli
"commandConn.CloseRead:",
))
experiments := experimental.NewState()
opts := ProjectOptions{}
var (
ansi string
@@ -581,27 +570,6 @@ func RootCommand(dockerCli command.Cli, backend Backend) *cobra.Command { //noli
}
cmd.SetContext(ctx)
// (6) Desktop integration
var desktopCli *desktop.Client
if !dryRun {
if desktopCli, err = desktop.NewFromDockerClient(ctx, dockerCli); desktopCli != nil {
logrus.Debugf("Enabled Docker Desktop integration (experimental) @ %s", desktopCli.Endpoint())
backend.SetDesktopClient(desktopCli)
} else if err != nil {
// not fatal, Compose will still work but behave as though
// it's not running as part of Docker Desktop
logrus.Debugf("failed to enable Docker Desktop integration: %v", err)
} else {
logrus.Trace("Docker Desktop integration not enabled")
}
}
// (7) experimental features
if err := experiments.Load(ctx, desktopCli); err != nil {
logrus.Debugf("Failed to query feature flags from Desktop: %v", err)
}
backend.SetExperiments(experiments)
return nil
},
}
@@ -715,15 +683,3 @@ var printerModes = []string{
ui.ModeJSON,
ui.ModeQuiet,
}
func SetUnchangedOption(name string, experimentalFlag bool) bool {
var value bool
// If the var is defined we use that value first
if envVar, ok := os.LookupEnv(name); ok {
value = utils.StringToBool(envVar)
} else {
// if not, we try to get it from experimental feature flag
value = experimentalFlag
}
return value
}

View File

@@ -36,10 +36,7 @@ import (
func pluginMain() {
plugin.Run(
func(dockerCli command.Cli) *cobra.Command {
// TODO(milas): this cast is safe but we should not need to do this,
// we should expose the concrete service type so that we do not need
// to rely on the `api.Service` interface internally
backend := compose.NewComposeService(dockerCli).(commands.Backend)
backend := compose.NewComposeService(dockerCli)
cmd := commands.RootCommand(dockerCli, backend)
originalPreRunE := cmd.PersistentPreRunE
cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {

View File

@@ -1,62 +0,0 @@
/*
Copyright 2024 Docker Compose CLI 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 desktop
import (
"context"
"fmt"
"strings"
"time"
"github.com/docker/cli/cli/command"
)
// engineLabelDesktopAddress is used to detect that Compose is running with a
// Docker Desktop context. When this label is present, the value is an endpoint
// address for an in-memory socket (AF_UNIX or named pipe).
const engineLabelDesktopAddress = "com.docker.desktop.address"
// NewFromDockerClient creates a Desktop Client using the Docker CLI client to
// auto-discover the Desktop CLI socket endpoint (if available).
//
// An error is returned if there is a failure communicating with Docker Desktop,
// but even on success, a nil Client can be returned if the active Docker Engine
// is not a Desktop instance.
func NewFromDockerClient(ctx context.Context, dockerCli command.Cli) (*Client, error) {
// safeguard to make sure this doesn't get stuck indefinitely
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
info, err := dockerCli.Client().Info(ctx)
if err != nil {
return nil, fmt.Errorf("querying server info: %w", err)
}
for _, l := range info.Labels {
k, v, ok := strings.Cut(l, "=")
if !ok || k != engineLabelDesktopAddress {
continue
}
desktopCli := NewClient(v)
_, err := desktopCli.Ping(ctx)
if err != nil {
return nil, fmt.Errorf("pinging Desktop API: %w", err)
}
return desktopCli, nil
}
return nil, nil
}

View File

@@ -38,8 +38,6 @@ import (
"github.com/docker/docker/client"
"github.com/jonboulle/clockwork"
"github.com/docker/compose/v2/internal/desktop"
"github.com/docker/compose/v2/internal/experimental"
"github.com/docker/compose/v2/pkg/api"
)
@@ -63,10 +61,7 @@ func NewComposeService(dockerCli command.Cli) api.Service {
}
type composeService struct {
dockerCli command.Cli
desktopCli *desktop.Client
experiments *experimental.State
dockerCli command.Cli
clock clockwork.Clock
maxConcurrency int
dryRun bool
@@ -81,9 +76,6 @@ func (s *composeService) Close() error {
if s.dockerCli != nil {
errs = append(errs, s.dockerCli.Client().Close())
}
if s.isDesktopIntegrationActive() {
errs = append(errs, s.desktopCli.Close())
}
return errors.Join(errs...)
}
@@ -321,7 +313,3 @@ func (s *composeService) RuntimeVersion(ctx context.Context) (string, error) {
})
return runtimeVersion.val, runtimeVersion.err
}
func (s *composeService) isDesktopIntegrationActive() bool {
return s.desktopCli != nil
}

View File

@@ -17,14 +17,25 @@
package compose
import (
"github.com/docker/compose/v2/internal/desktop"
"github.com/docker/compose/v2/internal/experimental"
"context"
"strings"
)
func (s *composeService) SetDesktopClient(cli *desktop.Client) {
s.desktopCli = cli
}
// engineLabelDesktopAddress is used to detect that Compose is running with a
// Docker Desktop context. When this label is present, the value is an endpoint
// address for an in-memory socket (AF_UNIX or named pipe).
const engineLabelDesktopAddress = "com.docker.desktop.address"
func (s *composeService) SetExperiments(experiments *experimental.State) {
s.experiments = experiments
func (s *composeService) isDesktopIntegrationActive(ctx context.Context) (bool, error) {
info, err := s.apiClient().Info(ctx)
if err != nil {
return false, err
}
for _, l := range info.Labels {
k, _, ok := strings.Cut(l, "=")
if ok && k == engineLabelDesktopAddress {
return true, nil
}
}
return false, nil
}

View File

@@ -83,7 +83,10 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
options.Start.NavigationMenu = false
} else {
defer keyboard.Close() //nolint:errcheck
isDockerDesktopActive := s.isDesktopIntegrationActive()
isDockerDesktopActive, err := s.isDesktopIntegrationActive(ctx)
if err != nil {
return err
}
tracing.KeyboardMetrics(ctx, options.Start.NavigationMenu, isDockerDesktopActive)
navigationMenu = formatter.NewKeyboardManager(isDockerDesktopActive, signalChan)
logConsumer = navigationMenu.Decorate(logConsumer)