Plumb contexts through commands

This is to prepare for otel support.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Brian Goff
2023-09-09 22:27:44 +00:00
committed by Sebastiaan van Stijn
parent 9eb632dc7c
commit 5400a48aaf
146 changed files with 409 additions and 470 deletions

View File

@ -48,7 +48,7 @@ func NewStackCommand(dockerCli command.Cli) *cobra.Command {
// completeNames offers completion for swarm stacks
func completeNames(dockerCli command.Cli) completion.ValidArgsFn {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
list, err := swarm.GetStacks(dockerCli)
list, err := swarm.GetStacks(cmd.Context(), dockerCli)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

View File

@ -26,7 +26,7 @@ func newDeployCommand(dockerCli command.Cli) *cobra.Command {
if err != nil {
return err
}
return swarm.RunDeploy(dockerCli, opts, config)
return swarm.RunDeploy(cmd.Context(), dockerCli, opts, config)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completeNames(dockerCli)(cmd, args, toComplete)

View File

@ -1,6 +1,7 @@
package stack
import (
"context"
"sort"
"github.com/docker/cli/cli"
@ -23,7 +24,7 @@ func newListCommand(dockerCli command.Cli) *cobra.Command {
Short: "List stacks",
Args: cli.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return RunList(dockerCli, opts)
return RunList(cmd.Context(), dockerCli, opts)
},
ValidArgsFunction: completion.NoComplete,
}
@ -34,8 +35,8 @@ func newListCommand(dockerCli command.Cli) *cobra.Command {
}
// RunList performs a stack list against the specified swarm cluster
func RunList(dockerCli command.Cli, opts options.List) error {
ss, err := swarm.GetStacks(dockerCli)
func RunList(ctx context.Context, dockerCli command.Cli, opts options.List) error {
ss, err := swarm.GetStacks(ctx, dockerCli)
if err != nil {
return err
}

View File

@ -22,7 +22,7 @@ func newPsCommand(dockerCli command.Cli) *cobra.Command {
if err := validateStackName(opts.Namespace); err != nil {
return err
}
return swarm.RunPS(dockerCli, opts)
return swarm.RunPS(cmd.Context(), dockerCli, opts)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completeNames(dockerCli)(cmd, args, toComplete)

View File

@ -21,7 +21,7 @@ func newRemoveCommand(dockerCli command.Cli) *cobra.Command {
if err := validateStackNames(opts.Namespaces); err != nil {
return err
}
return swarm.RunRemove(dockerCli, opts)
return swarm.RunRemove(cmd.Context(), dockerCli, opts)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completeNames(dockerCli)(cmd, args, toComplete)

View File

@ -1,6 +1,7 @@
package stack
import (
"context"
"fmt"
"sort"
@ -29,7 +30,7 @@ func newServicesCommand(dockerCli command.Cli) *cobra.Command {
if err := validateStackName(opts.Namespace); err != nil {
return err
}
return RunServices(dockerCli, opts)
return RunServices(cmd.Context(), dockerCli, opts)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completeNames(dockerCli)(cmd, args, toComplete)
@ -43,8 +44,8 @@ func newServicesCommand(dockerCli command.Cli) *cobra.Command {
}
// RunServices performs a stack services against the specified swarm cluster
func RunServices(dockerCli command.Cli, opts options.Services) error {
services, err := swarm.GetServices(dockerCli, opts)
func RunServices(ctx context.Context, dockerCli command.Cli, opts options.Services) error {
services, err := swarm.GetServices(ctx, dockerCli, opts)
if err != nil {
return err
}

View File

@ -22,9 +22,7 @@ const (
)
// RunDeploy is the swarm implementation of docker stack deploy
func RunDeploy(dockerCli command.Cli, opts options.Deploy, cfg *composetypes.Config) error {
ctx := context.Background()
func RunDeploy(ctx context.Context, dockerCli command.Cli, opts options.Deploy, cfg *composetypes.Config) error {
if err := validateResolveImageFlag(&opts); err != nil {
return err
}

View File

@ -56,7 +56,7 @@ func deployCompose(ctx context.Context, dockerCli command.Cli, opts options.Depl
return err
}
services, err := convert.Services(namespace, config, dockerCli.Client())
services, err := convert.Services(ctx, namespace, config, dockerCli.Client())
if err != nil {
return err
}

View File

@ -11,9 +11,9 @@ import (
)
// GetStacks lists the swarm stacks.
func GetStacks(dockerCli command.Cli) ([]*formatter.Stack, error) {
func GetStacks(ctx context.Context, dockerCli command.Cli) ([]*formatter.Stack, error) {
services, err := dockerCli.Client().ServiceList(
context.Background(),
ctx,
types.ServiceListOptions{Filters: getAllStacksFilter()})
if err != nil {
return nil, err

View File

@ -12,10 +12,9 @@ import (
)
// RunPS is the swarm implementation of docker stack ps
func RunPS(dockerCli command.Cli, opts options.PS) error {
func RunPS(ctx context.Context, dockerCli command.Cli, opts options.PS) error {
filter := getStackFilterFromOpt(opts.Namespace, opts.Filter)
ctx := context.Background()
client := dockerCli.Client()
tasks, err := client.TaskList(ctx, types.TaskListOptions{Filters: filter})
if err != nil {

View File

@ -15,9 +15,8 @@ import (
)
// RunRemove is the swarm implementation of docker stack remove
func RunRemove(dockerCli command.Cli, opts options.Remove) error {
func RunRemove(ctx context.Context, dockerCli command.Cli, opts options.Remove) error {
client := dockerCli.Client()
ctx := context.Background()
var errs []string
for _, namespace := range opts.Namespaces {

View File

@ -11,10 +11,9 @@ import (
)
// GetServices is the swarm implementation of listing stack services
func GetServices(dockerCli command.Cli, opts options.Services) ([]swarm.Service, error) {
func GetServices(ctx context.Context, dockerCli command.Cli, opts options.Services) ([]swarm.Service, error) {
var (
err error
ctx = context.Background()
client = dockerCli.Client()
)