mirror of
https://github.com/docker/compose.git
synced 2026-09-01 07:11:06 +00:00
refactor(cmd): extract withBackend helper to remove CLI boilerplate
Assisted-By: docker-agent Signed-off-by: Nicolas De loof <nicolas.deloof@gmail.com>
This commit is contained in:
parent
88545507e8
commit
616adea1b1
6 changed files with 88 additions and 76 deletions
46
cmd/compose/backend.go
Normal file
46
cmd/compose/backend.go
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
Copyright 2020 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 compose
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/docker/cli/cli/command"
|
||||
|
||||
"github.com/docker/compose/v5/pkg/api"
|
||||
"github.com/docker/compose/v5/pkg/compose"
|
||||
)
|
||||
|
||||
// withBackend creates a compose backend and passes it to fn.
|
||||
func withBackend(ctx context.Context, dockerCli command.Cli, opts *BackendOptions, fn func(api.Compose) error) error {
|
||||
backend, err := compose.NewComposeService(dockerCli, opts.Options...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return fn(backend)
|
||||
}
|
||||
|
||||
// optionalTimeout converts an integer timeout (in seconds) into a *time.Duration.
|
||||
// If changed is false, nil is returned (no timeout was explicitly set).
|
||||
func optionalTimeout(t int, changed bool) *time.Duration {
|
||||
if !changed {
|
||||
return nil
|
||||
}
|
||||
d := time.Duration(t) * time.Second
|
||||
return &d
|
||||
}
|
||||
|
|
@ -26,7 +26,6 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/docker/compose/v5/pkg/api"
|
||||
"github.com/docker/compose/v5/pkg/compose"
|
||||
"github.com/docker/compose/v5/pkg/utils"
|
||||
)
|
||||
|
||||
|
|
@ -63,19 +62,17 @@ func runKill(ctx context.Context, dockerCli command.Cli, backendOptions *Backend
|
|||
return err
|
||||
}
|
||||
|
||||
backend, err := compose.NewComposeService(dockerCli, backendOptions.Options...)
|
||||
if err != nil {
|
||||
return withBackend(ctx, dockerCli, backendOptions, func(backend api.Compose) error {
|
||||
err := backend.Kill(ctx, name, api.KillOptions{
|
||||
RemoveOrphans: opts.removeOrphans,
|
||||
Project: project,
|
||||
Services: services,
|
||||
Signal: opts.signal,
|
||||
})
|
||||
if errors.Is(err, api.ErrNoResources) {
|
||||
_, _ = fmt.Fprintln(stdinfo(dockerCli), "No container to kill")
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
err = backend.Kill(ctx, name, api.KillOptions{
|
||||
RemoveOrphans: opts.removeOrphans,
|
||||
Project: project,
|
||||
Services: services,
|
||||
Signal: opts.signal,
|
||||
})
|
||||
if errors.Is(err, api.ErrNoResources) {
|
||||
_, _ = fmt.Fprintln(stdinfo(dockerCli), "No container to kill")
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,7 +23,6 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/docker/compose/v5/pkg/api"
|
||||
"github.com/docker/compose/v5/pkg/compose"
|
||||
)
|
||||
|
||||
type pauseOptions struct {
|
||||
|
|
@ -50,14 +49,11 @@ func runPause(ctx context.Context, dockerCli command.Cli, backendOptions *Backen
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
backend, err := compose.NewComposeService(dockerCli, backendOptions.Options...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return backend.Pause(ctx, name, api.PauseOptions{
|
||||
Services: services,
|
||||
Project: project,
|
||||
return withBackend(ctx, dockerCli, backendOptions, func(backend api.Compose) error {
|
||||
return backend.Pause(ctx, name, api.PauseOptions{
|
||||
Services: services,
|
||||
Project: project,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -85,13 +81,10 @@ func runUnPause(ctx context.Context, dockerCli command.Cli, backendOptions *Back
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
backend, err := compose.NewComposeService(dockerCli, backendOptions.Options...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return backend.UnPause(ctx, name, api.PauseOptions{
|
||||
Services: services,
|
||||
Project: project,
|
||||
return withBackend(ctx, dockerCli, backendOptions, func(backend api.Compose) error {
|
||||
return backend.UnPause(ctx, name, api.PauseOptions{
|
||||
Services: services,
|
||||
Project: project,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,13 +18,11 @@ package compose
|
|||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/docker/cli/cli/command"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/docker/compose/v5/pkg/api"
|
||||
"github.com/docker/compose/v5/pkg/compose"
|
||||
)
|
||||
|
||||
type restartOptions struct {
|
||||
|
|
@ -69,20 +67,12 @@ func runRestart(ctx context.Context, dockerCli command.Cli, backendOptions *Back
|
|||
}
|
||||
}
|
||||
|
||||
var timeout *time.Duration
|
||||
if opts.timeChanged {
|
||||
timeoutValue := time.Duration(opts.timeout) * time.Second
|
||||
timeout = &timeoutValue
|
||||
}
|
||||
|
||||
backend, err := compose.NewComposeService(dockerCli, backendOptions.Options...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return backend.Restart(ctx, name, api.RestartOptions{
|
||||
Timeout: timeout,
|
||||
Services: services,
|
||||
Project: project,
|
||||
NoDeps: opts.noDeps,
|
||||
return withBackend(ctx, dockerCli, backendOptions, func(backend api.Compose) error {
|
||||
return backend.Restart(ctx, name, api.RestartOptions{
|
||||
Timeout: optionalTimeout(opts.timeout, opts.timeChanged),
|
||||
Services: services,
|
||||
Project: project,
|
||||
NoDeps: opts.noDeps,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,7 +24,6 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/docker/compose/v5/pkg/api"
|
||||
"github.com/docker/compose/v5/pkg/compose"
|
||||
)
|
||||
|
||||
type startOptions struct {
|
||||
|
|
@ -58,20 +57,17 @@ func runStart(ctx context.Context, dockerCli command.Cli, backendOptions *Backen
|
|||
return err
|
||||
}
|
||||
|
||||
backend, err := compose.NewComposeService(dockerCli, backendOptions.Options...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var timeout time.Duration
|
||||
if opts.waitTimeout > 0 {
|
||||
timeout = time.Duration(opts.waitTimeout) * time.Second
|
||||
}
|
||||
return backend.Start(ctx, name, api.StartOptions{
|
||||
AttachTo: services,
|
||||
Project: project,
|
||||
Services: services,
|
||||
Wait: opts.wait,
|
||||
WaitTimeout: timeout,
|
||||
return withBackend(ctx, dockerCli, backendOptions, func(backend api.Compose) error {
|
||||
return backend.Start(ctx, name, api.StartOptions{
|
||||
AttachTo: services,
|
||||
Project: project,
|
||||
Services: services,
|
||||
Wait: opts.wait,
|
||||
WaitTimeout: timeout,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,13 +18,11 @@ package compose
|
|||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/docker/cli/cli/command"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/docker/compose/v5/pkg/api"
|
||||
"github.com/docker/compose/v5/pkg/compose"
|
||||
)
|
||||
|
||||
type stopOptions struct {
|
||||
|
|
@ -59,19 +57,11 @@ func runStop(ctx context.Context, dockerCli command.Cli, backendOptions *Backend
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var timeout *time.Duration
|
||||
if opts.timeChanged {
|
||||
timeoutValue := time.Duration(opts.timeout) * time.Second
|
||||
timeout = &timeoutValue
|
||||
}
|
||||
backend, err := compose.NewComposeService(dockerCli, backendOptions.Options...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return backend.Stop(ctx, name, api.StopOptions{
|
||||
Timeout: timeout,
|
||||
Services: services,
|
||||
Project: project,
|
||||
return withBackend(ctx, dockerCli, backendOptions, func(backend api.Compose) error {
|
||||
return backend.Stop(ctx, name, api.StopOptions{
|
||||
Timeout: optionalTimeout(opts.timeout, opts.timeChanged),
|
||||
Services: services,
|
||||
Project: project,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue